Ticket #6228: common_middleware_urlconf_patch.diff

File common_middleware_urlconf_patch.diff, 7.0 KB (added by skevy, 15 years ago)

Working patch for this ticket w/tests. Disregard other.

  • django/middleware/common.py

     
    5353        # Append a slash if APPEND_SLASH is set and the URL doesn't have a
    5454        # trailing slash and there is no pattern for the current path
    5555        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)):
    5859                new_url[1] = new_url[1] + '/'
    5960                if settings.DEBUG and request.method == 'POST':
    6061                    raise RuntimeError, (""
     
    130131    # Different subdomains are treated as different domains.
    131132    return referer is not None and re.match("^https?://%s/" % re.escape(domain), referer)
    132133
    133 def _is_valid_path(path):
     134def _is_valid_path(path, urlconf=None):
    134135    """
    135136    Returns True if the given path resolves against the default URL resolver,
    136137    False otherwise.
     
    139140    easier, avoiding unnecessarily indented try...except blocks.
    140141    """
    141142    try:
    142         urlresolvers.resolve(path)
     143        urlresolvers.resolve(path, urlconf)
    143144        return True
    144145    except urlresolvers.Resolver404:
    145146        return False
  • tests/regressiontests/middleware/extra_urls.py

     
     1from django.conf.urls.defaults import patterns
     2
     3urlpatterns = patterns('',
     4    (r'^middleware/noslash$', 'view'),
     5    (r'^middleware/slash/$', 'view'),
     6    (r'^middleware/needsquoting#/$', 'view'),
     7)
     8 No newline at end of file
  • tests/regressiontests/middleware/tests.py

     
    125125        self.assertEquals(r.status_code, 301)
    126126        self.assertEquals(r['Location'],
    127127                          'http://www.testserver/middleware/slash/')
     128
     129    def test_append_slash_have_slash_custom_urlconf(self):
     130      """
     131      Tests that URLs with slashes go unmolested.
     132      """
     133      settings.APPEND_SLASH = True
     134      request = self._get_request('slash/')
     135      request.urlconf = 'regressiontests.middleware.extra_urls'
     136      self.assertEquals(CommonMiddleware().process_request(request), None)
     137
     138    def test_append_slash_slashless_resource_custom_urlconf(self):
     139      """
     140      Tests that matches to explicit slashless URLs go unmolested.
     141      """
     142      settings.APPEND_SLASH = True
     143      request = self._get_request('noslash')
     144      request.urlconf = 'regressiontests.middleware.extra_urls'
     145      self.assertEquals(CommonMiddleware().process_request(request), None)
     146
     147    def test_append_slash_slashless_unknown_custom_urlconf(self):
     148      """
     149      Tests that APPEND_SLASH doesn't redirect to unknown resources.
     150      """
     151      settings.APPEND_SLASH = True
     152      request = self._get_request('unknown')
     153      request.urlconf = 'regressiontests.middleware.extra_urls'
     154      self.assertEquals(CommonMiddleware().process_request(request), None)
     155
     156    def test_append_slash_redirect_custom_urlconf(self):
     157      """
     158      Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
     159      """
     160      settings.APPEND_SLASH = True
     161      request = self._get_request('slash')
     162      request.urlconf = 'regressiontests.middleware.extra_urls'
     163      r = CommonMiddleware().process_request(request)
     164      self.assertEquals(r.status_code, 301)
     165      self.assertEquals(r['Location'], 'http://testserver/middleware/slash/')
     166
     167    def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self):
     168      """
     169      Tests that while in debug mode, an exception is raised with a warning
     170      when a failed attempt is made to POST to an URL which would normally be
     171      redirected to a slashed version.
     172      """
     173      settings.APPEND_SLASH = True
     174      settings.DEBUG = True
     175      request = self._get_request('slash')
     176      request.urlconf = 'regressiontests.middleware.extra_urls'
     177      request.method = 'POST'
     178      self.assertRaises(
     179          RuntimeError,
     180          CommonMiddleware().process_request,
     181          request)
     182      try:
     183          CommonMiddleware().process_request(request)
     184      except RuntimeError, e:
     185          self.failUnless('end in a slash' in str(e))
     186      settings.DEBUG = False
     187
     188    def test_append_slash_disabled_custom_urlconf(self):
     189      """
     190      Tests disabling append slash functionality.
     191      """
     192      settings.APPEND_SLASH = False
     193      request = self._get_request('slash')
     194      request.urlconf = 'regressiontests.middleware.extra_urls'
     195      self.assertEquals(CommonMiddleware().process_request(request), None)
     196
     197    def test_append_slash_quoted_custom_urlconf(self):
     198      """
     199      Tests that URLs which require quoting are redirected to their slash
     200      version ok.
     201      """
     202      settings.APPEND_SLASH = True
     203      request = self._get_request('needsquoting#')
     204      request.urlconf = 'regressiontests.middleware.extra_urls'
     205      r = CommonMiddleware().process_request(request)
     206      self.assertEquals(r.status_code, 301)
     207      self.assertEquals(
     208          r['Location'],
     209          'http://testserver/middleware/needsquoting%23/')
     210
     211    def test_prepend_www_custom_urlconf(self):
     212      settings.PREPEND_WWW = True
     213      settings.APPEND_SLASH = False
     214      request = self._get_request('path/')
     215      request.urlconf = 'regressiontests.middleware.extra_urls'
     216      r = CommonMiddleware().process_request(request)
     217      self.assertEquals(r.status_code, 301)
     218      self.assertEquals(
     219          r['Location'],
     220          'http://www.testserver/middleware/path/')
     221
     222    def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
     223      settings.PREPEND_WWW = True
     224      settings.APPEND_SLASH = True
     225      request = self._get_request('slash/')
     226      request.urlconf = 'regressiontests.middleware.extra_urls'
     227      r = CommonMiddleware().process_request(request)
     228      self.assertEquals(r.status_code, 301)
     229      self.assertEquals(r['Location'],
     230                        'http://www.testserver/middleware/slash/')
     231
     232    def test_prepend_www_append_slash_slashless_custom_urlconf(self):
     233      settings.PREPEND_WWW = True
     234      settings.APPEND_SLASH = True
     235      request = self._get_request('slash')
     236      request.urlconf = 'regressiontests.middleware.extra_urls'
     237      r = CommonMiddleware().process_request(request)
     238      self.assertEquals(r.status_code, 301)
     239      self.assertEquals(r['Location'],
     240                        'http://www.testserver/middleware/slash/')
Back to Top