[Django] #31430: Bug in django.test.client.Client._handle_redirects

6 views
Skip to first unread message

Django

unread,
Apr 6, 2020, 9:37:42 AM4/6/20
to django-...@googlegroups.com
#31430: Bug in django.test.client.Client._handle_redirects
-----------------------------------------+------------------------
Reporter: Jacob Stöhr | Owner: nobody
Type: Bug | Status: new
Component: Uncategorized | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-----------------------------------------+------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/31430>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 6, 2020, 9:38:46 AM4/6/20
to django-...@googlegroups.com
#31430: Bug in django.test.client.Client._handle_redirects
-------------------------------+---------------------------------------
Reporter: Jacob Stöhr | Owner: Jacob Stöhr
Type: Bug | Status: assigned
Component: Uncategorized | Version: master
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------
Changes (by Jacob Stöhr):

* owner: nobody => Jacob Stöhr
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/31430#comment:1>

Django

unread,
Apr 6, 2020, 11:41:35 AM4/6/20
to django-...@googlegroups.com
#31430: Bug in django.test.client.Client._handle_redirects
-------------------------------+---------------------------------------
Reporter: Jacob Stöhr | Owner: Jacob Stöhr
Type: Bug | Status: assigned
Component: Uncategorized | Version: master
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+---------------------------------------

Comment (by Jacob Stöhr):

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.

--
Ticket URL: <https://code.djangoproject.com/ticket/31430#comment:2>

Django

unread,
Apr 6, 2020, 12:44:18 PM4/6/20
to django-...@googlegroups.com
#31430: Bug in django.test.client.Client._handle_redirects.
-----------------------------------+---------------------------------------

Reporter: Jacob Stöhr | Owner: Jacob Stöhr
Type: Bug | Status: closed
Component: Testing framework | Version: master
Severity: Normal | Resolution: invalid

Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+---------------------------------------
Changes (by felixxm):

* status: assigned => closed
* resolution: => invalid
* component: Uncategorized => Testing framework


Comment:

`test.Client` supports 307 and 308 redirects (see #27999). You should
return `HttpResponseRedirect(redirect_to, status=308)`.

--
Ticket URL: <https://code.djangoproject.com/ticket/31430#comment:3>

Django

unread,
Apr 6, 2020, 2:58:02 PM4/6/20
to django-...@googlegroups.com
#31430: Bug in django.test.client.Client._handle_redirects.
-----------------------------------+---------------------------------------
Reporter: Jacob Stöhr | Owner: Jacob Stöhr
Type: Bug | Status: closed
Component: Testing framework | Version: master
Severity: Normal | Resolution: invalid
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------+---------------------------------------

Comment (by Jacob Stöhr):

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 :)

--
Ticket URL: <https://code.djangoproject.com/ticket/31430#comment:4>

Reply all
Reply to author
Forward
0 new messages