Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.5.x] Fixed #16678 -- Wrote tests for contrib.redirects app

Thanks Julien Phalip for the report.
Backport of d9213d0 from master.
  • Loading branch information...
commit a8c415f8447cafb58315aa9b6e7240d46991f445 1 parent acab68d
Claude Paroz authored

Showing 1 changed file with 42 additions and 0 deletions. Show diff stats Hide diff stats

  1. 42  django/contrib/redirects/tests.py
42  django/contrib/redirects/tests.py
... ...
@@ -0,0 +1,42 @@
  1
+from django.conf import settings
  2
+from django.contrib.sites.models import Site
  3
+from django.test import TestCase
  4
+from django.test.utils import override_settings
  5
+from django.utils import six
  6
+
  7
+from .models import Redirect
  8
+
  9
+
  10
+@override_settings(
  11
+    SITE_ID=1,
  12
+    APPEND_SLASH=True,
  13
+    MIDDLEWARE_CLASSES=list(settings.MIDDLEWARE_CLASSES) +
  14
+        ['django.contrib.redirects.middleware.RedirectFallbackMiddleware'],
  15
+)
  16
+class RedirectTests(TestCase):
  17
+
  18
+    def setUp(self):
  19
+        self.site = Site.objects.get(pk=settings.SITE_ID)
  20
+
  21
+    def test_model(self):
  22
+        r1 = Redirect.objects.create(
  23
+            site=self.site, old_path='/initial', new_path='/new_target')
  24
+        self.assertEqual(six.text_type(r1), "/initial ---> /new_target")
  25
+
  26
+    def test_redirect_middleware(self):
  27
+        r1 = Redirect.objects.create(
  28
+            site=self.site, old_path='/initial', new_path='/new_target')
  29
+        response = self.client.get('/initial')
  30
+        self.assertRedirects(response,
  31
+            '/new_target', status_code=301, target_status_code=404)
  32
+        # Works also with trailing slash
  33
+        response = self.client.get('/initial/')
  34
+        self.assertRedirects(response,
  35
+            '/new_target', status_code=301, target_status_code=404)
  36
+
  37
+    def test_response_gone(self):
  38
+        """When the redirect target is '', return a 410"""
  39
+        r1 = Redirect.objects.create(
  40
+            site=self.site, old_path='/initial', new_path='')
  41
+        response = self.client.get('/initial')
  42
+        self.assertEqual(response.status_code, 410)

0 notes on commit a8c415f

Please sign in to comment.
Something went wrong with that request. Please try again.