2008-08-31 19:11:20 +08:00
|
|
|
"""
|
|
|
|
Unit tests for reverse URL lookups.
|
|
|
|
"""
|
2021-07-29 13:04:56 +08:00
|
|
|
import pickle
|
2014-04-21 01:12:43 +08:00
|
|
|
import sys
|
2016-07-15 02:41:52 +08:00
|
|
|
import threading
|
2013-07-01 20:22:27 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from admin_scripts.tests import AdminScriptTestCase
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
from django.conf import settings
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.contrib.auth.models import User
|
2011-06-17 00:41:14 +08:00
|
|
|
from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist
|
2015-01-28 20:35:27 +08:00
|
|
|
from django.http import (
|
|
|
|
HttpRequest, HttpResponsePermanentRedirect, HttpResponseRedirect,
|
|
|
|
)
|
2009-03-21 21:09:13 +08:00
|
|
|
from django.shortcuts import redirect
|
2019-12-23 18:34:36 +08:00
|
|
|
from django.test import (
|
|
|
|
RequestFactory, SimpleTestCase, TestCase, override_settings,
|
|
|
|
)
|
2015-03-15 18:26:47 +08:00
|
|
|
from django.test.utils import override_script_prefix
|
2015-12-30 23:51:16 +08:00
|
|
|
from django.urls import (
|
2016-10-21 01:29:04 +08:00
|
|
|
NoReverseMatch, Resolver404, ResolverMatch, URLPattern, URLResolver,
|
2018-12-08 06:52:28 +08:00
|
|
|
get_callable, get_resolver, get_urlconf, include, path, re_path, resolve,
|
|
|
|
reverse, reverse_lazy,
|
2015-12-30 23:51:16 +08:00
|
|
|
)
|
2016-10-21 01:29:04 +08:00
|
|
|
from django.urls.resolvers import RegexPattern
|
2006-07-28 10:08:36 +08:00
|
|
|
|
2015-01-28 20:35:27 +08:00
|
|
|
from . import middleware, urlconf_outer, views
|
2015-12-23 05:10:52 +08:00
|
|
|
from .utils import URLObject
|
2013-11-02 04:15:41 +08:00
|
|
|
from .views import empty_view
|
2011-10-14 05:34:56 +08:00
|
|
|
|
2010-08-05 15:09:47 +08:00
|
|
|
resolve_test_data = (
|
2013-10-03 15:20:04 +08:00
|
|
|
# These entries are in the format: (path, url_name, app_name, namespace, view_name, func, args, kwargs)
|
2010-08-05 15:09:47 +08:00
|
|
|
# Simple case
|
2017-06-02 07:08:59 +08:00
|
|
|
('/normal/42/37/', 'normal-view', '', '', 'normal-view', views.empty_view, (), {'arg1': '42', 'arg2': '37'}),
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
2017-06-02 07:08:59 +08:00
|
|
|
'/view_class/42/37/', 'view-class', '', '', 'view-class', views.view_class_instance, (),
|
2015-09-12 07:33:12 +08:00
|
|
|
{'arg1': '42', 'arg2': '37'}
|
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/included/normal/42/37/', 'inc-normal-view', 'included_namespace_urls',
|
|
|
|
'included_namespace_urls', 'included_namespace_urls:inc-normal-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/included/view_class/42/37/', 'inc-view-class', 'included_namespace_urls',
|
|
|
|
'included_namespace_urls', 'included_namespace_urls:inc-view-class',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.view_class_instance, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
2010-08-05 15:09:47 +08:00
|
|
|
|
|
|
|
# Unnamed args are dropped if you have *any* kwargs in a pattern
|
2017-06-02 07:08:59 +08:00
|
|
|
('/mixed_args/42/37/', 'mixed-args', '', '', 'mixed-args', views.empty_view, (), {'arg2': '37'}),
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/included/mixed_args/42/37/', 'inc-mixed-args', 'included_namespace_urls',
|
|
|
|
'included_namespace_urls', 'included_namespace_urls:inc-mixed-args',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/included/12/mixed_args/42/37/', 'inc-mixed-args', 'included_namespace_urls',
|
|
|
|
'included_namespace_urls', 'included_namespace_urls:inc-mixed-args',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
2010-08-05 15:09:47 +08:00
|
|
|
|
2013-10-03 15:20:04 +08:00
|
|
|
# Unnamed views should have None as the url_name. Regression data for #21157.
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
2017-06-02 07:08:59 +08:00
|
|
|
'/unnamed/normal/42/37/', None, '', '', 'urlpatterns_reverse.views.empty_view', views.empty_view, (),
|
2015-09-12 07:33:12 +08:00
|
|
|
{'arg1': '42', 'arg2': '37'}
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'/unnamed/view_class/42/37/', None, '', '', 'urlpatterns_reverse.views.ViewClass', views.view_class_instance,
|
2017-06-02 07:08:59 +08:00
|
|
|
(), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
2010-08-06 21:47:56 +08:00
|
|
|
|
2010-08-05 15:09:47 +08:00
|
|
|
# If you have no kwargs, you get an args list.
|
2015-06-04 01:12:27 +08:00
|
|
|
('/no_kwargs/42/37/', 'no-kwargs', '', '', 'no-kwargs', views.empty_view, ('42', '37'), {}),
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/included/no_kwargs/42/37/', 'inc-no-kwargs', 'included_namespace_urls',
|
|
|
|
'included_namespace_urls', 'included_namespace_urls:inc-no-kwargs',
|
|
|
|
views.empty_view, ('42', '37'), {}
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'/included/12/no_kwargs/42/37/', 'inc-no-kwargs', 'included_namespace_urls',
|
|
|
|
'included_namespace_urls', 'included_namespace_urls:inc-no-kwargs',
|
|
|
|
views.empty_view, ('12', '42', '37'), {}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
2010-08-05 15:09:47 +08:00
|
|
|
|
|
|
|
# Namespaces
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
|
|
|
'/test1/inner/42/37/', 'urlobject-view', 'testapp', 'test-ns1', 'test-ns1:urlobject-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/included/test3/inner/42/37/', 'urlobject-view', 'included_namespace_urls:testapp',
|
|
|
|
'included_namespace_urls:test-ns3', 'included_namespace_urls:test-ns3:urlobject-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/ns-included1/normal/42/37/', 'inc-normal-view', 'included_namespace_urls',
|
|
|
|
'inc-ns1', 'inc-ns1:inc-normal-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/included/test3/inner/42/37/', 'urlobject-view', 'included_namespace_urls:testapp',
|
|
|
|
'included_namespace_urls:test-ns3', 'included_namespace_urls:test-ns3:urlobject-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/default/inner/42/37/', 'urlobject-view', 'testapp', 'testapp', 'testapp:urlobject-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
'/other2/inner/42/37/', 'urlobject-view', 'nodefault', 'other-ns2', 'other-ns2:urlobject-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
'/other1/inner/42/37/', 'urlobject-view', 'nodefault', 'other-ns1', 'other-ns1:urlobject-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
2010-08-05 15:09:47 +08:00
|
|
|
|
|
|
|
# Nested namespaces
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/ns-included1/test3/inner/42/37/', 'urlobject-view', 'included_namespace_urls:testapp',
|
|
|
|
'inc-ns1:test-ns3', 'inc-ns1:test-ns3:urlobject-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/ns-included1/ns-included4/ns-included2/test3/inner/42/37/', 'urlobject-view',
|
|
|
|
'included_namespace_urls:namespace_urls:included_namespace_urls:testapp',
|
|
|
|
'inc-ns1:inc-ns4:inc-ns2:test-ns3',
|
|
|
|
'inc-ns1:inc-ns4:inc-ns2:test-ns3:urlobject-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/app-included/test3/inner/42/37/', 'urlobject-view', 'included_namespace_urls:testapp', 'inc-app:test-ns3',
|
2017-06-02 07:08:59 +08:00
|
|
|
'inc-app:test-ns3:urlobject-view', views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/app-included/ns-included4/ns-included2/test3/inner/42/37/', 'urlobject-view',
|
|
|
|
'included_namespace_urls:namespace_urls:included_namespace_urls:testapp',
|
|
|
|
'inc-app:inc-ns4:inc-ns2:test-ns3',
|
|
|
|
'inc-app:inc-ns4:inc-ns2:test-ns3:urlobject-view',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'arg1': '42', 'arg2': '37'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
2011-12-23 07:03:48 +08:00
|
|
|
|
|
|
|
# Namespaces capturing variables
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
2017-01-10 22:57:49 +08:00
|
|
|
'/inc70/', 'inner-nothing', 'included_urls', 'inc-ns5', 'inc-ns5:inner-nothing',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'outer': '70'}
|
2017-01-10 22:57:49 +08:00
|
|
|
),
|
|
|
|
(
|
|
|
|
'/inc78/extra/foobar/', 'inner-extra', 'included_urls', 'inc-ns5', 'inc-ns5:inner-extra',
|
2017-06-02 07:08:59 +08:00
|
|
|
views.empty_view, (), {'outer': '78', 'extra': 'foobar'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
2010-08-05 15:09:47 +08:00
|
|
|
)
|
2009-11-16 09:58:00 +08:00
|
|
|
|
2006-07-28 10:08:36 +08:00
|
|
|
test_data = (
|
2008-08-31 19:11:20 +08:00
|
|
|
('places', '/places/3/', [3], {}),
|
|
|
|
('places', '/places/3/', ['3'], {}),
|
|
|
|
('places', NoReverseMatch, ['a'], {}),
|
|
|
|
('places', NoReverseMatch, [], {}),
|
|
|
|
('places?', '/place/', [], {}),
|
|
|
|
('places+', '/places/', [], {}),
|
|
|
|
('places*', '/place/', [], {}),
|
|
|
|
('places2?', '/', [], {}),
|
|
|
|
('places2+', '/places/', [], {}),
|
|
|
|
('places2*', '/', [], {}),
|
|
|
|
('places3', '/places/4/', [4], {}),
|
|
|
|
('places3', '/places/harlem/', ['harlem'], {}),
|
|
|
|
('places3', NoReverseMatch, ['harlem64'], {}),
|
|
|
|
('places4', '/places/3/', [], {'id': 3}),
|
|
|
|
('people', NoReverseMatch, [], {}),
|
|
|
|
('people', '/people/adrian/', ['adrian'], {}),
|
|
|
|
('people', '/people/adrian/', [], {'name': 'adrian'}),
|
|
|
|
('people', NoReverseMatch, ['name with spaces'], {}),
|
|
|
|
('people', NoReverseMatch, [], {'name': 'name with spaces'}),
|
|
|
|
('people2', '/people/name/', [], {}),
|
|
|
|
('people2a', '/people/name/fred/', ['fred'], {}),
|
2012-01-04 06:49:13 +08:00
|
|
|
('people_backref', '/people/nate-nate/', ['nate'], {}),
|
|
|
|
('people_backref', '/people/nate-nate/', [], {'name': 'nate'}),
|
2008-08-31 19:11:20 +08:00
|
|
|
('optional', '/optional/fred/', [], {'name': 'fred'}),
|
|
|
|
('optional', '/optional/fred/', ['fred'], {}),
|
2015-03-19 15:56:38 +08:00
|
|
|
('named_optional', '/optional/1/', [1], {}),
|
|
|
|
('named_optional', '/optional/1/', [], {'arg1': 1}),
|
|
|
|
('named_optional', '/optional/1/2/', [1, 2], {}),
|
|
|
|
('named_optional', '/optional/1/2/', [], {'arg1': 1, 'arg2': 2}),
|
2019-06-19 16:59:55 +08:00
|
|
|
('named_optional_terminated', '/optional/1/', [1], {}),
|
|
|
|
('named_optional_terminated', '/optional/1/', [], {'arg1': 1}),
|
2015-03-19 15:56:38 +08:00
|
|
|
('named_optional_terminated', '/optional/1/2/', [1, 2], {}),
|
|
|
|
('named_optional_terminated', '/optional/1/2/', [], {'arg1': 1, 'arg2': 2}),
|
2008-08-31 19:11:20 +08:00
|
|
|
('hardcoded', '/hardcoded/', [], {}),
|
|
|
|
('hardcoded2', '/hardcoded/doc.pdf', [], {}),
|
|
|
|
('people3', '/people/il/adrian/', [], {'state': 'il', 'name': 'adrian'}),
|
|
|
|
('people3', NoReverseMatch, [], {'state': 'il'}),
|
|
|
|
('people3', NoReverseMatch, [], {'name': 'adrian'}),
|
|
|
|
('people4', NoReverseMatch, [], {'state': 'il', 'name': 'adrian'}),
|
|
|
|
('people6', '/people/il/test/adrian/', ['il/test', 'adrian'], {}),
|
|
|
|
('people6', '/people//adrian/', ['adrian'], {}),
|
|
|
|
('range', '/character_set/a/', [], {}),
|
|
|
|
('range2', '/character_set/x/', [], {}),
|
|
|
|
('price', '/price/$10/', ['10'], {}),
|
|
|
|
('price2', '/price/$10/', ['10'], {}),
|
|
|
|
('price3', '/price/$10/', ['10'], {}),
|
|
|
|
('product', '/product/chocolate+($2.00)/', [], {'price': '2.00', 'product': 'chocolate'}),
|
2017-06-02 07:08:59 +08:00
|
|
|
('headlines', '/headlines/2007.5.21/', [], {'year': 2007, 'month': 5, 'day': 21}),
|
2015-09-12 07:33:12 +08:00
|
|
|
(
|
|
|
|
'windows', r'/windows_path/C:%5CDocuments%20and%20Settings%5Cspam/', [],
|
2017-06-02 07:08:59 +08:00
|
|
|
{'drive_name': 'C', 'path': r'Documents and Settings\spam'}
|
2015-09-12 07:33:12 +08:00
|
|
|
),
|
2014-06-26 22:55:36 +08:00
|
|
|
('special', r'/special_chars/~@+%5C$*%7C/', [r'~@+\$*|'], {}),
|
2013-03-19 04:52:16 +08:00
|
|
|
('special', r'/special_chars/some%20resource/', [r'some resource'], {}),
|
|
|
|
('special', r'/special_chars/10%25%20complete/', [r'10% complete'], {}),
|
|
|
|
('special', r'/special_chars/some%20resource/', [], {'chars': r'some resource'}),
|
|
|
|
('special', r'/special_chars/10%25%20complete/', [], {'chars': r'10% complete'}),
|
2008-08-31 19:11:20 +08:00
|
|
|
('special', NoReverseMatch, [''], {}),
|
|
|
|
('mixed', '/john/0/', [], {'name': 'john'}),
|
|
|
|
('repeats', '/repeats/a/', [], {}),
|
|
|
|
('repeats2', '/repeats/aa/', [], {}),
|
2008-09-02 07:03:03 +08:00
|
|
|
('repeats3', '/repeats/aa/', [], {}),
|
2008-08-31 19:11:20 +08:00
|
|
|
('test', '/test/1', [], {}),
|
|
|
|
('inner-nothing', '/outer/42/', [], {'outer': '42'}),
|
|
|
|
('inner-nothing', '/outer/42/', ['42'], {}),
|
|
|
|
('inner-nothing', NoReverseMatch, ['foo'], {}),
|
|
|
|
('inner-extra', '/outer/42/extra/inner/', [], {'extra': 'inner', 'outer': '42'}),
|
|
|
|
('inner-extra', '/outer/42/extra/inner/', ['42', 'inner'], {}),
|
|
|
|
('inner-extra', NoReverseMatch, ['fred', 'inner'], {}),
|
2014-02-27 23:32:20 +08:00
|
|
|
('inner-no-kwargs', '/outer-no-kwargs/42/inner-no-kwargs/1/', ['42', '1'], {}),
|
2008-08-31 19:11:20 +08:00
|
|
|
('disjunction', NoReverseMatch, ['foo'], {}),
|
|
|
|
('inner-disjunction', NoReverseMatch, ['10', '11'], {}),
|
2008-09-01 01:20:40 +08:00
|
|
|
('extra-places', '/e-places/10/', ['10'], {}),
|
|
|
|
('extra-people', '/e-people/fred/', ['fred'], {}),
|
|
|
|
('extra-people', '/e-people/fred/', [], {'name': 'fred'}),
|
2008-09-30 13:52:35 +08:00
|
|
|
('part', '/part/one/', [], {'value': 'one'}),
|
|
|
|
('part', '/prefix/xx/part/one/', [], {'value': 'one', 'prefix': 'xx'}),
|
|
|
|
('part2', '/part2/one/', [], {'value': 'one'}),
|
|
|
|
('part2', '/part2/', [], {}),
|
|
|
|
('part2', '/prefix/xx/part2/one/', [], {'value': 'one', 'prefix': 'xx'}),
|
|
|
|
('part2', '/prefix/xx/part2/', [], {'prefix': 'xx'}),
|
2008-09-27 14:14:11 +08:00
|
|
|
|
2015-03-19 15:56:38 +08:00
|
|
|
# Tests for nested groups. Nested capturing groups will only work if you
|
|
|
|
# *only* supply the correct outer group.
|
|
|
|
('nested-noncapture', '/nested/noncapture/opt', [], {'p': 'opt'}),
|
|
|
|
('nested-capture', '/nested/capture/opt/', ['opt/'], {}),
|
|
|
|
('nested-capture', NoReverseMatch, [], {'p': 'opt'}),
|
|
|
|
('nested-mixedcapture', '/nested/capture/mixed/opt', ['opt'], {}),
|
|
|
|
('nested-mixedcapture', NoReverseMatch, [], {'p': 'opt'}),
|
|
|
|
('nested-namedcapture', '/nested/capture/named/opt/', [], {'outer': 'opt/'}),
|
|
|
|
('nested-namedcapture', NoReverseMatch, [], {'outer': 'opt/', 'inner': 'opt'}),
|
|
|
|
('nested-namedcapture', NoReverseMatch, [], {'inner': 'opt'}),
|
|
|
|
|
2011-05-08 00:59:25 +08:00
|
|
|
('non_path_include', '/includes/non_path_include/', [], {}),
|
2008-09-27 14:14:11 +08:00
|
|
|
|
2011-05-08 00:59:25 +08:00
|
|
|
# Tests for #13154
|
|
|
|
('defaults', '/defaults_view1/3/', [], {'arg1': 3, 'arg2': 1}),
|
|
|
|
('defaults', '/defaults_view2/3/', [], {'arg1': 3, 'arg2': 2}),
|
|
|
|
('defaults', NoReverseMatch, [], {'arg1': 3, 'arg2': 3}),
|
|
|
|
('defaults', NoReverseMatch, [], {'arg2': 1}),
|
2014-07-18 03:59:28 +08:00
|
|
|
|
|
|
|
# Security tests
|
|
|
|
('security', '/%2Fexample.com/security/', ['/example.com'], {}),
|
2006-07-28 10:08:36 +08:00
|
|
|
)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.no_urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class NoURLPatternsTests(SimpleTestCase):
|
2010-09-27 23:16:46 +08:00
|
|
|
|
|
|
|
def test_no_urls_exception(self):
|
|
|
|
"""
|
2016-10-21 01:29:04 +08:00
|
|
|
URLResolver should raise an exception when no urlpatterns exist.
|
2010-09-27 23:16:46 +08:00
|
|
|
"""
|
2016-10-21 01:29:04 +08:00
|
|
|
resolver = URLResolver(RegexPattern(r'^$'), settings.ROOT_URLCONF)
|
2010-09-27 23:16:46 +08:00
|
|
|
|
2016-01-04 16:50:08 +08:00
|
|
|
with self.assertRaisesMessage(
|
2015-03-13 00:22:52 +08:00
|
|
|
ImproperlyConfigured,
|
2015-10-23 02:46:42 +08:00
|
|
|
"The included URLconf 'urlpatterns_reverse.no_urls' does not "
|
2021-05-27 03:06:47 +08:00
|
|
|
"appear to have any patterns in it. If you see the 'urlpatterns' "
|
|
|
|
"variable with valid patterns in the file then the issue is "
|
|
|
|
"probably caused by a circular import."
|
2016-01-04 16:50:08 +08:00
|
|
|
):
|
|
|
|
getattr(resolver, 'url_patterns')
|
2010-09-27 23:16:46 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class URLPatternReverse(SimpleTestCase):
|
2008-08-31 19:11:20 +08:00
|
|
|
|
2006-08-27 21:59:47 +08:00
|
|
|
def test_urlpattern_reverse(self):
|
2014-12-22 04:19:05 +08:00
|
|
|
for name, expected, args, kwargs in test_data:
|
2017-07-07 20:23:11 +08:00
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
try:
|
|
|
|
got = reverse(name, args=args, kwargs=kwargs)
|
|
|
|
except NoReverseMatch:
|
|
|
|
self.assertEqual(NoReverseMatch, expected)
|
|
|
|
else:
|
|
|
|
self.assertEqual(got, expected)
|
2006-07-28 10:08:36 +08:00
|
|
|
|
2010-08-06 22:34:10 +08:00
|
|
|
def test_reverse_none(self):
|
|
|
|
# Reversing None should raise an error, not return the last un-named view.
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
reverse(None)
|
2010-08-06 22:34:10 +08:00
|
|
|
|
2017-09-14 19:03:06 +08:00
|
|
|
def test_mixing_args_and_kwargs(self):
|
|
|
|
msg = "Don't mix *args and **kwargs in call to reverse()!"
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
|
|
|
reverse('name', args=['a'], kwargs={'b': 'c'})
|
|
|
|
|
2015-03-15 18:26:47 +08:00
|
|
|
@override_script_prefix('/{{invalid}}/')
|
2012-11-04 04:06:57 +08:00
|
|
|
def test_prefix_braces(self):
|
2015-03-13 00:22:52 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'/%7B%7Binvalid%7D%7D/includes/non_path_include/',
|
2015-03-15 18:26:47 +08:00
|
|
|
reverse('non_path_include')
|
2015-03-13 00:22:52 +08:00
|
|
|
)
|
2012-11-04 04:06:57 +08:00
|
|
|
|
|
|
|
def test_prefix_parenthesis(self):
|
2015-03-08 18:07:55 +08:00
|
|
|
# Parentheses are allowed and should not cause errors or be escaped
|
2015-03-15 18:26:47 +08:00
|
|
|
with override_script_prefix('/bogus)/'):
|
|
|
|
self.assertEqual(
|
|
|
|
'/bogus)/includes/non_path_include/',
|
|
|
|
reverse('non_path_include')
|
|
|
|
)
|
|
|
|
with override_script_prefix('/(bogus)/'):
|
|
|
|
self.assertEqual(
|
|
|
|
'/(bogus)/includes/non_path_include/',
|
|
|
|
reverse('non_path_include')
|
|
|
|
)
|
|
|
|
|
|
|
|
@override_script_prefix('/bump%20map/')
|
2012-11-04 04:06:57 +08:00
|
|
|
def test_prefix_format_char(self):
|
2015-03-13 00:22:52 +08:00
|
|
|
self.assertEqual(
|
|
|
|
'/bump%2520map/includes/non_path_include/',
|
2015-03-15 18:26:47 +08:00
|
|
|
reverse('non_path_include')
|
2015-03-13 00:22:52 +08:00
|
|
|
)
|
2012-11-04 04:06:57 +08:00
|
|
|
|
2015-03-15 18:26:47 +08:00
|
|
|
@override_script_prefix('/%7Eme/')
|
2013-03-14 01:19:29 +08:00
|
|
|
def test_non_urlsafe_prefix_with_args(self):
|
2015-03-08 18:07:55 +08:00
|
|
|
# Regression for #20022, adjusted for #24013 because ~ is an unreserved
|
|
|
|
# character. Tests whether % is escaped.
|
2015-03-15 18:26:47 +08:00
|
|
|
self.assertEqual('/%257Eme/places/1/', reverse('places', args=[1]))
|
2013-03-14 01:19:29 +08:00
|
|
|
|
2013-06-13 20:55:18 +08:00
|
|
|
def test_patterns_reported(self):
|
|
|
|
# Regression for #17076
|
2016-06-28 23:21:26 +08:00
|
|
|
with self.assertRaisesMessage(NoReverseMatch, r"1 pattern(s) tried: ['people/(?P<name>\\w+)/$']"):
|
2013-06-13 20:55:18 +08:00
|
|
|
# this url exists, but requires an argument
|
|
|
|
reverse("people", args=[])
|
|
|
|
|
2015-03-15 18:26:47 +08:00
|
|
|
@override_script_prefix('/script:name/')
|
2015-03-08 18:07:55 +08:00
|
|
|
def test_script_name_escaping(self):
|
|
|
|
self.assertEqual(
|
2015-03-15 18:26:47 +08:00
|
|
|
reverse('optional', args=['foo:bar']),
|
2015-03-08 18:07:55 +08:00
|
|
|
'/script:name/optional/foo:bar/'
|
|
|
|
)
|
|
|
|
|
2017-01-12 10:40:18 +08:00
|
|
|
def test_view_not_found_message(self):
|
|
|
|
msg = (
|
2017-02-03 09:43:21 +08:00
|
|
|
"Reverse for 'nonexistent-view' not found. 'nonexistent-view' "
|
2017-01-12 10:40:18 +08:00
|
|
|
"is not a valid view function or pattern name."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(NoReverseMatch, msg):
|
2017-02-03 09:43:21 +08:00
|
|
|
reverse('nonexistent-view')
|
2017-01-12 10:40:18 +08:00
|
|
|
|
|
|
|
def test_no_args_message(self):
|
|
|
|
msg = "Reverse for 'places' with no arguments not found. 1 pattern(s) tried:"
|
|
|
|
with self.assertRaisesMessage(NoReverseMatch, msg):
|
|
|
|
reverse('places')
|
|
|
|
|
|
|
|
def test_illegal_args_message(self):
|
|
|
|
msg = "Reverse for 'places' with arguments '(1, 2)' not found. 1 pattern(s) tried:"
|
|
|
|
with self.assertRaisesMessage(NoReverseMatch, msg):
|
|
|
|
reverse('places', args=(1, 2))
|
|
|
|
|
|
|
|
def test_illegal_kwargs_message(self):
|
|
|
|
msg = "Reverse for 'places' with keyword arguments '{'arg1': 2}' not found. 1 pattern(s) tried:"
|
|
|
|
with self.assertRaisesMessage(NoReverseMatch, msg):
|
2017-01-20 17:20:53 +08:00
|
|
|
reverse('places', kwargs={'arg1': 2})
|
2017-01-12 10:40:18 +08:00
|
|
|
|
2013-06-13 20:55:18 +08:00
|
|
|
|
2016-06-28 23:21:26 +08:00
|
|
|
class ResolverTests(SimpleTestCase):
|
2012-09-02 04:07:18 +08:00
|
|
|
def test_resolver_repr(self):
|
|
|
|
"""
|
2016-10-21 01:29:04 +08:00
|
|
|
Test repr of URLResolver, especially when urlconf_name is a list
|
2012-09-02 04:07:18 +08:00
|
|
|
(#17892).
|
|
|
|
"""
|
2015-10-23 02:46:42 +08:00
|
|
|
# Pick a resolver from a namespaced URLconf
|
2013-02-26 20:19:18 +08:00
|
|
|
resolver = get_resolver('urlpatterns_reverse.namespace_urls')
|
2012-09-02 04:07:18 +08:00
|
|
|
sub_resolver = resolver.namespace_dict['test-ns1'][1]
|
2016-10-21 01:29:04 +08:00
|
|
|
self.assertIn('<URLPattern list>', repr(sub_resolver))
|
2012-09-02 04:07:18 +08:00
|
|
|
|
2013-09-07 02:43:58 +08:00
|
|
|
def test_reverse_lazy_object_coercion_by_resolve(self):
|
|
|
|
"""
|
|
|
|
Verifies lazy object returned by reverse_lazy is coerced to
|
|
|
|
text by resolve(). Previous to #21043, this would raise a TypeError.
|
|
|
|
"""
|
|
|
|
urls = 'urlpatterns_reverse.named_urls'
|
|
|
|
proxy_url = reverse_lazy('named-url1', urlconf=urls)
|
|
|
|
resolver = get_resolver(urls)
|
2016-06-28 23:21:26 +08:00
|
|
|
resolver.resolve(proxy_url)
|
2013-09-07 02:43:58 +08:00
|
|
|
|
2016-06-21 01:24:51 +08:00
|
|
|
def test_resolver_reverse(self):
|
|
|
|
resolver = get_resolver('urlpatterns_reverse.named_urls')
|
2017-07-07 20:23:11 +08:00
|
|
|
test_urls = [
|
|
|
|
# (name, args, kwargs, expected)
|
|
|
|
('named-url1', (), {}, ''),
|
|
|
|
('named-url2', ('arg',), {}, 'extra/arg/'),
|
|
|
|
('named-url2', (), {'extra': 'arg'}, 'extra/arg/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(resolver.reverse(name, *args, **kwargs), expected)
|
2016-06-21 01:24:51 +08:00
|
|
|
|
2016-11-05 22:48:31 +08:00
|
|
|
def test_resolver_reverse_conflict(self):
|
|
|
|
"""
|
2018-12-08 06:52:28 +08:00
|
|
|
URL pattern name arguments don't need to be unique. The last registered
|
2016-11-05 22:48:31 +08:00
|
|
|
pattern takes precedence for conflicting names.
|
|
|
|
"""
|
|
|
|
resolver = get_resolver('urlpatterns_reverse.named_urls_conflict')
|
2017-07-07 20:23:11 +08:00
|
|
|
test_urls = [
|
|
|
|
# (name, args, kwargs, expected)
|
|
|
|
# Without arguments, the last URL in urlpatterns has precedence.
|
|
|
|
('name-conflict', (), {}, 'conflict/'),
|
|
|
|
# With an arg, the last URL in urlpatterns has precedence.
|
|
|
|
('name-conflict', ('arg',), {}, 'conflict-last/arg/'),
|
2018-12-08 06:52:28 +08:00
|
|
|
# With a kwarg, other URL patterns can be reversed.
|
2017-07-07 20:23:11 +08:00
|
|
|
('name-conflict', (), {'first': 'arg'}, 'conflict-first/arg/'),
|
|
|
|
('name-conflict', (), {'middle': 'arg'}, 'conflict-middle/arg/'),
|
|
|
|
('name-conflict', (), {'last': 'arg'}, 'conflict-last/arg/'),
|
|
|
|
# The number and order of the arguments don't interfere with reversing.
|
|
|
|
('name-conflict', ('arg', 'arg'), {}, 'conflict/arg/arg/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(resolver.reverse(name, *args, **kwargs), expected)
|
2016-11-05 22:48:31 +08:00
|
|
|
|
2009-06-29 22:02:17 +08:00
|
|
|
def test_non_regex(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
A Resolver404 is raised if resolving doesn't meet the basic
|
|
|
|
requirements of a path to match - i.e., at the very least, it matches
|
|
|
|
the root pattern '^/'. Never return None from resolve() to prevent a
|
2017-01-19 18:00:41 +08:00
|
|
|
TypeError from occurring later (#10834).
|
2009-06-29 22:02:17 +08:00
|
|
|
"""
|
2017-07-07 20:23:11 +08:00
|
|
|
test_urls = ['', 'a', '\\', '.']
|
2018-12-08 06:52:28 +08:00
|
|
|
for path_ in test_urls:
|
|
|
|
with self.subTest(path=path_):
|
2017-07-07 20:23:11 +08:00
|
|
|
with self.assertRaises(Resolver404):
|
2018-12-08 06:52:28 +08:00
|
|
|
resolve(path_)
|
2010-09-27 23:16:46 +08:00
|
|
|
|
2010-09-13 03:27:33 +08:00
|
|
|
def test_404_tried_urls_have_names(self):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
The list of URLs that come back from a Resolver404 exception contains
|
|
|
|
a list in the right format for printing out in the DEBUG 404 page with
|
|
|
|
both the patterns and URL names, if available.
|
2010-09-13 03:27:33 +08:00
|
|
|
"""
|
2013-02-26 20:19:18 +08:00
|
|
|
urls = 'urlpatterns_reverse.named_urls'
|
2010-09-13 03:27:33 +08:00
|
|
|
# this list matches the expected URL types and names returned when
|
2017-02-03 09:43:21 +08:00
|
|
|
# you try to resolve a nonexistent URL in the first level of included
|
|
|
|
# URLs in named_urls.py (e.g., '/included/nonexistent-url')
|
2010-09-13 03:27:33 +08:00
|
|
|
url_types_names = [
|
2016-10-21 01:29:04 +08:00
|
|
|
[{'type': URLPattern, 'name': 'named-url1'}],
|
|
|
|
[{'type': URLPattern, 'name': 'named-url2'}],
|
|
|
|
[{'type': URLPattern, 'name': None}],
|
|
|
|
[{'type': URLResolver}, {'type': URLPattern, 'name': 'named-url3'}],
|
|
|
|
[{'type': URLResolver}, {'type': URLPattern, 'name': 'named-url4'}],
|
|
|
|
[{'type': URLResolver}, {'type': URLPattern, 'name': None}],
|
|
|
|
[{'type': URLResolver}, {'type': URLResolver}],
|
2010-09-13 03:27:33 +08:00
|
|
|
]
|
2016-12-01 18:38:01 +08:00
|
|
|
with self.assertRaisesMessage(Resolver404, 'tried') as cm:
|
2017-02-03 09:43:21 +08:00
|
|
|
resolve('/included/nonexistent-url', urlconf=urls)
|
2016-06-28 23:21:26 +08:00
|
|
|
e = cm.exception
|
|
|
|
# make sure we at least matched the root ('/') url resolver:
|
|
|
|
self.assertIn('tried', e.args[0])
|
|
|
|
self.assertEqual(
|
|
|
|
len(e.args[0]['tried']),
|
|
|
|
len(url_types_names),
|
|
|
|
'Wrong number of tried URLs returned. Expected %s, got %s.' % (
|
|
|
|
len(url_types_names), len(e.args[0]['tried'])
|
2015-09-12 07:33:12 +08:00
|
|
|
)
|
2016-06-28 23:21:26 +08:00
|
|
|
)
|
|
|
|
for tried, expected in zip(e.args[0]['tried'], url_types_names):
|
|
|
|
for t, e in zip(tried, expected):
|
2017-07-07 20:23:11 +08:00
|
|
|
with self.subTest(t):
|
|
|
|
self.assertIsInstance(t, e['type']), '%s is not an instance of %s' % (t, e['type'])
|
|
|
|
if 'name' in e:
|
|
|
|
if not e['name']:
|
|
|
|
self.assertIsNone(t.name, 'Expected no URL name but found %s.' % t.name)
|
|
|
|
else:
|
|
|
|
self.assertEqual(
|
|
|
|
t.name,
|
|
|
|
e['name'],
|
|
|
|
'Wrong URL name. Expected "%s", got "%s".' % (e['name'], t.name)
|
|
|
|
)
|
2009-06-29 22:02:17 +08:00
|
|
|
|
2016-06-18 22:39:32 +08:00
|
|
|
def test_namespaced_view_detail(self):
|
|
|
|
resolver = get_resolver('urlpatterns_reverse.nested_urls')
|
|
|
|
self.assertTrue(resolver._is_callback('urlpatterns_reverse.nested_urls.view1'))
|
|
|
|
self.assertTrue(resolver._is_callback('urlpatterns_reverse.nested_urls.view2'))
|
|
|
|
self.assertTrue(resolver._is_callback('urlpatterns_reverse.nested_urls.View3'))
|
|
|
|
self.assertFalse(resolver._is_callback('urlpatterns_reverse.nested_urls.blub'))
|
|
|
|
|
2016-08-13 02:07:24 +08:00
|
|
|
def test_view_detail_as_method(self):
|
|
|
|
# Views which have a class name as part of their path.
|
|
|
|
resolver = get_resolver('urlpatterns_reverse.method_view_urls')
|
|
|
|
self.assertTrue(resolver._is_callback('urlpatterns_reverse.method_view_urls.ViewContainer.method_view'))
|
|
|
|
self.assertTrue(resolver._is_callback('urlpatterns_reverse.method_view_urls.ViewContainer.classmethod_view'))
|
|
|
|
|
2016-07-15 02:41:52 +08:00
|
|
|
def test_populate_concurrency(self):
|
|
|
|
"""
|
2016-10-21 01:29:04 +08:00
|
|
|
URLResolver._populate() can be called concurrently, but not more
|
2016-07-15 02:41:52 +08:00
|
|
|
than once per thread (#26888).
|
|
|
|
"""
|
2016-10-21 01:29:04 +08:00
|
|
|
resolver = URLResolver(RegexPattern(r'^/'), 'urlpatterns_reverse.urls')
|
2016-07-15 02:41:52 +08:00
|
|
|
resolver._local.populating = True
|
|
|
|
thread = threading.Thread(target=resolver._populate)
|
|
|
|
thread.start()
|
|
|
|
thread.join()
|
|
|
|
self.assertNotEqual(resolver._reverse_dict, {})
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.reverse_lazy_urls')
|
2011-04-29 19:49:59 +08:00
|
|
|
class ReverseLazyTest(TestCase):
|
|
|
|
|
|
|
|
def test_redirect_with_lazy_reverse(self):
|
2014-11-27 11:44:56 +08:00
|
|
|
response = self.client.get('/redirect/')
|
2015-01-19 05:43:57 +08:00
|
|
|
self.assertRedirects(response, "/redirected_to/", status_code=302)
|
2011-04-29 19:49:59 +08:00
|
|
|
|
|
|
|
def test_user_permission_with_lazy_reverse(self):
|
2016-02-06 04:56:52 +08:00
|
|
|
alfred = User.objects.create_user('alfred', 'alfred@example.com', password='testpw')
|
2011-04-29 19:49:59 +08:00
|
|
|
response = self.client.get('/login_required_view/')
|
|
|
|
self.assertRedirects(response, "/login/?next=/login_required_view/", status_code=302)
|
2016-02-06 04:56:52 +08:00
|
|
|
self.client.force_login(alfred)
|
2011-04-29 19:49:59 +08:00
|
|
|
response = self.client.get('/login_required_view/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
|
2015-01-31 23:05:16 +08:00
|
|
|
def test_inserting_reverse_lazy_into_string(self):
|
|
|
|
self.assertEqual(
|
|
|
|
'Some URL: %s' % reverse_lazy('some-login-page'),
|
|
|
|
'Some URL: /login/'
|
|
|
|
)
|
|
|
|
|
2019-12-23 18:34:36 +08:00
|
|
|
def test_build_absolute_uri(self):
|
|
|
|
factory = RequestFactory()
|
|
|
|
request = factory.get('/')
|
|
|
|
self.assertEqual(
|
|
|
|
request.build_absolute_uri(reverse_lazy('some-login-page')),
|
|
|
|
'http://testserver/login/',
|
|
|
|
)
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-02-18 17:19:04 +08:00
|
|
|
class ReverseLazySettingsTest(AdminScriptTestCase):
|
|
|
|
"""
|
2016-10-27 15:53:39 +08:00
|
|
|
reverse_lazy can be used in settings without causing a circular
|
2014-02-18 17:19:04 +08:00
|
|
|
import error.
|
|
|
|
"""
|
|
|
|
def setUp(self):
|
2019-02-06 01:11:54 +08:00
|
|
|
super().setUp()
|
2018-02-01 16:29:46 +08:00
|
|
|
self.write_settings(
|
|
|
|
'settings.py',
|
|
|
|
extra="from django.urls import reverse_lazy\nLOGIN_URL = reverse_lazy('login')",
|
|
|
|
)
|
2014-02-18 17:19:04 +08:00
|
|
|
|
|
|
|
def test_lazy_in_settings(self):
|
2015-01-18 08:00:12 +08:00
|
|
|
out, err = self.run_manage(['check'])
|
2014-02-18 20:20:02 +08:00
|
|
|
self.assertNoOutput(err)
|
2014-02-18 17:19:04 +08:00
|
|
|
|
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class ReverseShortcutTests(SimpleTestCase):
|
2009-04-04 14:54:56 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
def test_redirect_to_object(self):
|
|
|
|
# We don't really need a model; just something with a get_absolute_url
|
2017-01-19 15:39:46 +08:00
|
|
|
class FakeObj:
|
2009-03-21 21:09:13 +08:00
|
|
|
def get_absolute_url(self):
|
|
|
|
return "/hi-there/"
|
2009-04-04 14:54:56 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
res = redirect(FakeObj())
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(res, HttpResponseRedirect)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(res.url, '/hi-there/')
|
2009-04-04 14:54:56 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
res = redirect(FakeObj(), permanent=True)
|
2013-05-21 17:42:15 +08:00
|
|
|
self.assertIsInstance(res, HttpResponsePermanentRedirect)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(res.url, '/hi-there/')
|
2009-04-04 14:54:56 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
def test_redirect_to_view_name(self):
|
|
|
|
res = redirect('hardcoded2')
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(res.url, '/hardcoded/doc.pdf')
|
2009-03-21 21:09:13 +08:00
|
|
|
res = redirect('places', 1)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(res.url, '/places/1/')
|
2009-03-21 21:09:13 +08:00
|
|
|
res = redirect('headlines', year='2008', month='02', day='17')
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(res.url, '/headlines/2008.02.17/')
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
redirect('not-a-view')
|
2009-04-04 14:54:56 +08:00
|
|
|
|
2009-03-21 21:09:13 +08:00
|
|
|
def test_redirect_to_url(self):
|
|
|
|
res = redirect('/foo/')
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(res.url, '/foo/')
|
2009-03-21 21:09:13 +08:00
|
|
|
res = redirect('http://example.com/')
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(res.url, 'http://example.com/')
|
2014-04-21 01:12:43 +08:00
|
|
|
# Assert that we can redirect using UTF-8 strings
|
|
|
|
res = redirect('/æøå/abc/')
|
|
|
|
self.assertEqual(res.url, '/%C3%A6%C3%B8%C3%A5/abc/')
|
|
|
|
# Assert that no imports are attempted when dealing with a relative path
|
|
|
|
# (previously, the below would resolve in a UnicodeEncodeError from __import__ )
|
|
|
|
res = redirect('/æøå.abc/')
|
|
|
|
self.assertEqual(res.url, '/%C3%A6%C3%B8%C3%A5.abc/')
|
|
|
|
res = redirect('os.path')
|
|
|
|
self.assertEqual(res.url, 'os.path')
|
|
|
|
|
|
|
|
def test_no_illegal_imports(self):
|
|
|
|
# modules that are not listed in urlpatterns should not be importable
|
|
|
|
redirect("urlpatterns_reverse.nonimported_module.view")
|
|
|
|
self.assertNotIn("urlpatterns_reverse.nonimported_module", sys.modules)
|
|
|
|
|
|
|
|
def test_reverse_by_path_nested(self):
|
2015-08-18 01:45:07 +08:00
|
|
|
# Views added to urlpatterns using include() should be reversible.
|
|
|
|
from .views import nested_view
|
|
|
|
self.assertEqual(reverse(nested_view), '/includes/nested_path/')
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2010-01-11 02:44:39 +08:00
|
|
|
def test_redirect_view_object(self):
|
2011-10-14 05:34:56 +08:00
|
|
|
from .views import absolute_kwargs_view
|
2010-01-11 02:44:39 +08:00
|
|
|
res = redirect(absolute_kwargs_view)
|
2013-02-13 16:55:43 +08:00
|
|
|
self.assertEqual(res.url, '/absolute_arg_view/')
|
2016-01-17 19:26:39 +08:00
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
redirect(absolute_kwargs_view, wrong_argument=None)
|
2010-01-11 02:44:39 +08:00
|
|
|
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.namespace_urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class NamespaceTests(SimpleTestCase):
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
def test_ambiguous_object(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""
|
|
|
|
Names deployed via dynamic URL objects that require namespaces can't
|
|
|
|
be resolved.
|
|
|
|
"""
|
|
|
|
test_urls = [
|
|
|
|
('urlobject-view', [], {}),
|
|
|
|
('urlobject-view', [37, 42], {}),
|
|
|
|
('urlobject-view', [], {'arg1': 42, 'arg2': 37}),
|
|
|
|
]
|
|
|
|
for name, args, kwargs in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
reverse(name, args=args, kwargs=kwargs)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
def test_ambiguous_urlpattern(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""
|
|
|
|
Names deployed via dynamic URL objects that require namespaces can't
|
|
|
|
be resolved.
|
|
|
|
"""
|
|
|
|
test_urls = [
|
|
|
|
('inner-nothing', [], {}),
|
|
|
|
('inner-nothing', [37, 42], {}),
|
|
|
|
('inner-nothing', [], {'arg1': 42, 'arg2': 37}),
|
|
|
|
]
|
|
|
|
for name, args, kwargs in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
reverse(name, args=args, kwargs=kwargs)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
def test_non_existent_namespace(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""Nonexistent namespaces raise errors."""
|
|
|
|
test_urls = [
|
|
|
|
'blahblah:urlobject-view',
|
|
|
|
'test-ns1:blahblah:urlobject-view',
|
|
|
|
]
|
|
|
|
for name in test_urls:
|
|
|
|
with self.subTest(name=name):
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
reverse(name)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
def test_normal_name(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""Normal lookups work as expected."""
|
|
|
|
test_urls = [
|
|
|
|
('normal-view', [], {}, '/normal/'),
|
|
|
|
('normal-view', [37, 42], {}, '/normal/37/42/'),
|
|
|
|
('normal-view', [], {'arg1': 42, 'arg2': 37}, '/normal/42/37/'),
|
|
|
|
('special-view', [], {}, '/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
def test_simple_included_name(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""Normal lookups work on names included from other patterns."""
|
|
|
|
test_urls = [
|
|
|
|
('included_namespace_urls:inc-normal-view', [], {}, '/included/normal/'),
|
|
|
|
('included_namespace_urls:inc-normal-view', [37, 42], {}, '/included/normal/37/42/'),
|
|
|
|
('included_namespace_urls:inc-normal-view', [], {'arg1': 42, 'arg2': 37}, '/included/normal/42/37/'),
|
|
|
|
('included_namespace_urls:inc-special-view', [], {}, '/included/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
def test_namespace_object(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""Dynamic URL objects can be found using a namespace."""
|
|
|
|
test_urls = [
|
|
|
|
('test-ns1:urlobject-view', [], {}, '/test1/inner/'),
|
|
|
|
('test-ns1:urlobject-view', [37, 42], {}, '/test1/inner/37/42/'),
|
|
|
|
('test-ns1:urlobject-view', [], {'arg1': 42, 'arg2': 37}, '/test1/inner/42/37/'),
|
|
|
|
('test-ns1:urlobject-special-view', [], {}, '/test1/inner/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
def test_app_object(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""
|
|
|
|
Dynamic URL objects can return a (pattern, app_name) 2-tuple, and
|
|
|
|
include() can set the namespace.
|
|
|
|
"""
|
|
|
|
test_urls = [
|
|
|
|
('new-ns1:urlobject-view', [], {}, '/newapp1/inner/'),
|
|
|
|
('new-ns1:urlobject-view', [37, 42], {}, '/newapp1/inner/37/42/'),
|
|
|
|
('new-ns1:urlobject-view', [], {'arg1': 42, 'arg2': 37}, '/newapp1/inner/42/37/'),
|
|
|
|
('new-ns1:urlobject-special-view', [], {}, '/newapp1/inner/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2015-05-28 23:25:52 +08:00
|
|
|
|
|
|
|
def test_app_object_default_namespace(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""
|
|
|
|
Namespace defaults to app_name when including a (pattern, app_name)
|
|
|
|
2-tuple.
|
|
|
|
"""
|
|
|
|
test_urls = [
|
|
|
|
('newapp:urlobject-view', [], {}, '/new-default/inner/'),
|
|
|
|
('newapp:urlobject-view', [37, 42], {}, '/new-default/inner/37/42/'),
|
|
|
|
('newapp:urlobject-view', [], {'arg1': 42, 'arg2': 37}, '/new-default/inner/42/37/'),
|
|
|
|
('newapp:urlobject-special-view', [], {}, '/new-default/inner/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2015-05-28 23:25:52 +08:00
|
|
|
|
2009-07-17 00:16:13 +08:00
|
|
|
def test_embedded_namespace_object(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""Namespaces can be installed anywhere in the URL pattern tree."""
|
|
|
|
test_urls = [
|
|
|
|
('included_namespace_urls:test-ns3:urlobject-view', [], {}, '/included/test3/inner/'),
|
|
|
|
('included_namespace_urls:test-ns3:urlobject-view', [37, 42], {}, '/included/test3/inner/37/42/'),
|
|
|
|
(
|
|
|
|
'included_namespace_urls:test-ns3:urlobject-view', [], {'arg1': 42, 'arg2': 37},
|
|
|
|
'/included/test3/inner/42/37/',
|
|
|
|
),
|
|
|
|
('included_namespace_urls:test-ns3:urlobject-special-view', [], {}, '/included/test3/inner/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
def test_namespace_pattern(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""Namespaces can be applied to include()'d urlpatterns."""
|
|
|
|
test_urls = [
|
|
|
|
('inc-ns1:inc-normal-view', [], {}, '/ns-included1/normal/'),
|
|
|
|
('inc-ns1:inc-normal-view', [37, 42], {}, '/ns-included1/normal/37/42/'),
|
|
|
|
('inc-ns1:inc-normal-view', [], {'arg1': 42, 'arg2': 37}, '/ns-included1/normal/42/37/'),
|
|
|
|
('inc-ns1:inc-special-view', [], {}, '/ns-included1/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
def test_app_name_pattern(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""
|
|
|
|
Namespaces can be applied to include()'d urlpatterns that set an
|
|
|
|
app_name attribute.
|
|
|
|
"""
|
|
|
|
test_urls = [
|
|
|
|
('app-ns1:inc-normal-view', [], {}, '/app-included1/normal/'),
|
|
|
|
('app-ns1:inc-normal-view', [37, 42], {}, '/app-included1/normal/37/42/'),
|
|
|
|
('app-ns1:inc-normal-view', [], {'arg1': 42, 'arg2': 37}, '/app-included1/normal/42/37/'),
|
|
|
|
('app-ns1:inc-special-view', [], {}, '/app-included1/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2015-05-28 23:25:52 +08:00
|
|
|
|
2011-08-12 22:15:50 +08:00
|
|
|
def test_namespace_pattern_with_variable_prefix(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""
|
|
|
|
Using include() with namespaces when there is a regex variable in front
|
|
|
|
of it.
|
|
|
|
"""
|
|
|
|
test_urls = [
|
|
|
|
('inc-outer:inc-normal-view', [], {'outer': 42}, '/ns-outer/42/normal/'),
|
|
|
|
('inc-outer:inc-normal-view', [42], {}, '/ns-outer/42/normal/'),
|
|
|
|
('inc-outer:inc-normal-view', [], {'arg1': 37, 'arg2': 4, 'outer': 42}, '/ns-outer/42/normal/37/4/'),
|
|
|
|
('inc-outer:inc-normal-view', [42, 37, 4], {}, '/ns-outer/42/normal/37/4/'),
|
|
|
|
('inc-outer:inc-special-view', [], {'outer': 42}, '/ns-outer/42/+%5C$*/'),
|
|
|
|
('inc-outer:inc-special-view', [42], {}, '/ns-outer/42/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2011-08-12 22:15:50 +08:00
|
|
|
|
2009-07-17 00:16:13 +08:00
|
|
|
def test_multiple_namespace_pattern(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""Namespaces can be embedded."""
|
|
|
|
test_urls = [
|
|
|
|
('inc-ns1:test-ns3:urlobject-view', [], {}, '/ns-included1/test3/inner/'),
|
|
|
|
('inc-ns1:test-ns3:urlobject-view', [37, 42], {}, '/ns-included1/test3/inner/37/42/'),
|
|
|
|
(
|
|
|
|
'inc-ns1:test-ns3:urlobject-view', [], {'arg1': 42, 'arg2': 37},
|
|
|
|
'/ns-included1/test3/inner/42/37/',
|
|
|
|
),
|
|
|
|
('inc-ns1:test-ns3:urlobject-special-view', [], {}, '/ns-included1/test3/inner/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2010-08-05 15:09:47 +08:00
|
|
|
def test_nested_namespace_pattern(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""Namespaces can be nested."""
|
|
|
|
test_urls = [
|
|
|
|
(
|
|
|
|
'inc-ns1:inc-ns4:inc-ns1:test-ns3:urlobject-view', [], {},
|
|
|
|
'/ns-included1/ns-included4/ns-included1/test3/inner/',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'inc-ns1:inc-ns4:inc-ns1:test-ns3:urlobject-view', [37, 42], {},
|
|
|
|
'/ns-included1/ns-included4/ns-included1/test3/inner/37/42/',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'inc-ns1:inc-ns4:inc-ns1:test-ns3:urlobject-view', [], {'arg1': 42, 'arg2': 37},
|
|
|
|
'/ns-included1/ns-included4/ns-included1/test3/inner/42/37/',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'inc-ns1:inc-ns4:inc-ns1:test-ns3:urlobject-special-view', [], {},
|
|
|
|
'/ns-included1/ns-included4/ns-included1/test3/inner/+%5C$*/',
|
|
|
|
),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2010-08-05 15:09:47 +08:00
|
|
|
|
2009-07-17 00:16:13 +08:00
|
|
|
def test_app_lookup_object(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""A default application namespace can be used for lookup."""
|
|
|
|
test_urls = [
|
|
|
|
('testapp:urlobject-view', [], {}, '/default/inner/'),
|
|
|
|
('testapp:urlobject-view', [37, 42], {}, '/default/inner/37/42/'),
|
|
|
|
('testapp:urlobject-view', [], {'arg1': 42, 'arg2': 37}, '/default/inner/42/37/'),
|
|
|
|
('testapp:urlobject-special-view', [], {}, '/default/inner/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
def test_app_lookup_object_with_default(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""A default application namespace is sensitive to the current app."""
|
|
|
|
test_urls = [
|
|
|
|
('testapp:urlobject-view', [], {}, 'test-ns3', '/default/inner/'),
|
|
|
|
('testapp:urlobject-view', [37, 42], {}, 'test-ns3', '/default/inner/37/42/'),
|
|
|
|
('testapp:urlobject-view', [], {'arg1': 42, 'arg2': 37}, 'test-ns3', '/default/inner/42/37/'),
|
|
|
|
('testapp:urlobject-special-view', [], {}, 'test-ns3', '/default/inner/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, current_app, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs, current_app=current_app):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs, current_app=current_app), expected)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
|
|
|
def test_app_lookup_object_without_default(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""
|
|
|
|
An application namespace without a default is sensitive to the current
|
|
|
|
app.
|
|
|
|
"""
|
|
|
|
test_urls = [
|
|
|
|
('nodefault:urlobject-view', [], {}, None, '/other2/inner/'),
|
|
|
|
('nodefault:urlobject-view', [37, 42], {}, None, '/other2/inner/37/42/'),
|
|
|
|
('nodefault:urlobject-view', [], {'arg1': 42, 'arg2': 37}, None, '/other2/inner/42/37/'),
|
|
|
|
('nodefault:urlobject-special-view', [], {}, None, '/other2/inner/+%5C$*/'),
|
|
|
|
('nodefault:urlobject-view', [], {}, 'other-ns1', '/other1/inner/'),
|
|
|
|
('nodefault:urlobject-view', [37, 42], {}, 'other-ns1', '/other1/inner/37/42/'),
|
|
|
|
('nodefault:urlobject-view', [], {'arg1': 42, 'arg2': 37}, 'other-ns1', '/other1/inner/42/37/'),
|
|
|
|
('nodefault:urlobject-special-view', [], {}, 'other-ns1', '/other1/inner/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, current_app, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs, current_app=current_app):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs, current_app=current_app), expected)
|
2011-11-07 09:09:13 +08:00
|
|
|
|
|
|
|
def test_special_chars_namespace(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
test_urls = [
|
|
|
|
('special:included_namespace_urls:inc-normal-view', [], {}, '/+%5C$*/included/normal/'),
|
|
|
|
('special:included_namespace_urls:inc-normal-view', [37, 42], {}, '/+%5C$*/included/normal/37/42/'),
|
|
|
|
(
|
|
|
|
'special:included_namespace_urls:inc-normal-view', [], {'arg1': 42, 'arg2': 37},
|
|
|
|
'/+%5C$*/included/normal/42/37/',
|
|
|
|
),
|
|
|
|
('special:included_namespace_urls:inc-special-view', [], {}, '/+%5C$*/included/+%5C$*/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2009-07-17 00:16:13 +08:00
|
|
|
|
2011-12-23 07:03:48 +08:00
|
|
|
def test_namespaces_with_variables(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""Namespace prefixes can capture variables."""
|
|
|
|
test_urls = [
|
|
|
|
('inc-ns5:inner-nothing', [], {'outer': '70'}, '/inc70/'),
|
|
|
|
('inc-ns5:inner-extra', [], {'extra': 'foobar', 'outer': '78'}, '/inc78/extra/foobar/'),
|
|
|
|
('inc-ns5:inner-nothing', ['70'], {}, '/inc70/'),
|
|
|
|
('inc-ns5:inner-extra', ['78', 'foobar'], {}, '/inc78/extra/foobar/'),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs), expected)
|
2011-12-23 07:03:48 +08:00
|
|
|
|
2015-06-03 23:11:08 +08:00
|
|
|
def test_nested_app_lookup(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""
|
|
|
|
A nested current_app should be split in individual namespaces (#24904).
|
|
|
|
"""
|
|
|
|
test_urls = [
|
|
|
|
('inc-ns1:testapp:urlobject-view', [], {}, None, '/ns-included1/test4/inner/'),
|
|
|
|
('inc-ns1:testapp:urlobject-view', [37, 42], {}, None, '/ns-included1/test4/inner/37/42/'),
|
|
|
|
('inc-ns1:testapp:urlobject-view', [], {'arg1': 42, 'arg2': 37}, None, '/ns-included1/test4/inner/42/37/'),
|
|
|
|
('inc-ns1:testapp:urlobject-special-view', [], {}, None, '/ns-included1/test4/inner/+%5C$*/'),
|
|
|
|
('inc-ns1:testapp:urlobject-view', [], {}, 'inc-ns1:test-ns3', '/ns-included1/test3/inner/'),
|
|
|
|
('inc-ns1:testapp:urlobject-view', [37, 42], {}, 'inc-ns1:test-ns3', '/ns-included1/test3/inner/37/42/'),
|
|
|
|
(
|
|
|
|
'inc-ns1:testapp:urlobject-view', [], {'arg1': 42, 'arg2': 37}, 'inc-ns1:test-ns3',
|
|
|
|
'/ns-included1/test3/inner/42/37/',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'inc-ns1:testapp:urlobject-special-view', [], {}, 'inc-ns1:test-ns3',
|
|
|
|
'/ns-included1/test3/inner/+%5C$*/',
|
|
|
|
),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, current_app, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs, current_app=current_app):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs, current_app=current_app), expected)
|
2015-06-03 23:11:08 +08:00
|
|
|
|
|
|
|
def test_current_app_no_partial_match(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
"""current_app shouldn't be used unless it matches the whole path."""
|
|
|
|
test_urls = [
|
|
|
|
('inc-ns1:testapp:urlobject-view', [], {}, 'nonexistent:test-ns3', '/ns-included1/test4/inner/'),
|
|
|
|
(
|
|
|
|
'inc-ns1:testapp:urlobject-view', [37, 42], {}, 'nonexistent:test-ns3',
|
|
|
|
'/ns-included1/test4/inner/37/42/',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'inc-ns1:testapp:urlobject-view', [], {'arg1': 42, 'arg2': 37}, 'nonexistent:test-ns3',
|
|
|
|
'/ns-included1/test4/inner/42/37/',
|
|
|
|
),
|
|
|
|
(
|
|
|
|
'inc-ns1:testapp:urlobject-special-view', [], {}, 'nonexistent:test-ns3',
|
|
|
|
'/ns-included1/test4/inner/+%5C$*/',
|
|
|
|
),
|
|
|
|
]
|
|
|
|
for name, args, kwargs, current_app, expected in test_urls:
|
|
|
|
with self.subTest(name=name, args=args, kwargs=kwargs, current_app=current_app):
|
|
|
|
self.assertEqual(reverse(name, args=args, kwargs=kwargs, current_app=current_app), expected)
|
2015-06-03 23:11:08 +08:00
|
|
|
|
2009-11-16 09:58:00 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
@override_settings(ROOT_URLCONF=urlconf_outer.__name__)
|
2015-04-18 05:38:20 +08:00
|
|
|
class RequestURLconfTests(SimpleTestCase):
|
2009-11-16 09:58:00 +08:00
|
|
|
def test_urlconf(self):
|
|
|
|
response = self.client.get('/test/me/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2015-03-13 00:22:52 +08:00
|
|
|
self.assertEqual(response.content, b'outer:/test/me/,inner:/inner_urlconf/second_test/')
|
2009-11-16 09:58:00 +08:00
|
|
|
response = self.client.get('/inner_urlconf/second_test/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
response = self.client.get('/second_test/')
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2009-11-16 09:58:00 +08:00
|
|
|
'%s.ChangeURLconfMiddleware' % middleware.__name__,
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2013-10-14 21:14:17 +08:00
|
|
|
)
|
|
|
|
def test_urlconf_overridden(self):
|
2009-11-16 09:58:00 +08:00
|
|
|
response = self.client.get('/test/me/')
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
response = self.client.get('/inner_urlconf/second_test/')
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
response = self.client.get('/second_test/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2012-08-14 17:57:18 +08:00
|
|
|
self.assertEqual(response.content, b'outer:,inner:/second_test/')
|
2009-12-14 04:29:04 +08:00
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2010-03-26 23:08:24 +08:00
|
|
|
'%s.NullChangeURLconfMiddleware' % middleware.__name__,
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2013-10-14 21:14:17 +08:00
|
|
|
)
|
|
|
|
def test_urlconf_overridden_with_null(self):
|
2015-06-20 19:05:35 +08:00
|
|
|
"""
|
|
|
|
Overriding request.urlconf with None will fall back to the default
|
|
|
|
URLconf.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/test/me/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'outer:/test/me/,inner:/inner_urlconf/second_test/')
|
|
|
|
response = self.client.get('/inner_urlconf/second_test/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
response = self.client.get('/second_test/')
|
|
|
|
self.assertEqual(response.status_code, 404)
|
2010-03-26 23:08:24 +08:00
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2013-10-14 21:14:17 +08:00
|
|
|
'%s.ChangeURLconfMiddleware' % middleware.__name__,
|
|
|
|
'%s.ReverseInnerInResponseMiddleware' % middleware.__name__,
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2013-10-14 21:14:17 +08:00
|
|
|
)
|
2013-03-07 07:33:51 +08:00
|
|
|
def test_reverse_inner_in_response_middleware(self):
|
|
|
|
"""
|
|
|
|
Test reversing an URL from the *overridden* URLconf from inside
|
|
|
|
a response middleware.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/second_test/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, b'/second_test/')
|
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2013-10-14 21:14:17 +08:00
|
|
|
'%s.ChangeURLconfMiddleware' % middleware.__name__,
|
|
|
|
'%s.ReverseOuterInResponseMiddleware' % middleware.__name__,
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2013-10-14 21:14:17 +08:00
|
|
|
)
|
2013-03-07 07:33:51 +08:00
|
|
|
def test_reverse_outer_in_response_middleware(self):
|
|
|
|
"""
|
|
|
|
Test reversing an URL from the *default* URLconf from inside
|
|
|
|
a response middleware.
|
|
|
|
"""
|
2019-03-16 07:27:57 +08:00
|
|
|
msg = (
|
|
|
|
"Reverse for 'outer' not found. 'outer' is not a valid view "
|
|
|
|
"function or pattern name."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(NoReverseMatch, msg):
|
2013-03-07 07:33:51 +08:00
|
|
|
self.client.get('/second_test/')
|
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2013-10-14 21:14:17 +08:00
|
|
|
'%s.ChangeURLconfMiddleware' % middleware.__name__,
|
|
|
|
'%s.ReverseInnerInStreaming' % middleware.__name__,
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2013-10-14 21:14:17 +08:00
|
|
|
)
|
2013-03-07 07:33:51 +08:00
|
|
|
def test_reverse_inner_in_streaming(self):
|
|
|
|
"""
|
|
|
|
Test reversing an URL from the *overridden* URLconf from inside
|
|
|
|
a streaming response.
|
|
|
|
"""
|
|
|
|
response = self.client.get('/second_test/')
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(b''.join(response), b'/second_test/')
|
|
|
|
|
2013-10-14 21:14:17 +08:00
|
|
|
@override_settings(
|
2015-11-07 23:12:37 +08:00
|
|
|
MIDDLEWARE=[
|
2013-10-14 21:14:17 +08:00
|
|
|
'%s.ChangeURLconfMiddleware' % middleware.__name__,
|
|
|
|
'%s.ReverseOuterInStreaming' % middleware.__name__,
|
2015-01-22 00:55:57 +08:00
|
|
|
]
|
2013-10-14 21:14:17 +08:00
|
|
|
)
|
2013-03-07 07:33:51 +08:00
|
|
|
def test_reverse_outer_in_streaming(self):
|
|
|
|
"""
|
|
|
|
Test reversing an URL from the *default* URLconf from inside
|
|
|
|
a streaming response.
|
|
|
|
"""
|
2017-01-12 10:40:18 +08:00
|
|
|
message = "Reverse for 'outer' not found."
|
2013-03-07 07:33:51 +08:00
|
|
|
with self.assertRaisesMessage(NoReverseMatch, message):
|
|
|
|
self.client.get('/second_test/')
|
|
|
|
b''.join(self.client.get('/second_test/'))
|
|
|
|
|
2018-09-27 03:35:24 +08:00
|
|
|
def test_urlconf_is_reset_after_request(self):
|
|
|
|
"""The URLconf is reset after each request."""
|
|
|
|
self.assertIsNone(get_urlconf())
|
|
|
|
with override_settings(MIDDLEWARE=['%s.ChangeURLconfMiddleware' % middleware.__name__]):
|
|
|
|
self.client.get(reverse('inner'))
|
|
|
|
self.assertIsNone(get_urlconf())
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class ErrorHandlerResolutionTests(SimpleTestCase):
|
2013-05-16 07:14:28 +08:00
|
|
|
"""Tests for handler400, handler404 and handler500"""
|
2010-03-26 23:08:24 +08:00
|
|
|
|
2009-12-14 04:29:04 +08:00
|
|
|
def setUp(self):
|
2013-02-26 20:19:18 +08:00
|
|
|
urlconf = 'urlpatterns_reverse.urls_error_handlers'
|
|
|
|
urlconf_callables = 'urlpatterns_reverse.urls_error_handlers_callables'
|
2016-10-21 01:29:04 +08:00
|
|
|
self.resolver = URLResolver(RegexPattern(r'^$'), urlconf)
|
|
|
|
self.callable_resolver = URLResolver(RegexPattern(r'^$'), urlconf_callables)
|
2010-03-26 23:08:24 +08:00
|
|
|
|
2009-12-14 04:29:04 +08:00
|
|
|
def test_named_handlers(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
for code in [400, 404, 500]:
|
|
|
|
with self.subTest(code=code):
|
2020-06-23 03:28:56 +08:00
|
|
|
self.assertEqual(self.resolver.resolve_error_handler(code), empty_view)
|
2009-12-14 04:29:04 +08:00
|
|
|
|
2015-12-30 01:37:47 +08:00
|
|
|
def test_callable_handlers(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
for code in [400, 404, 500]:
|
|
|
|
with self.subTest(code=code):
|
2020-06-23 03:28:56 +08:00
|
|
|
self.assertEqual(self.callable_resolver.resolve_error_handler(code), empty_view)
|
2010-03-26 23:08:24 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2018-12-31 23:24:28 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.urls_without_handlers')
|
2015-04-18 05:38:20 +08:00
|
|
|
class DefaultErrorHandlerTests(SimpleTestCase):
|
2010-08-14 22:27:35 +08:00
|
|
|
|
|
|
|
def test_default_handler(self):
|
|
|
|
"If the urls.py doesn't specify handlers, the defaults are used"
|
2016-06-28 23:21:26 +08:00
|
|
|
response = self.client.get('/test/')
|
|
|
|
self.assertEqual(response.status_code, 404)
|
|
|
|
|
2019-03-16 07:27:57 +08:00
|
|
|
msg = "I don't think I'm getting good value for this view"
|
|
|
|
with self.assertRaisesMessage(ValueError, msg):
|
2016-06-28 23:21:26 +08:00
|
|
|
self.client.get('/bad_view/')
|
2010-08-14 22:27:35 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF=None)
|
2015-04-18 05:38:20 +08:00
|
|
|
class NoRootUrlConfTests(SimpleTestCase):
|
2015-10-23 02:46:42 +08:00
|
|
|
"""Tests for handler404 and handler500 if ROOT_URLCONF is None"""
|
2010-03-26 23:08:24 +08:00
|
|
|
|
|
|
|
def test_no_handler_exception(self):
|
2017-05-29 03:37:21 +08:00
|
|
|
msg = (
|
|
|
|
"The included URLconf 'None' does not appear to have any patterns "
|
2021-05-27 03:06:47 +08:00
|
|
|
"in it. If you see the 'urlpatterns' variable with valid patterns "
|
|
|
|
"in the file then the issue is probably caused by a circular "
|
|
|
|
"import."
|
2017-05-29 03:37:21 +08:00
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
2016-01-17 19:26:39 +08:00
|
|
|
self.client.get('/test/me/')
|
2010-08-05 15:09:47 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.namespace_urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class ResolverMatchTests(SimpleTestCase):
|
2010-08-05 15:09:47 +08:00
|
|
|
|
|
|
|
def test_urlpattern_resolve(self):
|
2018-12-08 06:52:28 +08:00
|
|
|
for path_, url_name, app_name, namespace, view_name, func, args, kwargs in resolve_test_data:
|
|
|
|
with self.subTest(path=path_):
|
2017-07-07 20:23:11 +08:00
|
|
|
# Legacy support for extracting "function, args, kwargs".
|
2018-12-08 06:52:28 +08:00
|
|
|
match_func, match_args, match_kwargs = resolve(path_)
|
2017-07-07 20:23:11 +08:00
|
|
|
self.assertEqual(match_func, func)
|
|
|
|
self.assertEqual(match_args, args)
|
|
|
|
self.assertEqual(match_kwargs, kwargs)
|
|
|
|
# ResolverMatch capabilities.
|
2018-12-08 06:52:28 +08:00
|
|
|
match = resolve(path_)
|
2017-07-07 20:23:11 +08:00
|
|
|
self.assertEqual(match.__class__, ResolverMatch)
|
|
|
|
self.assertEqual(match.url_name, url_name)
|
|
|
|
self.assertEqual(match.app_name, app_name)
|
|
|
|
self.assertEqual(match.namespace, namespace)
|
|
|
|
self.assertEqual(match.view_name, view_name)
|
|
|
|
self.assertEqual(match.func, func)
|
|
|
|
self.assertEqual(match.args, args)
|
|
|
|
self.assertEqual(match.kwargs, kwargs)
|
|
|
|
# and for legacy purposes:
|
|
|
|
self.assertEqual(match[0], func)
|
|
|
|
self.assertEqual(match[1], args)
|
|
|
|
self.assertEqual(match[2], kwargs)
|
2011-06-17 00:41:14 +08:00
|
|
|
|
2012-09-27 21:06:58 +08:00
|
|
|
def test_resolver_match_on_request(self):
|
|
|
|
response = self.client.get('/resolver_match/')
|
|
|
|
resolver_match = response.resolver_match
|
|
|
|
self.assertEqual(resolver_match.url_name, 'test-resolver-match')
|
|
|
|
|
2013-03-11 06:24:34 +08:00
|
|
|
def test_resolver_match_on_request_before_resolution(self):
|
|
|
|
request = HttpRequest()
|
|
|
|
self.assertIsNone(request.resolver_match)
|
|
|
|
|
2018-11-29 03:17:01 +08:00
|
|
|
def test_repr(self):
|
|
|
|
self.assertEqual(
|
|
|
|
repr(resolve('/no_kwargs/42/37/')),
|
|
|
|
"ResolverMatch(func=urlpatterns_reverse.views.empty_view, "
|
2021-03-19 22:44:03 +08:00
|
|
|
"args=('42', '37'), kwargs={}, url_name='no-kwargs', app_names=[], "
|
|
|
|
"namespaces=[], route='^no_kwargs/([0-9]+)/([0-9]+)/$')",
|
2018-11-29 03:17:01 +08:00
|
|
|
)
|
|
|
|
|
2021-03-19 22:44:03 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.urls')
|
|
|
|
def test_repr_functools_partial(self):
|
|
|
|
tests = [
|
|
|
|
('partial', 'template.html'),
|
|
|
|
('partial_nested', 'nested_partial.html'),
|
|
|
|
('partial_wrapped', 'template.html'),
|
|
|
|
]
|
|
|
|
for name, template_name in tests:
|
|
|
|
with self.subTest(name=name):
|
|
|
|
func = (
|
|
|
|
f"functools.partial({views.empty_view!r}, "
|
|
|
|
f"template_name='{template_name}')"
|
|
|
|
)
|
|
|
|
self.assertEqual(
|
|
|
|
repr(resolve(f'/{name}/')),
|
|
|
|
f"ResolverMatch(func={func}, args=(), kwargs={{}}, "
|
|
|
|
f"url_name='{name}', app_names=[], namespaces=[], "
|
|
|
|
f"route='{name}/')",
|
|
|
|
)
|
|
|
|
|
2021-07-29 13:04:56 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns.path_urls')
|
|
|
|
def test_pickling(self):
|
|
|
|
msg = 'Cannot pickle ResolverMatch.'
|
|
|
|
with self.assertRaisesMessage(pickle.PicklingError, msg):
|
|
|
|
pickle.dumps(resolve('/users/'))
|
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2014-04-05 14:04:46 +08:00
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.erroneous_urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class ErroneousViewTests(SimpleTestCase):
|
2011-06-17 00:41:14 +08:00
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
def test_noncallable_view(self):
|
|
|
|
# View is not a callable (explicit import; arbitrary Python object)
|
|
|
|
with self.assertRaisesMessage(TypeError, 'view must be a callable'):
|
2018-12-08 06:52:28 +08:00
|
|
|
path('uncallable-object/', views.uncallable)
|
2013-10-03 15:20:04 +08:00
|
|
|
|
2015-08-18 01:45:07 +08:00
|
|
|
def test_invalid_regex(self):
|
|
|
|
# Regex contains an error (refs #6170)
|
|
|
|
msg = '(regex_error/$" is not a valid regular expression'
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
reverse(views.empty_view)
|
2012-08-15 03:12:08 +08:00
|
|
|
|
2013-11-03 05:34:05 +08:00
|
|
|
|
2015-04-18 05:38:20 +08:00
|
|
|
class ViewLoadingTests(SimpleTestCase):
|
2012-08-15 03:12:08 +08:00
|
|
|
def test_view_loading(self):
|
2015-03-13 00:22:52 +08:00
|
|
|
self.assertEqual(get_callable('urlpatterns_reverse.views.empty_view'), empty_view)
|
2013-11-02 04:15:41 +08:00
|
|
|
self.assertEqual(get_callable(empty_view), empty_view)
|
|
|
|
|
2018-07-20 04:48:23 +08:00
|
|
|
def test_view_does_not_exist(self):
|
|
|
|
msg = "View does not exist in module urlpatterns_reverse.views."
|
|
|
|
with self.assertRaisesMessage(ViewDoesNotExist, msg):
|
2015-03-13 00:22:52 +08:00
|
|
|
get_callable('urlpatterns_reverse.views.i_should_not_exist')
|
2018-07-20 04:48:23 +08:00
|
|
|
|
|
|
|
def test_attributeerror_not_hidden(self):
|
|
|
|
msg = 'I am here to confuse django.urls.get_callable'
|
|
|
|
with self.assertRaisesMessage(AttributeError, msg):
|
2015-03-13 00:22:52 +08:00
|
|
|
get_callable('urlpatterns_reverse.views_broken.i_am_broken')
|
2015-02-09 02:44:07 +08:00
|
|
|
|
2018-07-20 04:48:23 +08:00
|
|
|
def test_non_string_value(self):
|
|
|
|
msg = "'1' is not a callable or a dot-notation path"
|
|
|
|
with self.assertRaisesMessage(ViewDoesNotExist, msg):
|
|
|
|
get_callable(1)
|
|
|
|
|
|
|
|
def test_string_without_dot(self):
|
|
|
|
msg = "Could not import 'test'. The path must be fully qualified."
|
|
|
|
with self.assertRaisesMessage(ImportError, msg):
|
|
|
|
get_callable('test')
|
|
|
|
|
|
|
|
def test_module_does_not_exist(self):
|
|
|
|
with self.assertRaisesMessage(ImportError, "No module named 'foo'"):
|
|
|
|
get_callable('foo.bar')
|
|
|
|
|
|
|
|
def test_parent_module_does_not_exist(self):
|
|
|
|
msg = 'Parent module urlpatterns_reverse.foo does not exist.'
|
|
|
|
with self.assertRaisesMessage(ViewDoesNotExist, msg):
|
|
|
|
get_callable('urlpatterns_reverse.foo.bar')
|
|
|
|
|
|
|
|
def test_not_callable(self):
|
|
|
|
msg = (
|
|
|
|
"Could not import 'urlpatterns_reverse.tests.resolve_test_data'. "
|
|
|
|
"View is not callable."
|
|
|
|
)
|
|
|
|
with self.assertRaisesMessage(ViewDoesNotExist, msg):
|
|
|
|
get_callable('urlpatterns_reverse.tests.resolve_test_data')
|
|
|
|
|
2015-02-09 02:44:07 +08:00
|
|
|
|
|
|
|
class IncludeTests(SimpleTestCase):
|
2015-05-28 23:25:52 +08:00
|
|
|
url_patterns = [
|
2018-12-08 06:52:28 +08:00
|
|
|
path('inner/', views.empty_view, name='urlobject-view'),
|
|
|
|
re_path(r'^inner/(?P<arg1>[0-9]+)/(?P<arg2>[0-9]+)/$', views.empty_view, name='urlobject-view'),
|
|
|
|
re_path(r'^inner/\+\\\$\*/$', views.empty_view, name='urlobject-special-view'),
|
2015-05-28 23:25:52 +08:00
|
|
|
]
|
|
|
|
app_urls = URLObject('inc-app')
|
|
|
|
|
|
|
|
def test_include_urls(self):
|
|
|
|
self.assertEqual(include(self.url_patterns), (self.url_patterns, None, None))
|
|
|
|
|
|
|
|
def test_include_namespace(self):
|
2017-01-10 22:57:49 +08:00
|
|
|
msg = (
|
2017-09-14 03:24:12 +08:00
|
|
|
'Specifying a namespace in include() without providing an '
|
|
|
|
'app_name is not supported.'
|
2015-05-28 23:25:52 +08:00
|
|
|
)
|
2017-01-10 22:57:49 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
include(self.url_patterns, 'namespace')
|
2015-05-28 23:25:52 +08:00
|
|
|
|
2017-01-28 00:47:19 +08:00
|
|
|
def test_include_4_tuple(self):
|
2017-09-14 03:24:12 +08:00
|
|
|
msg = 'Passing a 4-tuple to include() is not supported.'
|
2017-01-28 00:47:19 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
include((self.url_patterns, 'app_name', 'namespace', 'blah'))
|
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
def test_include_3_tuple(self):
|
2017-09-14 03:24:12 +08:00
|
|
|
msg = 'Passing a 3-tuple to include() is not supported.'
|
2017-01-10 22:57:49 +08:00
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
include((self.url_patterns, 'app_name', 'namespace'))
|
2015-05-28 23:25:52 +08:00
|
|
|
|
2017-01-28 00:47:19 +08:00
|
|
|
def test_include_3_tuple_namespace(self):
|
|
|
|
msg = 'Cannot override the namespace for a dynamic module that provides a namespace.'
|
|
|
|
with self.assertRaisesMessage(ImproperlyConfigured, msg):
|
|
|
|
include((self.url_patterns, 'app_name', 'namespace'), 'namespace')
|
|
|
|
|
2015-05-28 23:25:52 +08:00
|
|
|
def test_include_2_tuple(self):
|
|
|
|
self.assertEqual(
|
|
|
|
include((self.url_patterns, 'app_name')),
|
|
|
|
(self.url_patterns, 'app_name', 'app_name')
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_include_2_tuple_namespace(self):
|
|
|
|
self.assertEqual(
|
|
|
|
include((self.url_patterns, 'app_name'), namespace='namespace'),
|
|
|
|
(self.url_patterns, 'app_name', 'namespace')
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_include_app_name(self):
|
|
|
|
self.assertEqual(
|
|
|
|
include(self.app_urls),
|
|
|
|
(self.app_urls, 'inc-app', 'inc-app')
|
|
|
|
)
|
|
|
|
|
|
|
|
def test_include_app_name_namespace(self):
|
|
|
|
self.assertEqual(
|
|
|
|
include(self.app_urls, 'namespace'),
|
|
|
|
(self.app_urls, 'inc-app', 'namespace')
|
|
|
|
)
|
2015-03-21 20:19:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
@override_settings(ROOT_URLCONF='urlpatterns_reverse.urls')
|
2015-04-18 05:38:20 +08:00
|
|
|
class LookaheadTests(SimpleTestCase):
|
2015-03-21 20:19:13 +08:00
|
|
|
def test_valid_resolve(self):
|
|
|
|
test_urls = [
|
|
|
|
'/lookahead-/a-city/',
|
|
|
|
'/lookbehind-/a-city/',
|
|
|
|
'/lookahead+/a-city/',
|
|
|
|
'/lookbehind+/a-city/',
|
|
|
|
]
|
|
|
|
for test_url in test_urls:
|
2017-07-07 20:23:11 +08:00
|
|
|
with self.subTest(url=test_url):
|
|
|
|
self.assertEqual(resolve(test_url).kwargs, {'city': 'a-city'})
|
2015-03-21 20:19:13 +08:00
|
|
|
|
|
|
|
def test_invalid_resolve(self):
|
|
|
|
test_urls = [
|
|
|
|
'/lookahead-/not-a-city/',
|
|
|
|
'/lookbehind-/not-a-city/',
|
|
|
|
'/lookahead+/other-city/',
|
|
|
|
'/lookbehind+/other-city/',
|
|
|
|
]
|
|
|
|
for test_url in test_urls:
|
2017-07-07 20:23:11 +08:00
|
|
|
with self.subTest(url=test_url):
|
|
|
|
with self.assertRaises(Resolver404):
|
|
|
|
resolve(test_url)
|
2015-03-21 20:19:13 +08:00
|
|
|
|
|
|
|
def test_valid_reverse(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
test_urls = [
|
|
|
|
('lookahead-positive', {'city': 'a-city'}, '/lookahead+/a-city/'),
|
|
|
|
('lookahead-negative', {'city': 'a-city'}, '/lookahead-/a-city/'),
|
|
|
|
('lookbehind-positive', {'city': 'a-city'}, '/lookbehind+/a-city/'),
|
|
|
|
('lookbehind-negative', {'city': 'a-city'}, '/lookbehind-/a-city/'),
|
|
|
|
]
|
|
|
|
for name, kwargs, expected in test_urls:
|
|
|
|
with self.subTest(name=name, kwargs=kwargs):
|
|
|
|
self.assertEqual(reverse(name, kwargs=kwargs), expected)
|
2015-03-21 20:19:13 +08:00
|
|
|
|
|
|
|
def test_invalid_reverse(self):
|
2017-07-07 20:23:11 +08:00
|
|
|
test_urls = [
|
|
|
|
('lookahead-positive', {'city': 'other-city'}),
|
|
|
|
('lookahead-negative', {'city': 'not-a-city'}),
|
|
|
|
('lookbehind-positive', {'city': 'other-city'}),
|
|
|
|
('lookbehind-negative', {'city': 'not-a-city'}),
|
|
|
|
]
|
|
|
|
for name, kwargs in test_urls:
|
|
|
|
with self.subTest(name=name, kwargs=kwargs):
|
|
|
|
with self.assertRaises(NoReverseMatch):
|
|
|
|
reverse(name, kwargs=kwargs)
|