Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.1.X] Fixed #6228: Changed common middleware to respect request-spe…

…cific urlconf. Thanks trey, skevy, and mikexstudios.

r12704 and r12705 from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12706 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit b86b38f6189b947a0fa1384daf7098112fa2bbad 1 parent 90e1266
Karen Tracey kmtracey authored
9 django/middleware/common.py
View
@@ -53,8 +53,9 @@ def process_request(self, request):
# Append a slash if APPEND_SLASH is set and the URL doesn't have a
# trailing slash and there is no pattern for the current path
if settings.APPEND_SLASH and (not old_url[1].endswith('/')):
- if (not _is_valid_path(request.path_info) and
- _is_valid_path("%s/" % request.path_info)):
+ urlconf = getattr(request, 'urlconf', None)
+ if (not _is_valid_path(request.path_info, urlconf) and
+ _is_valid_path("%s/" % request.path_info, urlconf)):
new_url[1] = new_url[1] + '/'
if settings.DEBUG and request.method == 'POST':
raise RuntimeError, (""
@@ -130,7 +131,7 @@ def _is_internal_request(domain, referer):
# Different subdomains are treated as different domains.
return referer is not None and re.match("^https?://%s/" % re.escape(domain), referer)
-def _is_valid_path(path):
+def _is_valid_path(path, urlconf=None):
"""
Returns True if the given path resolves against the default URL resolver,
False otherwise.
@@ -139,7 +140,7 @@ def _is_valid_path(path):
easier, avoiding unnecessarily indented try...except blocks.
"""
try:
- urlresolvers.resolve(path)
+ urlresolvers.resolve(path, urlconf)
return True
except urlresolvers.Resolver404:
return False
7 tests/regressiontests/middleware/extra_urls.py
View
@@ -0,0 +1,7 @@
+from django.conf.urls.defaults import patterns
+
+urlpatterns = patterns('',
+ (r'^middleware/customurlconf/noslash$', 'view'),
+ (r'^middleware/customurlconf/slash/$', 'view'),
+ (r'^middleware/customurlconf/needsquoting#/$', 'view'),
+)
121 tests/regressiontests/middleware/tests.py
View
@@ -125,3 +125,124 @@ def test_prepend_www_append_slash_slashless(self):
self.assertEquals(r.status_code, 301)
self.assertEquals(r['Location'],
'http://www.testserver/middleware/slash/')
+
+
+ # The following tests examine expected behavior given a custom urlconf that
+ # overrides the default one through the request object.
+
+ def test_append_slash_have_slash_custom_urlconf(self):
+ """
+ Tests that URLs with slashes go unmolested.
+ """
+ settings.APPEND_SLASH = True
+ request = self._get_request('customurlconf/slash/')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ self.assertEquals(CommonMiddleware().process_request(request), None)
+
+ def test_append_slash_slashless_resource_custom_urlconf(self):
+ """
+ Tests that matches to explicit slashless URLs go unmolested.
+ """
+ settings.APPEND_SLASH = True
+ request = self._get_request('customurlconf/noslash')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ self.assertEquals(CommonMiddleware().process_request(request), None)
+
+ def test_append_slash_slashless_unknown_custom_urlconf(self):
+ """
+ Tests that APPEND_SLASH doesn't redirect to unknown resources.
+ """
+ settings.APPEND_SLASH = True
+ request = self._get_request('customurlconf/unknown')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ self.assertEquals(CommonMiddleware().process_request(request), None)
+
+ def test_append_slash_redirect_custom_urlconf(self):
+ """
+ Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
+ """
+ settings.APPEND_SLASH = True
+ request = self._get_request('customurlconf/slash')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ r = CommonMiddleware().process_request(request)
+ self.failIf(r is None,
+ "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
+ self.assertEquals(r.status_code, 301)
+ self.assertEquals(r['Location'], 'http://testserver/middleware/customurlconf/slash/')
+
+ def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self):
+ """
+ Tests that while in debug mode, an exception is raised with a warning
+ when a failed attempt is made to POST to an URL which would normally be
+ redirected to a slashed version.
+ """
+ settings.APPEND_SLASH = True
+ settings.DEBUG = True
+ request = self._get_request('customurlconf/slash')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ request.method = 'POST'
+ self.assertRaises(
+ RuntimeError,
+ CommonMiddleware().process_request,
+ request)
+ try:
+ CommonMiddleware().process_request(request)
+ except RuntimeError, e:
+ self.failUnless('end in a slash' in str(e))
+ settings.DEBUG = False
+
+ def test_append_slash_disabled_custom_urlconf(self):
+ """
+ Tests disabling append slash functionality.
+ """
+ settings.APPEND_SLASH = False
+ request = self._get_request('customurlconf/slash')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ self.assertEquals(CommonMiddleware().process_request(request), None)
+
+ def test_append_slash_quoted_custom_urlconf(self):
+ """
+ Tests that URLs which require quoting are redirected to their slash
+ version ok.
+ """
+ settings.APPEND_SLASH = True
+ request = self._get_request('customurlconf/needsquoting#')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ r = CommonMiddleware().process_request(request)
+ self.failIf(r is None,
+ "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
+ self.assertEquals(r.status_code, 301)
+ self.assertEquals(
+ r['Location'],
+ 'http://testserver/middleware/customurlconf/needsquoting%23/')
+
+ def test_prepend_www_custom_urlconf(self):
+ settings.PREPEND_WWW = True
+ settings.APPEND_SLASH = False
+ request = self._get_request('customurlconf/path/')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ r = CommonMiddleware().process_request(request)
+ self.assertEquals(r.status_code, 301)
+ self.assertEquals(
+ r['Location'],
+ 'http://www.testserver/middleware/customurlconf/path/')
+
+ def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
+ settings.PREPEND_WWW = True
+ settings.APPEND_SLASH = True
+ request = self._get_request('customurlconf/slash/')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ r = CommonMiddleware().process_request(request)
+ self.assertEquals(r.status_code, 301)
+ self.assertEquals(r['Location'],
+ 'http://www.testserver/middleware/customurlconf/slash/')
+
+ def test_prepend_www_append_slash_slashless_custom_urlconf(self):
+ settings.PREPEND_WWW = True
+ settings.APPEND_SLASH = True
+ request = self._get_request('customurlconf/slash')
+ request.urlconf = 'regressiontests.middleware.extra_urls'
+ r = CommonMiddleware().process_request(request)
+ self.assertEquals(r.status_code, 301)
+ self.assertEquals(r['Location'],
+ 'http://www.testserver/middleware/customurlconf/slash/')
Please sign in to comment.
Something went wrong with that request. Please try again.