Commits

Carl Meyer committed 684006d

Added iro_to_uri encoding of non-ASCII redirect URLs. Fixes #13

Comments (0)

Files changed (4)

 ---------
 
 Tip: (unreleased)
+  * Added iri_to_uri encoding of non-ASCII redirect URLs. Fixes #13.
   * Sorted language codes longest-first to avoid matching e.g. 'pt' before
     'pt-br'. Fixes #15. Thanks Roman Barczyński for report and draft patch.
   * Added ``LOCALE_INDEPENDENT_STATIC_URL`` setting to mirror

localeurl/middleware.py

 import django.core.exceptions
 from django.http import HttpResponsePermanentRedirect
 from django.utils import translation
-# TODO importing undocumented function
+from django.utils.encoding import iri_to_uri
 from django.utils.translation.trans_real import parse_accept_lang_header
 from localeurl import settings as localeurl_settings
 from localeurl import utils
             if request.META.get("QUERY_STRING", ""):
                 locale_url = "%s?%s" % (locale_url,
                         request.META['QUERY_STRING'])
-            return HttpResponsePermanentRedirect(locale_url)
+            return HttpResponsePermanentRedirect(iri_to_uri(locale_url))
         request.path_info = path
         if not locale:
             try:

localeurl/tests/__init__.py

+# -*- coding: utf-8 -*-
 """
 Tests for the localeurl application.
 """
         self.assertEqual('/en/test/', r2['Location'])
 
 
+    def test_non_ascii(self):
+        r1 = self.request_factory.get('/plaît/')
+        r2 = self.middleware.process_request(r1)
+        self.assertEqual(301, r2.status_code)
+        self.assertEqual('/en/pla%C3%AEt/', r2['Location'])
+
+
     def test_with_query_string(self):
         r1 = self.request_factory.get('/test/?somevar=someval')
         r2 = self.middleware.process_request(r1)
     sys.path.insert(0, parent)
     try:
         from django.test.simple import DjangoTestSuiteRunner
-        def run_tests(test_args, verbosity, interactive, failfast):
+        def run_tests(test_args, verbosity, interactive):
             runner = DjangoTestSuiteRunner(
-                verbosity=verbosity, interactive=interactive, failfast=failfast)
+                verbosity=verbosity, interactive=interactive, failfast=False)
             return runner.run_tests(test_args)
     except ImportError:
         # for Django versions that don't have DjangoTestSuiteRunner
         from django.test.simple import run_tests
     failures = run_tests(
-        test_args, verbosity=1, interactive=True, failfast=False)
+        test_args, verbosity=1, interactive=True)
     sys.exit(failures)