From 6771f4e348926608a86bc5152defd90a82fa3624 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Sun, 12 Apr 2009 03:54:49 +0000 Subject: [PATCH] [1.0.X] Fixed #10267 -- Correctly handle IRIs in HttpResponse.build_absolute_uri(). Backport of r10539 from trunk. git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10540 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/http/__init__.py | 6 +++--- tests/regressiontests/views/tests/__init__.py | 8 +++++--- tests/regressiontests/views/tests/specials.py | 15 +++++++++++++++ tests/regressiontests/views/urls.py | 5 +++++ tests/regressiontests/views/views.py | 9 ++++++++- 5 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 tests/regressiontests/views/tests/specials.py diff --git a/django/http/__init__.py b/django/http/__init__.py index 812a0fdfa1..7c6b8f9a0f 100644 --- a/django/http/__init__.py +++ b/django/http/__init__.py @@ -72,7 +72,7 @@ class HttpRequest(object): current_uri = '%s://%s%s' % (self.is_secure() and 'https' or 'http', self.get_host(), self.path) location = urljoin(current_uri, location) - return location + return iri_to_uri(location) def is_secure(self): return os.environ.get("HTTPS") == "on" @@ -398,14 +398,14 @@ class HttpResponseRedirect(HttpResponse): def __init__(self, redirect_to): HttpResponse.__init__(self) - self['Location'] = iri_to_uri(redirect_to) + self['Location'] = redirect_to class HttpResponsePermanentRedirect(HttpResponse): status_code = 301 def __init__(self, redirect_to): HttpResponse.__init__(self) - self['Location'] = iri_to_uri(redirect_to) + self['Location'] = redirect_to class HttpResponseNotModified(HttpResponse): status_code = 304 diff --git a/tests/regressiontests/views/tests/__init__.py b/tests/regressiontests/views/tests/__init__.py index 9964cd5833..ac947ab1dc 100644 --- a/tests/regressiontests/views/tests/__init__.py +++ b/tests/regressiontests/views/tests/__init__.py @@ -1,5 +1,7 @@ from defaults import * -from i18n import * -from static import * -from generic.date_based import * from generic.create_update import * +from generic.date_based import * +from i18n import * +from specials import * +from static import * + diff --git a/tests/regressiontests/views/tests/specials.py b/tests/regressiontests/views/tests/specials.py new file mode 100644 index 0000000000..af2f9d0ffe --- /dev/null +++ b/tests/regressiontests/views/tests/specials.py @@ -0,0 +1,15 @@ +# coding: utf-8 +from django.test import TestCase + +class URLHandling(TestCase): + """ + Tests for URL handling in views and responses. + """ + def test_iri_redirect(self): + """ + Tests that redirecting to an IRI, requiring encoding before we use it + in an HTTP response, is handled correctly. + """ + response = self.client.get(u'/views/中文/') + self.assertRedirects(response, "/views/%E4%B8%AD%E6%96%87/target/") + diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py index a6283c12bc..8ea0f31648 100644 --- a/tests/regressiontests/views/urls.py +++ b/tests/regressiontests/views/urls.py @@ -1,3 +1,4 @@ +# coding: utf-8 from os import path from django.conf.urls.defaults import * @@ -38,6 +39,10 @@ urlpatterns = patterns('', # Static views (r'^site_media/(?P.*)$', 'django.views.static.serve', {'document_root': media_dir}), + + # Special URLs for particular regression cases. + url(u'^中文/$', 'regressiontests.views.views.redirect'), + url(u'^中文/target/$', 'regressiontests.views.views.index_page'), ) # Date-based generic views. diff --git a/tests/regressiontests/views/views.py b/tests/regressiontests/views/views.py index b90852189c..9a9191dd4d 100644 --- a/tests/regressiontests/views/views.py +++ b/tests/regressiontests/views/views.py @@ -1,4 +1,4 @@ -from django.http import HttpResponse +from django.http import HttpResponse, HttpResponseRedirect from django import forms from django.views.generic.create_update import create_object @@ -27,3 +27,10 @@ def custom_create(request): return create_object(request, post_save_redirect='/views/create_update/view/article/%(slug)s/', form_class=SlugChangingArticleForm) + +def redirect(request): + """ + Forces an HTTP redirect. + """ + return HttpResponseRedirect("target/") +