#31430 closed Bug (invalid)
Bug in django.test.client.Client._handle_redirects.
Reported by: | Jacob Stöhr | Owned by: | Jacob Stöhr |
---|---|---|---|
Component: | Testing framework | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
When handling redirects via the method mentioned above, for example via client.get(path, follow=True)
, a failure will occur when a response received somewhere in the chain does not bases HttpResponseRedirectBase
from django.http.response because only instances/objects which are derived from that class have the url
attribute. The attribute is accessed inside the function mentioned in the summary to determine where to redirect to.
For example:
a view yields a 308 Code which will be an instance of a plain HttpResponse
object instead of a subclass of HttpResponseRedirectBase
, thus it does not have a url
attribute with the url the response is redirecting to.
I will hand in a patch soon.
Change History (4)
comment:1 by , 5 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:2 by , 5 years ago
comment:3 by , 5 years ago
Component: | Uncategorized → Testing framework |
---|---|
Resolution: | → invalid |
Status: | assigned → closed |
Summary: | Bug in django.test.client.Client._handle_redirects → Bug in django.test.client.Client._handle_redirects. |
test.Client
supports 307 and 308 redirects (see #27999). You should return HttpResponseRedirect(redirect_to, status=308)
.
comment:4 by , 5 years ago
Thank you for your speedy response!
I have now been debugging my issue for another ~2 hours (and I havent seen your mail until just now), turns out this was not django's fault (of course) but a commit no other than myself made 8 months ago that caused the problem in our project.
I foolishly returned a bare HttpResponse(status=308)
and overwrote the Location
to force a redirect. This of course failed in the _handle_redirects
method of the testclient because the status_code
was in redirect_status_codes
but the response was not based on HttpResponseRedirectBase
and did thus not have the url
property.
Your suggestion is of course correct, I will fix it in our project. Thank you very much :)
While trying to fix this bug, I encountered something else which I filed under https://code.djangoproject.com/ticket/31432 . Fixing that ticket would render this ticket obsolete, therefore I will not produce a patch for this ticket yet.