2007-12-03 07:25:55 +08:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2008-01-03 10:14:29 +08:00
|
|
|
from django.test import TestCase
|
2007-12-03 07:25:55 +08:00
|
|
|
from django.http import HttpRequest
|
|
|
|
from django.middleware.common import CommonMiddleware
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
class CommonMiddlewareTest(TestCase):
|
2008-10-08 16:36:41 +08:00
|
|
|
def setUp(self):
|
|
|
|
self.slash = settings.APPEND_SLASH
|
|
|
|
self.www = settings.PREPEND_WWW
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
settings.APPEND_SLASH = self.slash
|
|
|
|
settings.PREPEND_WWW = self.www
|
|
|
|
|
2007-12-03 07:25:55 +08:00
|
|
|
def _get_request(self, path):
|
|
|
|
request = HttpRequest()
|
|
|
|
request.META = {
|
|
|
|
'SERVER_NAME': 'testserver',
|
|
|
|
'SERVER_PORT': 80,
|
|
|
|
}
|
2008-08-21 09:32:18 +08:00
|
|
|
request.path = request.path_info = "/middleware/%s" % path
|
2007-12-03 07:25:55 +08:00
|
|
|
return request
|
|
|
|
|
|
|
|
def test_append_slash_have_slash(self):
|
|
|
|
"""
|
2008-01-03 10:14:29 +08:00
|
|
|
Tests that URLs with slashes go unmolested.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
|
|
|
settings.APPEND_SLASH = True
|
|
|
|
request = self._get_request('slash/')
|
|
|
|
self.assertEquals(CommonMiddleware().process_request(request), None)
|
|
|
|
|
|
|
|
def test_append_slash_slashless_resource(self):
|
|
|
|
"""
|
2008-01-03 10:14:29 +08:00
|
|
|
Tests that matches to explicit slashless URLs go unmolested.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
|
|
|
settings.APPEND_SLASH = True
|
|
|
|
request = self._get_request('noslash')
|
|
|
|
self.assertEquals(CommonMiddleware().process_request(request), None)
|
|
|
|
|
|
|
|
def test_append_slash_slashless_unknown(self):
|
|
|
|
"""
|
2008-01-03 10:14:29 +08:00
|
|
|
Tests that APPEND_SLASH doesn't redirect to unknown resources.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
|
|
|
settings.APPEND_SLASH = True
|
|
|
|
request = self._get_request('unknown')
|
|
|
|
self.assertEquals(CommonMiddleware().process_request(request), None)
|
|
|
|
|
|
|
|
def test_append_slash_redirect(self):
|
|
|
|
"""
|
2008-01-03 10:14:29 +08:00
|
|
|
Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
|
|
|
settings.APPEND_SLASH = True
|
|
|
|
request = self._get_request('slash')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEquals(r.status_code, 301)
|
|
|
|
self.assertEquals(r['Location'], 'http://testserver/middleware/slash/')
|
|
|
|
|
|
|
|
def test_append_slash_no_redirect_on_POST_in_DEBUG(self):
|
|
|
|
"""
|
2008-01-03 10:14:29 +08:00
|
|
|
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.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
|
|
|
settings.APPEND_SLASH = True
|
|
|
|
settings.DEBUG = True
|
|
|
|
request = self._get_request('slash')
|
|
|
|
request.method = 'POST'
|
|
|
|
self.assertRaises(
|
|
|
|
RuntimeError,
|
|
|
|
CommonMiddleware().process_request,
|
|
|
|
request)
|
|
|
|
try:
|
|
|
|
CommonMiddleware().process_request(request)
|
|
|
|
except RuntimeError, e:
|
2008-01-06 15:23:21 +08:00
|
|
|
self.failUnless('end in a slash' in str(e))
|
2008-01-03 10:14:29 +08:00
|
|
|
settings.DEBUG = False
|
2007-12-03 07:25:55 +08:00
|
|
|
|
|
|
|
def test_append_slash_disabled(self):
|
|
|
|
"""
|
2008-01-03 10:14:29 +08:00
|
|
|
Tests disabling append slash functionality.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
|
|
|
settings.APPEND_SLASH = False
|
|
|
|
request = self._get_request('slash')
|
|
|
|
self.assertEquals(CommonMiddleware().process_request(request), None)
|
|
|
|
|
|
|
|
def test_append_slash_quoted(self):
|
|
|
|
"""
|
2008-01-03 10:14:29 +08:00
|
|
|
Tests that URLs which require quoting are redirected to their slash
|
|
|
|
version ok.
|
2007-12-03 07:25:55 +08:00
|
|
|
"""
|
|
|
|
settings.APPEND_SLASH = True
|
|
|
|
request = self._get_request('needsquoting#')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEquals(r.status_code, 301)
|
|
|
|
self.assertEquals(
|
|
|
|
r['Location'],
|
|
|
|
'http://testserver/middleware/needsquoting%23/')
|
2008-10-07 16:22:50 +08:00
|
|
|
|
|
|
|
def test_prepend_www(self):
|
|
|
|
settings.PREPEND_WWW = True
|
|
|
|
settings.APPEND_SLASH = False
|
|
|
|
request = self._get_request('path/')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEquals(r.status_code, 301)
|
|
|
|
self.assertEquals(
|
|
|
|
r['Location'],
|
|
|
|
'http://www.testserver/middleware/path/')
|
|
|
|
|
|
|
|
def test_prepend_www_append_slash_have_slash(self):
|
|
|
|
settings.PREPEND_WWW = True
|
|
|
|
settings.APPEND_SLASH = True
|
|
|
|
request = self._get_request('slash/')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEquals(r.status_code, 301)
|
|
|
|
self.assertEquals(r['Location'],
|
|
|
|
'http://www.testserver/middleware/slash/')
|
|
|
|
|
|
|
|
def test_prepend_www_append_slash_slashless(self):
|
|
|
|
settings.PREPEND_WWW = True
|
|
|
|
settings.APPEND_SLASH = True
|
|
|
|
request = self._get_request('slash')
|
|
|
|
r = CommonMiddleware().process_request(request)
|
|
|
|
self.assertEquals(r.status_code, 301)
|
|
|
|
self.assertEquals(r['Location'],
|
|
|
|
'http://www.testserver/middleware/slash/')
|