From 6c0624631ebea4dc5ea63177e25cfffc88ec69c2 Mon Sep 17 00:00:00 2001 From: Henrik Ossipoff Hansen Date: Mon, 19 May 2014 12:59:13 +0200 Subject: [PATCH 1/2] Test showing behaviour of issue #1586 given Python3 and Internet Explorer behaviour on query strings --- rest_framework/test.py | 14 ++++++++++++++ rest_framework/tests/test_request.py | 15 ++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/rest_framework/test.py b/rest_framework/test.py index df5a5b3b3..abc1fd976 100644 --- a/rest_framework/test.py +++ b/rest_framework/test.py @@ -9,6 +9,7 @@ from django.test.client import Client as DjangoClient from django.test.client import ClientHandler from django.test import testcases from django.utils.http import urlencode +from django.utils.six.moves.urllib.parse import unquote from rest_framework.settings import api_settings from rest_framework.compat import RequestFactory as DjangoRequestFactory from rest_framework.compat import force_bytes_or_smart_bytes, six @@ -169,3 +170,16 @@ if django.VERSION >= (1, 4): class APILiveServerTestCase(testcases.LiveServerTestCase): client_class = APIClient + + +class IEClient(DjangoClient): + def request(self, **kwargs): + try: + kwargs['QUERY_STRING'] = unquote(kwargs['QUERY_STRING']) + except: + pass + return super(IEClient, self).request(**kwargs) + + +class IETestCase(testcases.TestCase): + client_class = IEClient diff --git a/rest_framework/tests/test_request.py b/rest_framework/tests/test_request.py index c0b50f330..236524be7 100644 --- a/rest_framework/tests/test_request.py +++ b/rest_framework/tests/test_request.py @@ -1,3 +1,4 @@ +# -*- coding: utf-8 -*- """ Tests for content parsing, and form-overloaded content parsing. """ @@ -19,7 +20,7 @@ from rest_framework.parsers import ( from rest_framework.request import Request, Empty from rest_framework.response import Response from rest_framework.settings import api_settings -from rest_framework.test import APIRequestFactory, APIClient +from rest_framework.test import APIRequestFactory, APIClient, IETestCase from rest_framework.views import APIView from rest_framework.compat import six from io import BytesIO @@ -272,6 +273,9 @@ class MockView(APIView): return Response(status=status.INTERNAL_SERVER_ERROR) + def get(self, request): + return Response({}) + urlpatterns = patterns('', (r'^$', MockView.as_view()), ) @@ -345,3 +349,12 @@ class TestAuthSetter(TestCase): request = Request(factory.get('/')) request.auth = 'DUMMY' self.assertEqual(request.auth, 'DUMMY') + + +class TestQueryString(IETestCase): + urls = 'rest_framework.tests.test_request' + + def test_query_string_utf8(self): + qs = {'q': u'pølse'} + response = self.client.get('/', qs) + self.assertEqual(status.HTTP_200_OK, response.status_code) From 73aa12297a3ed41e9aeeb89e694ad3bb626d9e71 Mon Sep 17 00:00:00 2001 From: Henrik Ossipoff Hansen Date: Mon, 19 May 2014 14:54:00 +0200 Subject: [PATCH 2/2] Rely on built-in unquote functionality from Python --- rest_framework/test.py | 5 ++++- rest_framework/tests/test_request.py | 2 +- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/rest_framework/test.py b/rest_framework/test.py index abc1fd976..dfec698ba 100644 --- a/rest_framework/test.py +++ b/rest_framework/test.py @@ -3,13 +3,16 @@ # Note that we import as `DjangoRequestFactory` and `DjangoClient` in order # to make it harder for the user to import the wrong thing without realizing. from __future__ import unicode_literals +try: + from urllib import unquote +except ImportError: + from urllib.parse import unquote import django from django.conf import settings from django.test.client import Client as DjangoClient from django.test.client import ClientHandler from django.test import testcases from django.utils.http import urlencode -from django.utils.six.moves.urllib.parse import unquote from rest_framework.settings import api_settings from rest_framework.compat import RequestFactory as DjangoRequestFactory from rest_framework.compat import force_bytes_or_smart_bytes, six diff --git a/rest_framework/tests/test_request.py b/rest_framework/tests/test_request.py index 236524be7..e32a50b88 100644 --- a/rest_framework/tests/test_request.py +++ b/rest_framework/tests/test_request.py @@ -355,6 +355,6 @@ class TestQueryString(IETestCase): urls = 'rest_framework.tests.test_request' def test_query_string_utf8(self): - qs = {'q': u'pølse'} + qs = {'q': 'pølse'} response = self.client.get('/', qs) self.assertEqual(status.HTTP_200_OK, response.status_code)