diff options
| author | Alasdair Nicol <alasdair@thenicols.net> | 2015-09-16 23:07:39 +0100 |
|---|---|---|
| committer | Josh Smeaton <josh.smeaton@gmail.com> | 2015-09-21 23:46:21 +1000 |
| commit | fe3fc5210f0bb334a679ed420152af1c862c0239 (patch) | |
| tree | dc41897b19aaac166e62dd455334faa28fad0bab /tests | |
| parent | 2f53d342f1ce2fb36cddbd90e24c4fda86389c27 (diff) | |
Fixed #23813 -- Added checks for common URL pattern errors
Thanks jwa and lamby for the suggestions, and timgraham and jarshwah
for their reviews.
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/check_framework/test_urls.py | 37 | ||||
| -rw-r--r-- | tests/check_framework/urls_include.py | 7 | ||||
| -rw-r--r-- | tests/check_framework/urls_name.py | 13 | ||||
| -rw-r--r-- | tests/check_framework/urls_no_warnings.py | 15 | ||||
| -rw-r--r-- | tests/check_framework/urls_slash.py | 13 |
5 files changed, 85 insertions, 0 deletions
diff --git a/tests/check_framework/test_urls.py b/tests/check_framework/test_urls.py new file mode 100644 index 0000000000..2a05146e77 --- /dev/null +++ b/tests/check_framework/test_urls.py @@ -0,0 +1,37 @@ +from django.core.checks.urls import check_url_config +from django.test import SimpleTestCase +from django.test.utils import override_settings + + +class CheckUrlsTest(SimpleTestCase): + @override_settings(ROOT_URLCONF='check_framework.urls_no_warnings') + def test_include_no_warnings(self): + result = check_url_config(None) + self.assertEqual(result, []) + + @override_settings(ROOT_URLCONF='check_framework.urls_include') + def test_include_with_dollar(self): + result = check_url_config(None) + self.assertEqual(len(result), 1) + warning = result[0] + self.assertEqual(warning.id, 'urls.W001') + expected_msg = "Your URL pattern '^include-with-dollar$' uses include with a regex ending with a '$'." + self.assertIn(expected_msg, warning.msg) + + @override_settings(ROOT_URLCONF='check_framework.urls_slash') + def test_url_beginning_with_slash(self): + result = check_url_config(None) + self.assertEqual(len(result), 1) + warning = result[0] + self.assertEqual(warning.id, 'urls.W002') + expected_msg = "Your URL pattern '/starting-with-slash/$' has a regex beginning with a '/'" + self.assertIn(expected_msg, warning.msg) + + @override_settings(ROOT_URLCONF='check_framework.urls_name') + def test_url_pattern_name_with_colon(self): + result = check_url_config(None) + self.assertEqual(len(result), 1) + warning = result[0] + self.assertEqual(warning.id, 'urls.W003') + expected_msg = "Your URL pattern '^$' [name='name_with:colon'] has a name including a ':'." + self.assertIn(expected_msg, warning.msg) diff --git a/tests/check_framework/urls_include.py b/tests/check_framework/urls_include.py new file mode 100644 index 0000000000..5bb94c9688 --- /dev/null +++ b/tests/check_framework/urls_include.py @@ -0,0 +1,7 @@ +from django.conf.urls import include, url + +urlpatterns = [ + url(r'^', include([ + url(r'^include-with-dollar$', include([])), + ])), +] diff --git a/tests/check_framework/urls_name.py b/tests/check_framework/urls_name.py new file mode 100644 index 0000000000..9662532851 --- /dev/null +++ b/tests/check_framework/urls_name.py @@ -0,0 +1,13 @@ +from django.conf.urls import include, url +from django.http import HttpResponse + + +def view(request): + return HttpResponse('') + + +urlpatterns = [ + url('^', include([ + url(r'^$', view, name='name_with:colon'), + ])), +] diff --git a/tests/check_framework/urls_no_warnings.py b/tests/check_framework/urls_no_warnings.py new file mode 100644 index 0000000000..657306f1ef --- /dev/null +++ b/tests/check_framework/urls_no_warnings.py @@ -0,0 +1,15 @@ +from django.conf.urls import include, url +from django.http import HttpResponse + + +def view(request): + return HttpResponse('') + + +urlpatterns = [ + url(r'^foo/', view, name='foo'), + # This dollar is ok as it is escaped + url(r'^\$', include([ + url(r'^bar/$', view, name='bar'), + ])), +] diff --git a/tests/check_framework/urls_slash.py b/tests/check_framework/urls_slash.py new file mode 100644 index 0000000000..474f6293d5 --- /dev/null +++ b/tests/check_framework/urls_slash.py @@ -0,0 +1,13 @@ +from django.conf.urls import include, url +from django.http import HttpResponse + + +def view(request): + return HttpResponse('') + + +urlpatterns = [ + url('^', include([ + url(r'/starting-with-slash/$', view), + ])), +] |
