Skip to content

Commit

Permalink
Fix redirects for HEAD requests. (#1147)
Browse files Browse the repository at this point in the history
Fixes: #1146
  • Loading branch information
catlee authored and asvetlov committed Sep 10, 2016
1 parent 70f8a1d commit 118826c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
4 changes: 3 additions & 1 deletion aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,9 @@ def _request(self, method, url, *,

# For 301 and 302, mimic IE behaviour, now changed in RFC.
# Details: https://github.com/kennethreitz/requests/pull/269
if resp.status != 307:
if (resp.status == 303 and resp.method != hdrs.METH_HEAD) \
or (resp.status in (301, 302) and
resp.method == hdrs.METH_POST):
method = hdrs.METH_GET
data = None
if headers.get(hdrs.CONTENT_LENGTH):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -604,6 +604,29 @@ def redirect(request):
resp.close()


@asyncio.coroutine
def test_HTTP_302_REDIRECT_HEAD(create_app_and_client):
@asyncio.coroutine
def handler(request):
return web.Response(text=request.method)

@asyncio.coroutine
def redirect(request):
return web.HTTPFound(location='/')

app, client = yield from create_app_and_client()
app.router.add_get('/', handler)
app.router.add_get('/redirect', redirect)
app.router.add_head('/', handler)
app.router.add_head('/redirect', redirect)

resp = yield from client.request('head', '/redirect')
assert 200 == resp.status
assert 1 == len(resp.history)
assert resp.method == 'HEAD'
resp.close()


@asyncio.coroutine
def test_HTTP_302_REDIRECT_NON_HTTP(create_app_and_client):

Expand Down

0 comments on commit 118826c

Please sign in to comment.