aboutsummaryrefslogtreecommitdiffstats
path: root/tests/test_urlpatterns.py
blob: e0060e69039cf805063bfa1fd398f3d01204a5db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
from __future__ import unicode_literals
from collections import namedtuple
from django.conf.urls import patterns, url, include
from django.core import urlresolvers
from django.test import TestCase
from rest_framework.test import APIRequestFactory
from rest_framework.urlpatterns import format_suffix_patterns


# A container class for test paths for the test case
URLTestPath = namedtuple('URLTestPath', ['path', 'args', 'kwargs'])


def dummy_view(request, *args, **kwargs):
    pass


class FormatSuffixTests(TestCase):
    """
    Tests `format_suffix_patterns` against different URLPatterns to ensure the URLs still resolve properly, including any captured parameters.
    """
    def _resolve_urlpatterns(self, urlpatterns, test_paths):
        factory = APIRequestFactory()
        try:
            urlpatterns = format_suffix_patterns(urlpatterns)
        except Exception:
            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)
            except Exception:
                self.fail("Failed to resolve URL: %s" % request.path_info)
            self.assertEqual(callback_args, test_path.args)
            self.assertEqual(callback_kwargs, test_path.kwargs)

    def test_format_suffix(self):
        urlpatterns = patterns(
            '',
            url(r'^test$', dummy_view),
        )
        test_paths = [
            URLTestPath('/test', (), {}),
            URLTestPath('/test.api', (), {'format': 'api'}),
            URLTestPath('/test.asdf', (), {'format': 'asdf'}),
        ]
        self._resolve_urlpatterns(urlpatterns, test_paths)

    def test_default_args(self):
        urlpatterns = patterns(
            '',
            url(r'^test$', dummy_view, {'foo': 'bar'}),
        )
        test_paths = [
            URLTestPath('/test', (), {'foo': 'bar', }),
            URLTestPath('/test.api', (), {'foo': 'bar', 'format': 'api'}),
            URLTestPath('/test.asdf', (), {'foo': 'bar', 'format': 'asdf'}),
        ]
        self._resolve_urlpatterns(urlpatterns, test_paths)

    def test_included_urls(self):
        nested_patterns = patterns(
            '',
            url(r'^path$', dummy_view)
        )
        urlpatterns = patterns(
            '',
            url(r'^test/', include(nested_patterns), {'foo': 'bar'}),
        )
        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)