diff --git a/CHANGES.rst b/CHANGES.rst index 5016a9821a0..dd23dc748af 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -17,7 +17,7 @@ CHANGES `web.Request.url` are added. URLs and templates are percent-encoded now. #1224 -- +- Accept `yarl.URL` by server redirections #1278 - diff --git a/aiohttp/web_exceptions.py b/aiohttp/web_exceptions.py index b886697a5dd..fa0869366d5 100644 --- a/aiohttp/web_exceptions.py +++ b/aiohttp/web_exceptions.py @@ -135,7 +135,7 @@ def __init__(self, location, *, headers=None, reason=None, raise ValueError("HTTP redirects need a location to redirect to.") super().__init__(headers=headers, reason=reason, body=body, text=text, content_type=content_type) - self.headers['Location'] = location + self.headers['Location'] = str(location) self.location = location diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index bdd98ae677c..055be1a2a63 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -6,6 +6,7 @@ import pytest from multidict import MultiDict +from yarl import URL from aiohttp import FormData, multipart, web from aiohttp.protocol import HttpVersion, HttpVersion10, HttpVersion11 @@ -838,3 +839,23 @@ def handler(request): resp = yield from client.get('/') assert 200 == resp.status assert client.handler.requests_count == 3 + + +@asyncio.coroutine +def test_redirect_url(loop, test_client): + + @asyncio.coroutine + def redirector(request): + raise web.HTTPFound(location=URL('/redirected')) + + @asyncio.coroutine + def redirected(request): + return web.Response() + + app = web.Application(loop=loop) + app.router.add_get('/redirector', redirector) + app.router.add_get('/redirected', redirected) + + client = yield from test_client(app) + resp = yield from client.get('/redirector') + assert resp.status == 200