diff --git a/django/test/client.py b/django/test/client.py index 94cfada725..2196805c4c 100644 --- a/django/test/client.py +++ b/django/test/client.py @@ -335,7 +335,6 @@ class RequestFactory(object): data = force_bytes(data, settings.DEFAULT_CHARSET) r = { 'PATH_INFO': self._get_path(parsed), - 'QUERY_STRING': force_str(parsed[4]), 'REQUEST_METHOD': str(method), } if data: @@ -345,8 +344,16 @@ class RequestFactory(object): 'wsgi.input': FakePayload(data), }) r.update(extra) + # If QUERY_STRING is absent or empty, we want to extract it from the URL. + if not r.get('QUERY_STRING'): + query_string = force_bytes(parsed[4]) + # WSGI requires latin-1 encoded strings. See get_path_info(). + if six.PY3: + query_string = query_string.decode('iso-8859-1') + r['QUERY_STRING'] = query_string return self.request(**r) + class Client(RequestFactory): """ A class that can act as a client for testing purposes.