#28337: fetch_redirect_response from assertRedirects doesn't honor HTTP_HOST
setting in self.client.get
---------------------------------------------+------------------------
Reporter: Roland van Laar | Owner: nobody
Type: Bug | Status: new
Component: Testing framework | Version: 1.11
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 |
---------------------------------------------+------------------------
I would like to see `assertRedirects` honor the `HTTP_HOST` settings in
self.client.get.
I'm writing middleware that checks if `request.get_host()` is coupled to a
site, else we do a redirect to an external domain.
{{{
def test_redirect(self):
response = self.client.get("/", follow=False,
HTTP_HOST=self.site1.domain)
self.assertRedirects(response, '/accounts/login/?next=',
fetch_redirect_response=True)
}}}
The `assertRedirect` does an extra request to follow the redirect. What
goes wrong is that the HTTP_HOST with the request is not set to
'{{ self.site1.domain }}' but to `testserver`. This results in a redirect
to our external domain.
Middleware code:
{{{
class SiteMiddleware(object):
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
host = self.request.get_host()
try:
request.site = Site.objects.get(domain=host)
except ObjectDoesNotExist:
return HttpResponseRedirect("http://example.com")
return self.get_response(request)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/28337>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/051.cf06708c1f2243b86878d8791ac15cde%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.