2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2017-12-20 15:17:54 +03:00
|
|
|
import unittest
|
2013-01-21 00:59:27 +04:00
|
|
|
from collections import namedtuple
|
2015-06-25 23:55:51 +03:00
|
|
|
|
|
|
|
from django.conf.urls import include, url
|
2013-01-21 00:59:27 +04:00
|
|
|
from django.test import TestCase
|
2017-11-10 11:41:03 +03:00
|
|
|
from django.urls import Resolver404
|
2015-06-25 23:55:51 +03:00
|
|
|
|
2017-12-20 15:17:54 +03:00
|
|
|
from rest_framework.compat import make_url_resolver, path, re_path
|
2013-06-28 20:17:39 +04:00
|
|
|
from rest_framework.test import APIRequestFactory
|
2013-01-21 00:59:27 +04:00
|
|
|
from rest_framework.urlpatterns import format_suffix_patterns
|
|
|
|
|
|
|
|
# A container class for test paths for the test case
|
|
|
|
URLTestPath = namedtuple('URLTestPath', ['path', 'args', 'kwargs'])
|
|
|
|
|
|
|
|
|
2013-01-21 21:37:50 +04:00
|
|
|
def dummy_view(request, *args, **kwargs):
|
2013-01-21 00:59:27 +04:00
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
class FormatSuffixTests(TestCase):
|
2013-01-21 21:37:50 +04:00
|
|
|
"""
|
2015-06-11 01:32:05 +03:00
|
|
|
Tests `format_suffix_patterns` against different URLPatterns to ensure the
|
|
|
|
URLs still resolve properly, including any captured parameters.
|
2013-01-21 21:37:50 +04:00
|
|
|
"""
|
2017-12-20 15:17:54 +03:00
|
|
|
def _resolve_urlpatterns(self, urlpatterns, test_paths, allowed=None):
|
2013-06-28 20:17:39 +04:00
|
|
|
factory = APIRequestFactory()
|
2013-01-21 00:59:27 +04:00
|
|
|
try:
|
2017-12-20 15:17:54 +03:00
|
|
|
urlpatterns = format_suffix_patterns(urlpatterns, allowed=allowed)
|
2013-02-06 17:05:17 +04:00
|
|
|
except Exception:
|
2013-01-21 00:59:27 +04:00
|
|
|
self.fail("Failed to apply `format_suffix_patterns` on the supplied urlpatterns")
|
2017-10-16 10:33:31 +03:00
|
|
|
resolver = make_url_resolver(r'^/', urlpatterns)
|
2013-01-21 00:59:27 +04:00
|
|
|
for test_path in test_paths:
|
2017-12-20 15:17:54 +03:00
|
|
|
try:
|
|
|
|
test_path, expected_resolved = test_path
|
|
|
|
except (TypeError, ValueError):
|
|
|
|
expected_resolved = True
|
|
|
|
|
2013-01-21 00:59:27 +04:00
|
|
|
request = factory.get(test_path.path)
|
|
|
|
try:
|
|
|
|
callback, callback_args, callback_kwargs = resolver.resolve(request.path_info)
|
2017-12-20 15:17:54 +03:00
|
|
|
except Resolver404:
|
|
|
|
callback, callback_args, callback_kwargs = (None, None, None)
|
|
|
|
if expected_resolved:
|
|
|
|
raise
|
2013-02-06 17:05:17 +04:00
|
|
|
except Exception:
|
2013-01-21 00:59:27 +04:00
|
|
|
self.fail("Failed to resolve URL: %s" % request.path_info)
|
2017-12-20 15:17:54 +03:00
|
|
|
|
|
|
|
if not expected_resolved:
|
|
|
|
assert callback is None
|
|
|
|
continue
|
|
|
|
|
2017-01-04 12:13:32 +03:00
|
|
|
assert callback_args == test_path.args
|
|
|
|
assert callback_kwargs == test_path.kwargs
|
2013-01-21 00:59:27 +04:00
|
|
|
|
2017-12-20 15:17:54 +03:00
|
|
|
def _test_trailing_slash(self, urlpatterns):
|
2015-06-11 01:32:05 +03:00
|
|
|
test_paths = [
|
|
|
|
(URLTestPath('/test.api', (), {'format': 'api'}), True),
|
|
|
|
(URLTestPath('/test/.api', (), {'format': 'api'}), False),
|
|
|
|
(URLTestPath('/test.api/', (), {'format': 'api'}), True),
|
|
|
|
]
|
2017-12-20 15:17:54 +03:00
|
|
|
self._resolve_urlpatterns(urlpatterns, test_paths)
|
2015-06-11 01:32:05 +03:00
|
|
|
|
2017-12-20 15:17:54 +03:00
|
|
|
def test_trailing_slash(self):
|
|
|
|
urlpatterns = [
|
|
|
|
url(r'^test/$', dummy_view),
|
|
|
|
]
|
|
|
|
self._test_trailing_slash(urlpatterns)
|
2015-06-11 01:32:05 +03:00
|
|
|
|
2017-12-20 15:17:54 +03:00
|
|
|
@unittest.skipUnless(path, 'needs Django 2')
|
|
|
|
def test_trailing_slash_django2(self):
|
2015-06-11 01:32:05 +03:00
|
|
|
urlpatterns = [
|
2017-12-20 15:17:54 +03:00
|
|
|
path('test/', dummy_view),
|
2015-06-11 01:32:05 +03:00
|
|
|
]
|
2017-12-20 15:17:54 +03:00
|
|
|
self._test_trailing_slash(urlpatterns)
|
|
|
|
|
|
|
|
def _test_format_suffix(self, urlpatterns):
|
2013-01-21 00:59:27 +04:00
|
|
|
test_paths = [
|
|
|
|
URLTestPath('/test', (), {}),
|
|
|
|
URLTestPath('/test.api', (), {'format': 'api'}),
|
|
|
|
URLTestPath('/test.asdf', (), {'format': 'asdf'}),
|
|
|
|
]
|
2013-01-21 21:37:50 +04:00
|
|
|
self._resolve_urlpatterns(urlpatterns, test_paths)
|
2013-01-21 00:59:27 +04:00
|
|
|
|
2017-12-20 15:17:54 +03:00
|
|
|
def test_format_suffix(self):
|
2015-06-11 01:32:05 +03:00
|
|
|
urlpatterns = [
|
2017-12-20 15:17:54 +03:00
|
|
|
url(r'^test$', dummy_view),
|
|
|
|
]
|
|
|
|
self._test_format_suffix(urlpatterns)
|
|
|
|
|
|
|
|
@unittest.skipUnless(path, 'needs Django 2')
|
|
|
|
def test_format_suffix_django2(self):
|
|
|
|
urlpatterns = [
|
|
|
|
path('test', dummy_view),
|
2015-06-11 01:32:05 +03:00
|
|
|
]
|
2017-12-20 15:17:54 +03:00
|
|
|
self._test_format_suffix(urlpatterns)
|
|
|
|
|
|
|
|
@unittest.skipUnless(path, 'needs Django 2')
|
|
|
|
def test_format_suffix_django2_args(self):
|
|
|
|
urlpatterns = [
|
|
|
|
path('convtest/<int:pk>', dummy_view),
|
|
|
|
re_path(r'^retest/(?P<pk>[0-9]+)$', dummy_view),
|
|
|
|
]
|
|
|
|
test_paths = [
|
|
|
|
URLTestPath('/convtest/42', (), {'pk': 42}),
|
|
|
|
URLTestPath('/convtest/42.api', (), {'pk': 42, 'format': 'api'}),
|
|
|
|
URLTestPath('/convtest/42.asdf', (), {'pk': 42, 'format': 'asdf'}),
|
|
|
|
URLTestPath('/retest/42', (), {'pk': '42'}),
|
|
|
|
URLTestPath('/retest/42.api', (), {'pk': '42', 'format': 'api'}),
|
|
|
|
URLTestPath('/retest/42.asdf', (), {'pk': '42', 'format': 'asdf'}),
|
|
|
|
]
|
|
|
|
self._resolve_urlpatterns(urlpatterns, test_paths)
|
|
|
|
|
|
|
|
def _test_default_args(self, urlpatterns):
|
2013-01-21 00:59:27 +04:00
|
|
|
test_paths = [
|
|
|
|
URLTestPath('/test', (), {'foo': 'bar', }),
|
|
|
|
URLTestPath('/test.api', (), {'foo': 'bar', 'format': 'api'}),
|
|
|
|
URLTestPath('/test.asdf', (), {'foo': 'bar', 'format': 'asdf'}),
|
|
|
|
]
|
2013-01-21 21:37:50 +04:00
|
|
|
self._resolve_urlpatterns(urlpatterns, test_paths)
|
2013-01-21 00:59:27 +04:00
|
|
|
|
2017-12-20 15:17:54 +03:00
|
|
|
def test_default_args(self):
|
|
|
|
urlpatterns = [
|
|
|
|
url(r'^test$', dummy_view, {'foo': 'bar'}),
|
|
|
|
]
|
|
|
|
self._test_default_args(urlpatterns)
|
|
|
|
|
|
|
|
@unittest.skipUnless(path, 'needs Django 2')
|
|
|
|
def test_default_args_django2(self):
|
|
|
|
urlpatterns = [
|
|
|
|
path('test', dummy_view, {'foo': 'bar'}),
|
|
|
|
]
|
|
|
|
self._test_default_args(urlpatterns)
|
|
|
|
|
|
|
|
def _test_included_urls(self, urlpatterns):
|
|
|
|
test_paths = [
|
|
|
|
URLTestPath('/test/path', (), {'foo': 'bar', }),
|
|
|
|
URLTestPath('/test/path.api', (), {'foo': 'bar', 'format': 'api'}),
|
|
|
|
URLTestPath('/test/path.asdf', (), {'foo': 'bar', 'format': 'asdf'}),
|
|
|
|
]
|
|
|
|
self._resolve_urlpatterns(urlpatterns, test_paths)
|
|
|
|
|
2013-01-21 00:59:27 +04:00
|
|
|
def test_included_urls(self):
|
2015-06-11 01:32:05 +03:00
|
|
|
nested_patterns = [
|
2013-01-21 21:37:50 +04:00
|
|
|
url(r'^path$', dummy_view)
|
2015-06-11 01:32:05 +03:00
|
|
|
]
|
|
|
|
urlpatterns = [
|
2013-01-21 00:59:27 +04:00
|
|
|
url(r'^test/', include(nested_patterns), {'foo': 'bar'}),
|
2015-06-11 01:32:05 +03:00
|
|
|
]
|
2017-12-20 15:17:54 +03:00
|
|
|
self._test_included_urls(urlpatterns)
|
|
|
|
|
|
|
|
@unittest.skipUnless(path, 'needs Django 2')
|
|
|
|
def test_included_urls_django2(self):
|
|
|
|
nested_patterns = [
|
|
|
|
path('path', dummy_view)
|
|
|
|
]
|
|
|
|
urlpatterns = [
|
|
|
|
path('test/', include(nested_patterns), {'foo': 'bar'}),
|
|
|
|
]
|
|
|
|
self._test_included_urls(urlpatterns)
|
|
|
|
|
|
|
|
@unittest.skipUnless(path, 'needs Django 2')
|
|
|
|
def test_included_urls_django2_mixed(self):
|
|
|
|
nested_patterns = [
|
|
|
|
path('path', dummy_view)
|
|
|
|
]
|
|
|
|
urlpatterns = [
|
|
|
|
url('^test/', include(nested_patterns), {'foo': 'bar'}),
|
|
|
|
]
|
|
|
|
self._test_included_urls(urlpatterns)
|
|
|
|
|
|
|
|
@unittest.skipUnless(path, 'needs Django 2')
|
|
|
|
def test_included_urls_django2_mixed_args(self):
|
|
|
|
nested_patterns = [
|
|
|
|
path('path/<int:child>', dummy_view),
|
|
|
|
url('^url/(?P<child>[0-9]+)$', dummy_view)
|
|
|
|
]
|
|
|
|
urlpatterns = [
|
|
|
|
url('^purl/(?P<parent>[0-9]+)/', include(nested_patterns), {'foo': 'bar'}),
|
|
|
|
path('ppath/<int:parent>/', include(nested_patterns), {'foo': 'bar'}),
|
|
|
|
]
|
2013-01-21 00:59:27 +04:00
|
|
|
test_paths = [
|
2017-12-20 15:17:54 +03:00
|
|
|
# parent url() nesting child path()
|
|
|
|
URLTestPath('/purl/87/path/42', (), {'parent': '87', 'child': 42, 'foo': 'bar', }),
|
|
|
|
URLTestPath('/purl/87/path/42.api', (), {'parent': '87', 'child': 42, 'foo': 'bar', 'format': 'api'}),
|
|
|
|
URLTestPath('/purl/87/path/42.asdf', (), {'parent': '87', 'child': 42, 'foo': 'bar', 'format': 'asdf'}),
|
|
|
|
|
|
|
|
# parent path() nesting child url()
|
|
|
|
URLTestPath('/ppath/87/url/42', (), {'parent': 87, 'child': '42', 'foo': 'bar', }),
|
|
|
|
URLTestPath('/ppath/87/url/42.api', (), {'parent': 87, 'child': '42', 'foo': 'bar', 'format': 'api'}),
|
|
|
|
URLTestPath('/ppath/87/url/42.asdf', (), {'parent': 87, 'child': '42', 'foo': 'bar', 'format': 'asdf'}),
|
|
|
|
|
|
|
|
# parent path() nesting child path()
|
|
|
|
URLTestPath('/ppath/87/path/42', (), {'parent': 87, 'child': 42, 'foo': 'bar', }),
|
|
|
|
URLTestPath('/ppath/87/path/42.api', (), {'parent': 87, 'child': 42, 'foo': 'bar', 'format': 'api'}),
|
|
|
|
URLTestPath('/ppath/87/path/42.asdf', (), {'parent': 87, 'child': 42, 'foo': 'bar', 'format': 'asdf'}),
|
|
|
|
|
|
|
|
# parent url() nesting child url()
|
|
|
|
URLTestPath('/purl/87/url/42', (), {'parent': '87', 'child': '42', 'foo': 'bar', }),
|
|
|
|
URLTestPath('/purl/87/url/42.api', (), {'parent': '87', 'child': '42', 'foo': 'bar', 'format': 'api'}),
|
|
|
|
URLTestPath('/purl/87/url/42.asdf', (), {'parent': '87', 'child': '42', 'foo': 'bar', 'format': 'asdf'}),
|
2013-01-21 00:59:27 +04:00
|
|
|
]
|
2013-01-21 21:37:50 +04:00
|
|
|
self._resolve_urlpatterns(urlpatterns, test_paths)
|
2017-12-20 15:17:54 +03:00
|
|
|
|
|
|
|
def _test_allowed_formats(self, urlpatterns):
|
|
|
|
allowed_formats = ['good', 'ugly']
|
|
|
|
test_paths = [
|
|
|
|
(URLTestPath('/test.good/', (), {'format': 'good'}), True),
|
|
|
|
(URLTestPath('/test.bad', (), {}), False),
|
|
|
|
(URLTestPath('/test.ugly', (), {'format': 'ugly'}), True),
|
|
|
|
]
|
|
|
|
self._resolve_urlpatterns(urlpatterns, test_paths, allowed=allowed_formats)
|
|
|
|
|
|
|
|
def test_allowed_formats(self):
|
|
|
|
urlpatterns = [
|
|
|
|
url('^test$', dummy_view),
|
|
|
|
]
|
|
|
|
self._test_allowed_formats(urlpatterns)
|
|
|
|
|
|
|
|
@unittest.skipUnless(path, 'needs Django 2')
|
|
|
|
def test_allowed_formats_django2(self):
|
|
|
|
urlpatterns = [
|
|
|
|
path('test', dummy_view),
|
|
|
|
]
|
|
|
|
self._test_allowed_formats(urlpatterns)
|