Skip to content

Commit b50b3cf

Browse files
committed
[1.1.X] Fixed #11522: Restored ability of http redirect responses to correctly handle redirect locations with non-ASCII chars.
r12659 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.1.X@12660 bcc190cf-cafb-0310-a4f2-bffc1f526a37
1 parent cea0374 commit b50b3cf

File tree

3 files changed

+34
-5
lines changed

3 files changed

+34
-5
lines changed

django/http/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -434,14 +434,14 @@ class HttpResponseRedirect(HttpResponse):
434434

435435
def __init__(self, redirect_to):
436436
HttpResponse.__init__(self)
437-
self['Location'] = redirect_to
437+
self['Location'] = iri_to_uri(redirect_to)
438438

439439
class HttpResponsePermanentRedirect(HttpResponse):
440440
status_code = 301
441441

442442
def __init__(self, redirect_to):
443443
HttpResponse.__init__(self)
444-
self['Location'] = redirect_to
444+
self['Location'] = iri_to_uri(redirect_to)
445445

446446
class HttpResponseNotModified(HttpResponse):
447447
status_code = 304

tests/regressiontests/views/tests/specials.py

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,31 @@ class URLHandling(TestCase):
55
"""
66
Tests for URL handling in views and responses.
77
"""
8-
def test_iri_redirect(self):
8+
redirect_target = "/views/%E4%B8%AD%E6%96%87/target/"
9+
10+
def test_combining_redirect(self):
911
"""
1012
Tests that redirecting to an IRI, requiring encoding before we use it
11-
in an HTTP response, is handled correctly.
13+
in an HTTP response, is handled correctly. In this case the arg to
14+
HttpRedirect is ASCII but the current request path contains non-ASCII
15+
characters so this test ensures the creation of the full path with a
16+
base non-ASCII part is handled correctly.
1217
"""
1318
response = self.client.get(u'/views/中文/')
14-
self.assertRedirects(response, "/views/%E4%B8%AD%E6%96%87/target/")
19+
self.assertRedirects(response, self.redirect_target)
20+
21+
def test_nonascii_redirect(self):
22+
"""
23+
Tests that a non-ASCII argument to HttpRedirect is handled properly.
24+
"""
25+
response = self.client.get('/views/nonascii_redirect/')
26+
self.assertRedirects(response, self.redirect_target)
27+
28+
def test_permanent_nonascii_redirect(self):
29+
"""
30+
Tests that a non-ASCII argument to HttpPermanentRedirect is handled
31+
properly.
32+
"""
33+
response = self.client.get('/views/permanent_nonascii_redirect/')
34+
self.assertRedirects(response, self.redirect_target, status_code=301)
1535

tests/regressiontests/views/urls.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@
9797
urlpatterns += patterns('',
9898
(r'^raises/$', views.raises)
9999
)
100+
101+
# rediriects, both temporary and permanent, with non-ASCII targets
102+
urlpatterns += patterns('django.views.generic.simple',
103+
('^nonascii_redirect/$', 'redirect_to',
104+
{'url': u'/views/中文/target/', 'permanent': False}),
105+
('^permanent_nonascii_redirect/$', 'redirect_to',
106+
{'url': u'/views/中文/target/', 'permanent': True}),
107+
)
108+

0 commit comments

Comments
 (0)