Fixed #16705 - Made the test client adhere to the WSGI spec -- in particular, removed the assumption that environ['QUERY_STRING'] exists.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@16933 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
9279dce1fe
commit
e2f9c11736
|
@ -85,7 +85,7 @@ class CommonMiddleware(object):
|
||||||
new_url[0], urlquote(new_url[1]))
|
new_url[0], urlquote(new_url[1]))
|
||||||
else:
|
else:
|
||||||
newurl = urlquote(new_url[1])
|
newurl = urlquote(new_url[1])
|
||||||
if request.GET:
|
if request.META.get('QUERY_STRING', ''):
|
||||||
newurl += '?' + request.META['QUERY_STRING']
|
newurl += '?' + request.META['QUERY_STRING']
|
||||||
return http.HttpResponsePermanentRedirect(newurl)
|
return http.HttpResponsePermanentRedirect(newurl)
|
||||||
|
|
||||||
|
|
|
@ -182,18 +182,18 @@ class RequestFactory(object):
|
||||||
"""
|
"""
|
||||||
The base environment for a request.
|
The base environment for a request.
|
||||||
"""
|
"""
|
||||||
|
# This is a minimal valid WSGI environ dictionary, plus HTTP_COOKIE
|
||||||
|
# for cookie support. Empty strings are omitted.
|
||||||
|
# See http://www.python.org/dev/peps/pep-3333/#environ-variables
|
||||||
environ = {
|
environ = {
|
||||||
'HTTP_COOKIE': self.cookies.output(header='', sep='; '),
|
'HTTP_COOKIE': self.cookies.output(header='', sep='; '),
|
||||||
'PATH_INFO': '/',
|
|
||||||
'QUERY_STRING': '',
|
|
||||||
'REMOTE_ADDR': '127.0.0.1',
|
|
||||||
'REQUEST_METHOD': 'GET',
|
'REQUEST_METHOD': 'GET',
|
||||||
'SCRIPT_NAME': '',
|
|
||||||
'SERVER_NAME': 'testserver',
|
'SERVER_NAME': 'testserver',
|
||||||
'SERVER_PORT': '80',
|
'SERVER_PORT': '80',
|
||||||
'SERVER_PROTOCOL': 'HTTP/1.1',
|
'SERVER_PROTOCOL': 'HTTP/1.1',
|
||||||
'wsgi.version': (1,0),
|
'wsgi.version': (1,0),
|
||||||
'wsgi.url_scheme': 'http',
|
'wsgi.url_scheme': 'http',
|
||||||
|
'wsgi.input': FakePayload(''),
|
||||||
'wsgi.errors': self.errors,
|
'wsgi.errors': self.errors,
|
||||||
'wsgi.multiprocess': True,
|
'wsgi.multiprocess': True,
|
||||||
'wsgi.multithread': False,
|
'wsgi.multithread': False,
|
||||||
|
@ -235,7 +235,6 @@ class RequestFactory(object):
|
||||||
'PATH_INFO': self._get_path(parsed),
|
'PATH_INFO': self._get_path(parsed),
|
||||||
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
||||||
'REQUEST_METHOD': 'GET',
|
'REQUEST_METHOD': 'GET',
|
||||||
'wsgi.input': FakePayload('')
|
|
||||||
}
|
}
|
||||||
r.update(extra)
|
r.update(extra)
|
||||||
return self.request(**r)
|
return self.request(**r)
|
||||||
|
@ -267,7 +266,6 @@ class RequestFactory(object):
|
||||||
'PATH_INFO': self._get_path(parsed),
|
'PATH_INFO': self._get_path(parsed),
|
||||||
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
||||||
'REQUEST_METHOD': 'HEAD',
|
'REQUEST_METHOD': 'HEAD',
|
||||||
'wsgi.input': FakePayload('')
|
|
||||||
}
|
}
|
||||||
r.update(extra)
|
r.update(extra)
|
||||||
return self.request(**r)
|
return self.request(**r)
|
||||||
|
@ -280,7 +278,6 @@ class RequestFactory(object):
|
||||||
'PATH_INFO': self._get_path(parsed),
|
'PATH_INFO': self._get_path(parsed),
|
||||||
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
||||||
'REQUEST_METHOD': 'OPTIONS',
|
'REQUEST_METHOD': 'OPTIONS',
|
||||||
'wsgi.input': FakePayload('')
|
|
||||||
}
|
}
|
||||||
r.update(extra)
|
r.update(extra)
|
||||||
return self.request(**r)
|
return self.request(**r)
|
||||||
|
@ -311,7 +308,6 @@ class RequestFactory(object):
|
||||||
'PATH_INFO': self._get_path(parsed),
|
'PATH_INFO': self._get_path(parsed),
|
||||||
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
'QUERY_STRING': urlencode(data, doseq=True) or parsed[4],
|
||||||
'REQUEST_METHOD': 'DELETE',
|
'REQUEST_METHOD': 'DELETE',
|
||||||
'wsgi.input': FakePayload('')
|
|
||||||
}
|
}
|
||||||
r.update(extra)
|
r.update(extra)
|
||||||
return self.request(**r)
|
return self.request(**r)
|
||||||
|
|
|
@ -140,7 +140,7 @@ class RedirectView(View):
|
||||||
are provided as kwargs to this method.
|
are provided as kwargs to this method.
|
||||||
"""
|
"""
|
||||||
if self.url:
|
if self.url:
|
||||||
args = self.request.META["QUERY_STRING"]
|
args = self.request.META.get('QUERY_STRING', '')
|
||||||
if args and self.query_string:
|
if args and self.query_string:
|
||||||
url = "%s?%s" % (self.url, args)
|
url = "%s?%s" % (self.url, args)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -48,7 +48,7 @@ def redirect_to(request, url, permanent=True, query_string=False, **kwargs):
|
||||||
from the request is appended to the URL.
|
from the request is appended to the URL.
|
||||||
|
|
||||||
"""
|
"""
|
||||||
args = request.META["QUERY_STRING"]
|
args = request.META.get('QUERY_STRING', '')
|
||||||
if args and query_string and url is not None:
|
if args and query_string and url is not None:
|
||||||
url = "%s?%s" % (url, args)
|
url = "%s?%s" % (url, args)
|
||||||
|
|
||||||
|
|
|
@ -248,7 +248,7 @@ class RedirectViewTest(unittest.TestCase):
|
||||||
response = RedirectView.as_view()(self.rf.get('/foo/'))
|
response = RedirectView.as_view()(self.rf.get('/foo/'))
|
||||||
self.assertEqual(response.status_code, 410)
|
self.assertEqual(response.status_code, 410)
|
||||||
|
|
||||||
def test_permanaent_redirect(self):
|
def test_permanent_redirect(self):
|
||||||
"Default is a permanent redirect"
|
"Default is a permanent redirect"
|
||||||
response = RedirectView.as_view(url='/bar/')(self.rf.get('/foo/'))
|
response = RedirectView.as_view(url='/bar/')(self.rf.get('/foo/'))
|
||||||
self.assertEqual(response.status_code, 301)
|
self.assertEqual(response.status_code, 301)
|
||||||
|
@ -305,3 +305,9 @@ class RedirectViewTest(unittest.TestCase):
|
||||||
response = RedirectView.as_view(url='/bar/')(self.rf.delete('/foo/'))
|
response = RedirectView.as_view(url='/bar/')(self.rf.delete('/foo/'))
|
||||||
self.assertEqual(response.status_code, 301)
|
self.assertEqual(response.status_code, 301)
|
||||||
self.assertEqual(response['Location'], '/bar/')
|
self.assertEqual(response['Location'], '/bar/')
|
||||||
|
|
||||||
|
def test_redirect_when_meta_contains_no_query_string(self):
|
||||||
|
"regression for #16705"
|
||||||
|
# we can't use self.rf.get because it always sets QUERY_STRING
|
||||||
|
response = RedirectView.as_view(url='/bar/')(self.rf.request(PATH_INFO='/foo/'))
|
||||||
|
self.assertEqual(response.status_code, 301)
|
||||||
|
|
|
@ -47,3 +47,10 @@ class RedirectToTest(TestCase):
|
||||||
response = self.client.get('/simple/redirect_to_query/?param1=foo¶m2=bar')
|
response = self.client.get('/simple/redirect_to_query/?param1=foo¶m2=bar')
|
||||||
self.assertEqual(response.status_code, 301)
|
self.assertEqual(response.status_code, 301)
|
||||||
self.assertEqual('http://testserver/simple/target/?param1=foo¶m2=bar', response['Location'])
|
self.assertEqual('http://testserver/simple/target/?param1=foo¶m2=bar', response['Location'])
|
||||||
|
|
||||||
|
def test_redirect_to_when_meta_contains_no_query_string(self):
|
||||||
|
"regression for #16705"
|
||||||
|
# we can't use self.client.get because it always sets QUERY_STRING
|
||||||
|
response = self.client.request(PATH_INFO='/simple/redirect_to/')
|
||||||
|
self.assertEqual(response.status_code, 301)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue