Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[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 authored
9  django/middleware/common.py
@@ -53,8 +53,9 @@ def process_request(self, request):
53 53
         # Append a slash if APPEND_SLASH is set and the URL doesn't have a
54 54
         # trailing slash and there is no pattern for the current path
55 55
         if settings.APPEND_SLASH and (not old_url[1].endswith('/')):
56  
-            if (not _is_valid_path(request.path_info) and
57  
-                    _is_valid_path("%s/" % request.path_info)):
  56
+            urlconf = getattr(request, 'urlconf', None)
  57
+            if (not _is_valid_path(request.path_info, urlconf) and
  58
+                    _is_valid_path("%s/" % request.path_info, urlconf)):
58 59
                 new_url[1] = new_url[1] + '/'
59 60
                 if settings.DEBUG and request.method == 'POST':
60 61
                     raise RuntimeError, (""
@@ -130,7 +131,7 @@ def _is_internal_request(domain, referer):
130 131
     # Different subdomains are treated as different domains.
131 132
     return referer is not None and re.match("^https?://%s/" % re.escape(domain), referer)
132 133
 
133  
-def _is_valid_path(path):
  134
+def _is_valid_path(path, urlconf=None):
134 135
     """
135 136
     Returns True if the given path resolves against the default URL resolver,
136 137
     False otherwise.
@@ -139,7 +140,7 @@ def _is_valid_path(path):
139 140
     easier, avoiding unnecessarily indented try...except blocks.
140 141
     """
141 142
     try:
142  
-        urlresolvers.resolve(path)
  143
+        urlresolvers.resolve(path, urlconf)
143 144
         return True
144 145
     except urlresolvers.Resolver404:
145 146
         return False
7  tests/regressiontests/middleware/extra_urls.py
... ...
@@ -0,0 +1,7 @@
  1
+from django.conf.urls.defaults import patterns
  2
+
  3
+urlpatterns = patterns('',
  4
+    (r'^middleware/customurlconf/noslash$', 'view'),
  5
+    (r'^middleware/customurlconf/slash/$', 'view'),
  6
+    (r'^middleware/customurlconf/needsquoting#/$', 'view'),
  7
+)
121  tests/regressiontests/middleware/tests.py
@@ -125,3 +125,124 @@ def test_prepend_www_append_slash_slashless(self):
125 125
         self.assertEquals(r.status_code, 301)
126 126
         self.assertEquals(r['Location'],
127 127
                           'http://www.testserver/middleware/slash/')
  128
+   
  129
+
  130
+    # The following tests examine expected behavior given a custom urlconf that
  131
+    # overrides the default one through the request object.
  132
+
  133
+    def test_append_slash_have_slash_custom_urlconf(self):
  134
+      """
  135
+      Tests that URLs with slashes go unmolested.
  136
+      """
  137
+      settings.APPEND_SLASH = True
  138
+      request = self._get_request('customurlconf/slash/')
  139
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  140
+      self.assertEquals(CommonMiddleware().process_request(request), None)
  141
+
  142
+    def test_append_slash_slashless_resource_custom_urlconf(self):
  143
+      """
  144
+      Tests that matches to explicit slashless URLs go unmolested.
  145
+      """
  146
+      settings.APPEND_SLASH = True
  147
+      request = self._get_request('customurlconf/noslash')
  148
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  149
+      self.assertEquals(CommonMiddleware().process_request(request), None)
  150
+
  151
+    def test_append_slash_slashless_unknown_custom_urlconf(self):
  152
+      """
  153
+      Tests that APPEND_SLASH doesn't redirect to unknown resources.
  154
+      """
  155
+      settings.APPEND_SLASH = True
  156
+      request = self._get_request('customurlconf/unknown')
  157
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  158
+      self.assertEquals(CommonMiddleware().process_request(request), None)
  159
+
  160
+    def test_append_slash_redirect_custom_urlconf(self):
  161
+      """
  162
+      Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
  163
+      """
  164
+      settings.APPEND_SLASH = True
  165
+      request = self._get_request('customurlconf/slash')
  166
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  167
+      r = CommonMiddleware().process_request(request)
  168
+      self.failIf(r is None, 
  169
+          "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
  170
+      self.assertEquals(r.status_code, 301)
  171
+      self.assertEquals(r['Location'], 'http://testserver/middleware/customurlconf/slash/')
  172
+
  173
+    def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self):
  174
+      """
  175
+      Tests that while in debug mode, an exception is raised with a warning
  176
+      when a failed attempt is made to POST to an URL which would normally be
  177
+      redirected to a slashed version.
  178
+      """
  179
+      settings.APPEND_SLASH = True
  180
+      settings.DEBUG = True
  181
+      request = self._get_request('customurlconf/slash')
  182
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  183
+      request.method = 'POST'
  184
+      self.assertRaises(
  185
+          RuntimeError,
  186
+          CommonMiddleware().process_request,
  187
+          request)
  188
+      try:
  189
+          CommonMiddleware().process_request(request)
  190
+      except RuntimeError, e:
  191
+          self.failUnless('end in a slash' in str(e))
  192
+      settings.DEBUG = False
  193
+
  194
+    def test_append_slash_disabled_custom_urlconf(self):
  195
+      """
  196
+      Tests disabling append slash functionality.
  197
+      """
  198
+      settings.APPEND_SLASH = False
  199
+      request = self._get_request('customurlconf/slash')
  200
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  201
+      self.assertEquals(CommonMiddleware().process_request(request), None)
  202
+
  203
+    def test_append_slash_quoted_custom_urlconf(self):
  204
+      """
  205
+      Tests that URLs which require quoting are redirected to their slash
  206
+      version ok.
  207
+      """
  208
+      settings.APPEND_SLASH = True
  209
+      request = self._get_request('customurlconf/needsquoting#')
  210
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  211
+      r = CommonMiddleware().process_request(request)
  212
+      self.failIf(r is None, 
  213
+          "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
  214
+      self.assertEquals(r.status_code, 301)
  215
+      self.assertEquals(
  216
+          r['Location'],
  217
+          'http://testserver/middleware/customurlconf/needsquoting%23/')
  218
+
  219
+    def test_prepend_www_custom_urlconf(self):
  220
+      settings.PREPEND_WWW = True
  221
+      settings.APPEND_SLASH = False
  222
+      request = self._get_request('customurlconf/path/')
  223
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  224
+      r = CommonMiddleware().process_request(request)
  225
+      self.assertEquals(r.status_code, 301)
  226
+      self.assertEquals(
  227
+          r['Location'],
  228
+          'http://www.testserver/middleware/customurlconf/path/')
  229
+
  230
+    def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
  231
+      settings.PREPEND_WWW = True
  232
+      settings.APPEND_SLASH = True
  233
+      request = self._get_request('customurlconf/slash/')
  234
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  235
+      r = CommonMiddleware().process_request(request)
  236
+      self.assertEquals(r.status_code, 301)
  237
+      self.assertEquals(r['Location'],
  238
+                        'http://www.testserver/middleware/customurlconf/slash/')
  239
+
  240
+    def test_prepend_www_append_slash_slashless_custom_urlconf(self):
  241
+      settings.PREPEND_WWW = True
  242
+      settings.APPEND_SLASH = True
  243
+      request = self._get_request('customurlconf/slash')
  244
+      request.urlconf = 'regressiontests.middleware.extra_urls'
  245
+      r = CommonMiddleware().process_request(request)
  246
+      self.assertEquals(r.status_code, 301)
  247
+      self.assertEquals(r['Location'],
  248
+                        'http://www.testserver/middleware/customurlconf/slash/')

0 notes on commit b86b38f

Please sign in to comment.
Something went wrong with that request. Please try again.