Ticket #6228: common_middleware_urlconf_patch_improved.diff

File common_middleware_urlconf_patch_improved.diff, 7.4 KB (added by mikexstudios, 15 years ago)

Improved previous patch by skevy so that some of the new tests fail without patch.

  • django/middleware/common.py

    diff --git django/middleware/common.py django/middleware/common.py
    index b2c97c6..3090588 100644
    class CommonMiddleware(object):  
    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, (""
    def _is_internal_request(domain, referer):  
    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.
    def _is_valid_path(path):  
    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
  • new file tests/regressiontests/middleware/extra_urls.py

    diff --git tests/regressiontests/middleware/extra_urls.py tests/regressiontests/middleware/extra_urls.py
    new file mode 100644
    index 0000000..b2a8902
    - +  
     1from django.conf.urls.defaults import patterns
     2
     3urlpatterns = patterns('',
     4    (r'^middleware/customurlconf/noslash$', 'view'),
     5    (r'^middleware/customurlconf/slash/$', 'view'),
     6    (r'^middleware/customurlconf/needsquoting#/$', 'view'),
     7)
  • tests/regressiontests/middleware/tests.py

    diff --git tests/regressiontests/middleware/tests.py tests/regressiontests/middleware/tests.py
    index b7e830a..789bb7f 100644
    class CommonMiddlewareTest(TestCase):  
    125125        self.assertEquals(r.status_code, 301)
    126126        self.assertEquals(r['Location'],
    127127                          '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.assertEquals(r.status_code, 301)
     169      self.assertEquals(r['Location'], 'http://testserver/middleware/customurlconf/slash/')
     170
     171    def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self):
     172      """
     173      Tests that while in debug mode, an exception is raised with a warning
     174      when a failed attempt is made to POST to an URL which would normally be
     175      redirected to a slashed version.
     176      """
     177      settings.APPEND_SLASH = True
     178      settings.DEBUG = True
     179      request = self._get_request('customurlconf/slash')
     180      request.urlconf = 'regressiontests.middleware.extra_urls'
     181      request.method = 'POST'
     182      self.assertRaises(
     183          RuntimeError,
     184          CommonMiddleware().process_request,
     185          request)
     186      try:
     187          CommonMiddleware().process_request(request)
     188      except RuntimeError, e:
     189          self.failUnless('end in a slash' in str(e))
     190      settings.DEBUG = False
     191
     192    def test_append_slash_disabled_custom_urlconf(self):
     193      """
     194      Tests disabling append slash functionality.
     195      """
     196      settings.APPEND_SLASH = False
     197      request = self._get_request('customurlconf/slash')
     198      request.urlconf = 'regressiontests.middleware.extra_urls'
     199      self.assertEquals(CommonMiddleware().process_request(request), None)
     200
     201    def test_append_slash_quoted_custom_urlconf(self):
     202      """
     203      Tests that URLs which require quoting are redirected to their slash
     204      version ok.
     205      """
     206      settings.APPEND_SLASH = True
     207      request = self._get_request('customurlconf/needsquoting#')
     208      request.urlconf = 'regressiontests.middleware.extra_urls'
     209      r = CommonMiddleware().process_request(request)
     210      self.assertEquals(r.status_code, 301)
     211      self.assertEquals(
     212          r['Location'],
     213          'http://testserver/middleware/customurlconf/needsquoting%23/')
     214
     215    def test_prepend_www_custom_urlconf(self):
     216      settings.PREPEND_WWW = True
     217      settings.APPEND_SLASH = False
     218      request = self._get_request('customurlconf/path/')
     219      request.urlconf = 'regressiontests.middleware.extra_urls'
     220      r = CommonMiddleware().process_request(request)
     221      self.assertEquals(r.status_code, 301)
     222      self.assertEquals(
     223          r['Location'],
     224          'http://www.testserver/middleware/customurlconf/path/')
     225
     226    def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
     227      settings.PREPEND_WWW = True
     228      settings.APPEND_SLASH = True
     229      request = self._get_request('customurlconf/slash/')
     230      request.urlconf = 'regressiontests.middleware.extra_urls'
     231      r = CommonMiddleware().process_request(request)
     232      self.assertEquals(r.status_code, 301)
     233      self.assertEquals(r['Location'],
     234                        'http://www.testserver/middleware/customurlconf/slash/')
     235
     236    def test_prepend_www_append_slash_slashless_custom_urlconf(self):
     237      settings.PREPEND_WWW = True
     238      settings.APPEND_SLASH = True
     239      request = self._get_request('customurlconf/slash')
     240      request.urlconf = 'regressiontests.middleware.extra_urls'
     241      r = CommonMiddleware().process_request(request)
     242      self.assertEquals(r.status_code, 301)
     243      self.assertEquals(r['Location'],
     244                        'http://www.testserver/middleware/customurlconf/slash/')
Back to Top