Skip to content

Commit f466632

Browse files
committed
Add tests for redirects panel.
Also simplify the implementation.
1 parent c5e6a85 commit f466632

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

debug_toolbar/panels/redirects.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from __future__ import unicode_literals
22

33
from django.core.handlers.wsgi import STATUS_CODE_TEXT
4-
from django.http import HttpResponseRedirect
54
from django.shortcuts import render
65
from django.utils.translation import ugettext as _
76

@@ -22,14 +21,15 @@ def enabled(self):
2221
return self.toolbar.request.COOKIES.get('djdt' + self.panel_id, default) == 'on'
2322

2423
def process_response(self, request, response):
25-
if isinstance(response, HttpResponseRedirect):
24+
if 300 <= int(response.status_code) < 400:
2625
redirect_to = response.get('Location', None)
2726
if redirect_to:
28-
try:
29-
status_text = STATUS_CODE_TEXT[response.status_code]
30-
except KeyError:
31-
status_text = 'UNKNOWN STATUS CODE'
32-
status_line = '%s %s' % (response.status_code, status_text.title())
27+
try: # Django >= 1.6
28+
reason_phrase = response.reason_phrase
29+
except AttributeError:
30+
reason_phrase = STATUS_CODE_TEXT.get(response.status_code,
31+
'UNKNOWN STATUS CODE')
32+
status_line = '%s %s' % (response.status_code, reason_phrase)
3333
cookies = response.cookies
3434
context = {'redirect_to': redirect_to, 'status_line': status_line}
3535
response = render(request, 'debug_toolbar/redirect.html', context)

tests/panels/test_profiling.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ def test_without_line_profiler(self):
4343
profiling.DJ_PROFILE_USE_LINE_PROFILER = _use_line_profiler
4444

4545

46-
4746
@override_settings(DEBUG=True,
4847
DEBUG_TOOLBAR_PANELS=['debug_toolbar.panels.profiling.ProfilingDebugPanel'])
4948
class ProfilingPanelIntegrationTestCase(TestCase):

tests/panels/test_redirects.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from __future__ import unicode_literals
2+
3+
import django
4+
from django.http import HttpResponse
5+
from django.test.utils import override_settings
6+
from django.utils import unittest
7+
8+
from ..base import BaseTestCase
9+
10+
11+
@override_settings(DEBUG_TOOLBAR_CONFIG={'INTERCEPT_REDIRECTS': True})
12+
class RedirectsPanelTestCase(BaseTestCase):
13+
14+
def setUp(self):
15+
super(RedirectsPanelTestCase, self).setUp()
16+
self.panel = self.toolbar.get_panel_by_id('InterceptRedirectsPanel')
17+
18+
def test_regular_response(self):
19+
response = self.panel.process_response(self.request, self.response)
20+
self.assertTrue(response is self.response)
21+
22+
def test_not_a_redirect(self):
23+
redirect = HttpResponse(status=304) # not modified
24+
response = self.panel.process_response(self.request, redirect)
25+
self.assertTrue(response is redirect)
26+
27+
def test_redirect(self):
28+
redirect = HttpResponse(status=302)
29+
redirect['Location'] = 'http://somewhere/else/'
30+
response = self.panel.process_response(self.request, redirect)
31+
self.assertFalse(response is redirect)
32+
self.assertContains(response, '302 FOUND')
33+
self.assertContains(response, 'http://somewhere/else/')
34+
35+
def test_unknown_status_code(self):
36+
redirect = HttpResponse(status=369)
37+
redirect['Location'] = 'http://somewhere/else/'
38+
response = self.panel.process_response(self.request, redirect)
39+
self.assertContains(response, '369 UNKNOWN STATUS CODE')
40+
41+
@unittest.skipIf(django.VERSION[:2] < (1, 6), "reason isn't supported")
42+
def test_unknown_status_code_with_reason(self):
43+
redirect = HttpResponse(status=369, reason='Look Ma!')
44+
redirect['Location'] = 'http://somewhere/else/'
45+
response = self.panel.process_response(self.request, redirect)
46+
self.assertContains(response, '369 Look Ma!')

tests/tests.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from .panels.test_cache import * # noqa
66
from .panels.test_logger import * # noqa
77
from .panels.test_profiling import * # noqa
8+
from .panels.test_redirects import * # noqa
89
from .panels.test_request_vars import * # noqa
910
from .panels.test_sql import * # noqa
1011
from .panels.test_template import * # noqa

0 commit comments

Comments
 (0)