From 118826c9c3337be27591fab82bcd92b31b8c4bea Mon Sep 17 00:00:00 2001 From: Chris AtLee Date: Sat, 10 Sep 2016 05:02:11 -0400 Subject: [PATCH] Fix redirects for HEAD requests. (#1147) Fixes: #1146 --- aiohttp/client.py | 4 +++- tests/test_client_functional.py | 23 +++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/aiohttp/client.py b/aiohttp/client.py index 0e5ae1bcd4d..ed8fb065815 100644 --- a/aiohttp/client.py +++ b/aiohttp/client.py @@ -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): diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index cdbe0f96dce..00d1ba46030 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -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):