From a8c415f8447cafb58315aa9b6e7240d46991f445 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 1 Nov 2012 19:44:45 +0100 Subject: [PATCH] [1.5.x] Fixed #16678 -- Wrote tests for contrib.redirects app Thanks Julien Phalip for the report. Backport of d9213d09db from master. --- django/contrib/redirects/tests.py | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 django/contrib/redirects/tests.py diff --git a/django/contrib/redirects/tests.py b/django/contrib/redirects/tests.py new file mode 100644 index 0000000000..11ffc7b748 --- /dev/null +++ b/django/contrib/redirects/tests.py @@ -0,0 +1,42 @@ +from django.conf import settings +from django.contrib.sites.models import Site +from django.test import TestCase +from django.test.utils import override_settings +from django.utils import six + +from .models import Redirect + + +@override_settings( + SITE_ID=1, + APPEND_SLASH=True, + MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) + + ['django.contrib.redirects.middleware.RedirectFallbackMiddleware'], +) +class RedirectTests(TestCase): + + def setUp(self): + self.site = Site.objects.get(pk=settings.SITE_ID) + + def test_model(self): + r1 = Redirect.objects.create( + site=self.site, old_path='/initial', new_path='/new_target') + self.assertEqual(six.text_type(r1), "/initial ---> /new_target") + + def test_redirect_middleware(self): + r1 = Redirect.objects.create( + site=self.site, old_path='/initial', new_path='/new_target') + response = self.client.get('/initial') + self.assertRedirects(response, + '/new_target', status_code=301, target_status_code=404) + # Works also with trailing slash + response = self.client.get('/initial/') + self.assertRedirects(response, + '/new_target', status_code=301, target_status_code=404) + + def test_response_gone(self): + """When the redirect target is '', return a 410""" + r1 = Redirect.objects.create( + site=self.site, old_path='/initial', new_path='') + response = self.client.get('/initial') + self.assertEqual(response.status_code, 410)