2015-06-11 01:45:23 +03:00
|
|
|
from django.conf.urls import url
|
2016-06-01 17:31:00 +03:00
|
|
|
from django.test import TestCase, override_settings
|
2017-11-10 11:41:03 +03:00
|
|
|
from django.urls import NoReverseMatch
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2012-09-20 16:06:27 +04:00
|
|
|
from rest_framework.reverse import reverse
|
2013-06-28 20:17:39 +04:00
|
|
|
from rest_framework.test import APIRequestFactory
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2013-06-28 20:17:39 +04:00
|
|
|
factory = APIRequestFactory()
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2012-09-28 19:09:52 +04:00
|
|
|
def null_view(request):
|
|
|
|
pass
|
2012-09-20 16:06:27 +04:00
|
|
|
|
2017-05-29 22:15:07 +03:00
|
|
|
|
2015-06-11 01:45:23 +03:00
|
|
|
urlpatterns = [
|
2012-09-28 19:09:52 +04:00
|
|
|
url(r'^view$', null_view, name='view'),
|
2015-06-11 01:45:23 +03:00
|
|
|
]
|
2012-09-20 16:06:27 +04:00
|
|
|
|
|
|
|
|
2019-04-30 18:53:44 +03:00
|
|
|
class MockVersioningScheme:
|
2015-03-20 02:12:28 +03:00
|
|
|
|
|
|
|
def __init__(self, raise_error=False):
|
|
|
|
self.raise_error = raise_error
|
|
|
|
|
|
|
|
def reverse(self, *args, **kwargs):
|
|
|
|
if self.raise_error:
|
|
|
|
raise NoReverseMatch()
|
|
|
|
|
|
|
|
return 'http://scheme-reversed/view'
|
|
|
|
|
|
|
|
|
2016-06-01 17:31:00 +03:00
|
|
|
@override_settings(ROOT_URLCONF='tests.test_reverse')
|
2012-09-20 16:06:27 +04:00
|
|
|
class ReverseTests(TestCase):
|
|
|
|
"""
|
2013-02-25 17:48:21 +04:00
|
|
|
Tests for fully qualified URLs when using `reverse`.
|
2012-09-20 16:06:27 +04:00
|
|
|
"""
|
|
|
|
def test_reversed_urls_are_fully_qualified(self):
|
2012-09-28 19:09:52 +04:00
|
|
|
request = factory.get('/view')
|
|
|
|
url = reverse('view', request=request)
|
2017-01-12 19:41:16 +03:00
|
|
|
assert url == 'http://testserver/view'
|
2015-03-20 02:12:28 +03:00
|
|
|
|
|
|
|
def test_reverse_with_versioning_scheme(self):
|
|
|
|
request = factory.get('/view')
|
|
|
|
request.versioning_scheme = MockVersioningScheme()
|
|
|
|
|
|
|
|
url = reverse('view', request=request)
|
2017-01-12 19:41:16 +03:00
|
|
|
assert url == 'http://scheme-reversed/view'
|
2015-03-20 02:12:28 +03:00
|
|
|
|
|
|
|
def test_reverse_with_versioning_scheme_fallback_to_default_on_error(self):
|
|
|
|
request = factory.get('/view')
|
|
|
|
request.versioning_scheme = MockVersioningScheme(raise_error=True)
|
|
|
|
|
|
|
|
url = reverse('view', request=request)
|
2017-01-12 19:41:16 +03:00
|
|
|
assert url == 'http://testserver/view'
|