Ticket #22996: 22996-1.6.diff

File 22996-1.6.diff, 2.1 KB (added by Claude Paroz, 10 years ago)
  • django/core/handlers/wsgi.py

    diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py
    index adc8804..98fbdfc 100644
    a b class WSGIRequest(http.HttpRequest):  
    134134            # The WSGI spec says 'QUERY_STRING' may be absent.
    135135            raw_query_string = self.environ.get('QUERY_STRING', str(''))
    136136            if six.PY3:
    137                 raw_query_string = raw_query_string.encode('iso-8859-1').decode('utf-8')
     137                try:
     138                    # Assuming first that the query string has been utf-8 encoded
     139                    raw_query_string = raw_query_string.encode('iso-8859-1').decode('utf-8')
     140                except UnicodeDecodeError:
     141                    pass
    138142            self._get = http.QueryDict(raw_query_string, encoding=self._encoding)
    139143        return self._get
    140144
  • tests/handlers/tests.py

    diff --git a/tests/handlers/tests.py b/tests/handlers/tests.py
    index f5dc7c8..99dc1ba 100644
    a b class HandlerTests(TestCase):  
    4444    def test_non_ascii_query_string(self):
    4545        """Test that non-ASCII query strings are properly decoded (#20530)."""
    4646        environ = RequestFactory().get('/').environ
    47         raw_query_string = 'want=café'
    48         if six.PY3:
    49             raw_query_string = raw_query_string.encode('utf-8').decode('iso-8859-1')
    50         environ['QUERY_STRING'] = raw_query_string
    51         request = WSGIRequest(environ)
    52         self.assertEqual(request.GET['want'], "café")
     47        raw_query_strings = ['want=café', 'want=cafÃ', 'want=café']
     48        got = []
     49        for raw_query_string in raw_query_strings:
     50            environ['QUERY_STRING'] = raw_query_string
     51            request = WSGIRequest(environ)
     52            got.append(request.GET['want'])
     53        if six.PY2:
     54            self.assertListEqual(got, ['café', 'cafÃ', 'café'])
     55        else:
     56            self.assertListEqual(got, ['café', 'cafÃ', 'café'])
    5357
    5458    def test_non_ascii_cookie(self):
    5559        """Test that non-ASCII cookies set in JavaScript are properly decoded (#20557)."""
Back to Top