mirror of
https://github.com/Hopiu/wagtail.git
synced 2026-05-03 04:54:45 +00:00
Provide tests for issue #647 and improve efficiency
Provide test cases for redirects stripping out query string. Don't repeat database query in middleware if there is no query string.
This commit is contained in:
parent
aa80e61bef
commit
13c6904598
2 changed files with 30 additions and 0 deletions
|
|
@ -13,12 +13,18 @@ class RedirectMiddleware(object):
|
|||
|
||||
# Get the path
|
||||
path = models.Redirect.normalise_path(request.get_full_path())
|
||||
|
||||
# Get the path without the query string or params
|
||||
path_without_query = urlparse(path)[2]
|
||||
|
||||
# Find redirect
|
||||
try:
|
||||
redirect = models.Redirect.get_for_site(request.site).get(old_path=path)
|
||||
except models.Redirect.DoesNotExist:
|
||||
if path == path_without_query:
|
||||
# don't try again if we know we will get the same response
|
||||
return response
|
||||
|
||||
try:
|
||||
redirect = models.Redirect.get_for_site(request.site).get(old_path=path_without_query)
|
||||
except models.Redirect.DoesNotExist:
|
||||
|
|
|
|||
|
|
@ -67,6 +67,30 @@ class TestRedirects(TestCase):
|
|||
self.assertEqual(r.status_code, 302)
|
||||
self.assertTrue(r.has_header('Location'))
|
||||
|
||||
def test_redirect_stripping_query_string(self):
|
||||
# Get a client
|
||||
c = Client()
|
||||
|
||||
# Create a redirect which includes a query string
|
||||
redirect_with_query_string = models.Redirect(old_path='/redirectme?foo=Bar', redirect_link='/with-query-string-only')
|
||||
redirect_with_query_string.save()
|
||||
|
||||
# ... and another redirect without the query string
|
||||
redirect_without_query_string = models.Redirect(old_path='/redirectme', redirect_link='/without-query-string')
|
||||
redirect_without_query_string.save()
|
||||
|
||||
# Navigate to the redirect with the query string
|
||||
r_matching_qs = c.get('/redirectme/?foo=Bar')
|
||||
self.assertEqual(r_matching_qs.status_code, 301)
|
||||
self.assertTrue(r_matching_qs.has_header('Location'))
|
||||
self.assertEqual(r_matching_qs['Location'][-23:], '/with-query-string-only')
|
||||
|
||||
# Navigate to the redirect with a different query string
|
||||
# This should strip out the query string and match redirect_without_query_string
|
||||
r_no_qs = c.get('/redirectme/?utm_source=irrelevant')
|
||||
self.assertEqual(r_no_qs.status_code, 301)
|
||||
self.assertTrue(r_no_qs.has_header('Location'))
|
||||
self.assertEqual(r_no_qs['Location'][-21:], '/without-query-string')
|
||||
|
||||
class TestRedirectsIndexView(TestCase, WagtailTestUtils):
|
||||
def setUp(self):
|
||||
|
|
|
|||
Loading…
Reference in a new issue