Completed test coverage for RedirectFallbackMiddleware.

This commit is contained in:
Anton Samarchyan 2017-03-17 15:34:04 -04:00 committed by Tim Graham
parent 2b53c8377d
commit 0c1355e9bb
2 changed files with 21 additions and 1 deletions

View File

@ -8,7 +8,7 @@ from django.test import TestCase, modify_settings, override_settings
@modify_settings(MIDDLEWARE={'append': 'django.contrib.redirects.middleware.RedirectFallbackMiddleware'}) @modify_settings(MIDDLEWARE={'append': 'django.contrib.redirects.middleware.RedirectFallbackMiddleware'})
@override_settings(APPEND_SLASH=False, SITE_ID=1) @override_settings(APPEND_SLASH=False, ROOT_URLCONF='redirects_tests.urls', SITE_ID=1)
class RedirectTests(TestCase): class RedirectTests(TestCase):
def setUp(self): def setUp(self):
@ -35,6 +35,20 @@ class RedirectTests(TestCase):
response = self.client.get('/initial?foo') response = self.client.get('/initial?foo')
self.assertRedirects(response, '/new_target/', status_code=301, target_status_code=404) self.assertRedirects(response, '/new_target/', status_code=301, target_status_code=404)
@override_settings(APPEND_SLASH=True)
def test_redirect_not_found_with_append_slash(self):
"""
Exercise the second Redirect.DoesNotExist branch in
RedirectFallbackMiddleware.
"""
response = self.client.get('/test')
self.assertEqual(response.status_code, 404)
def test_redirect_shortcircuits_non_404_response(self):
"""RedirectFallbackMiddleware short-circuits on non-404 requests."""
response = self.client.get('/')
self.assertEqual(response.status_code, 200)
def test_response_gone(self): def test_response_gone(self):
"""When the redirect target is '', return a 410""" """When the redirect target is '', return a 410"""
Redirect.objects.create(site=self.site, old_path='/initial', new_path='') Redirect.objects.create(site=self.site, old_path='/initial', new_path='')

View File

@ -0,0 +1,6 @@
from django.conf.urls import url
from django.http import HttpResponse
urlpatterns = [
url(r'^$', lambda req: HttpResponse('OK')),
]