Allow test Client to re-use query string for non-GET requests when following 307/308 redirects.
If an application uses an HTTP 307 or 308 response to redirect /foo/ to /bar/?a=b, when the test client is instructed to follow redirects, the ultimate request will be to /bar/ (with no query string). This is due to the fact that the test client only examines the query portion of the response URL if the HTTP status is not 307 or 308:
if response.status_code in (HTTPStatus.TEMPORARY_REDIRECT, HTTPStatus.PERMANENT_REDIRECT):
# Preserve request method post-redirect for 307/308 responses.
request_method = getattr(self, response.request['REQUEST_METHOD'].lower())
else:
request_method = self.get
data = QueryDict(url.query)
content_type = None
The test client should respect query strings indicated in redirect URLs, even if the request method is something other than GET or HEAD.
Change History
(10)
Summary: |
Test client discards query string when following 307 or 308 redirects → Allow test Client to re-use query string for non-GET requests when following 307/308 redirects.
|
Triage Stage: |
Unreviewed → Accepted
|
Type: |
Uncategorized → New feature
|
Cc: |
Ahmad Abdallah added
|
Owner: |
changed from nobody to Ahmad Abdallah
|
Status: |
new → assigned
|
Patch needs improvement: |
set
|
Patch needs improvement: |
unset
|
Version: |
3.0 → master
|
Resolution: |
→ fixed
|
Status: |
assigned → closed
|
OK, so there's a valid use-case here I think. One should be able to explicitly pass the query string when creating the redirect response. Such as:
Expecting both the original POST data and the query string to be resubmitted when following the redirect. (Assuming a POST request in the first place.)
Examining the test client behaviour, the
redirect_chain
is correct (e.g.response.redirect_chain[0]
gives('/post_view/?hello=world', 307)
) but the final request query string is empty, as per the report._handle_redirects()
receives the correct request, including the query string, but this gets dropped by theurlsplit
call, and as Max says is not then reused.Where
method != 'GET'
path
should have the query parameters re-appended in this case.I'll mark this a new feature since it's never been supported. (Arguably could have been part of 272f685794de0b8dead220ee57b30e65c9aa097c but...)