2013-02-05 00:55:35 +04:00
|
|
|
from __future__ import unicode_literals
|
2013-01-21 00:59:27 +04:00
|
|
|
from collections import namedtuple
|
2015-06-11 01:36:37 +03:00
|
|
|
from django.conf.urls import url, include
|
2013-01-21 00:59:27 +04:00
|
|
|
from django.core import urlresolvers
|
|
|
|
from django.test import TestCase
|
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
|
|
|
"""
|
|
|
|
def _resolve_urlpatterns(self, urlpatterns, test_paths):
|
2013-06-28 20:17:39 +04:00
|
|
|
factory = APIRequestFactory()
|
2013-01-21 00:59:27 +04:00
|
|
|
try:
|
|
|
|
urlpatterns = format_suffix_patterns(urlpatterns)
|
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")
|
|
|
|
resolver = urlresolvers.RegexURLResolver(r'^/', urlpatterns)
|
|
|
|
for test_path in test_paths:
|
|
|
|
request = factory.get(test_path.path)
|
|
|
|
try:
|
|
|
|
callback, callback_args, callback_kwargs = resolver.resolve(request.path_info)
|
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)
|
2013-02-28 01:15:00 +04:00
|
|
|
self.assertEqual(callback_args, test_path.args)
|
|
|
|
self.assertEqual(callback_kwargs, test_path.kwargs)
|
2013-01-21 00:59:27 +04:00
|
|
|
|
2015-06-11 01:32:05 +03:00
|
|
|
def test_trailing_slash(self):
|
|
|
|
factory = APIRequestFactory()
|
|
|
|
urlpatterns = format_suffix_patterns([
|
|
|
|
url(r'^test/$', dummy_view),
|
|
|
|
])
|
|
|
|
resolver = urlresolvers.RegexURLResolver(r'^/', urlpatterns)
|
|
|
|
|
|
|
|
test_paths = [
|
|
|
|
(URLTestPath('/test.api', (), {'format': 'api'}), True),
|
|
|
|
(URLTestPath('/test/.api', (), {'format': 'api'}), False),
|
|
|
|
(URLTestPath('/test.api/', (), {'format': 'api'}), True),
|
|
|
|
]
|
|
|
|
|
|
|
|
for test_path, expected_resolved in test_paths:
|
|
|
|
request = factory.get(test_path.path)
|
|
|
|
try:
|
|
|
|
callback, callback_args, callback_kwargs = resolver.resolve(request.path_info)
|
|
|
|
except urlresolvers.Resolver404:
|
|
|
|
callback, callback_args, callback_kwargs = (None, None, None)
|
|
|
|
if not expected_resolved:
|
|
|
|
assert callback is None
|
|
|
|
continue
|
|
|
|
|
|
|
|
print(test_path, callback, callback_args, callback_kwargs)
|
|
|
|
assert callback_args == test_path.args
|
|
|
|
assert callback_kwargs == test_path.kwargs
|
|
|
|
|
2013-01-21 00:59:27 +04:00
|
|
|
def test_format_suffix(self):
|
2015-06-11 01:32:05 +03:00
|
|
|
urlpatterns = [
|
2013-01-21 21:37:50 +04:00
|
|
|
url(r'^test$', dummy_view),
|
2015-06-11 01:32:05 +03:00
|
|
|
]
|
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
|
|
|
|
|
|
|
def test_default_args(self):
|
2015-06-11 01:32:05 +03:00
|
|
|
urlpatterns = [
|
2013-01-21 21:37:50 +04:00
|
|
|
url(r'^test$', dummy_view, {'foo': 'bar'}),
|
2015-06-11 01:32:05 +03:00
|
|
|
]
|
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
|
|
|
|
|
|
|
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
|
|
|
]
|
2013-01-21 00:59:27 +04:00
|
|
|
test_paths = [
|
|
|
|
URLTestPath('/test/path', (), {'foo': 'bar', }),
|
|
|
|
URLTestPath('/test/path.api', (), {'foo': 'bar', 'format': 'api'}),
|
|
|
|
URLTestPath('/test/path.asdf', (), {'foo': 'bar', 'format': 'asdf'}),
|
|
|
|
]
|
2013-01-21 21:37:50 +04:00
|
|
|
self._resolve_urlpatterns(urlpatterns, test_paths)
|