Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix for redirect issue #2030

Merged
merged 10 commits into from
Jun 29, 2017
Merged
10 changes: 4 additions & 6 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
from . import connector as connector_mod
from . import client_exceptions, client_reqrep, hdrs, http, payload
from .client_exceptions import * # noqa
from .client_exceptions import (ClientError, ClientOSError,
ClientRedirectError, ServerTimeoutError,
from .client_exceptions import (ClientError, ClientOSError, ServerTimeoutError,
WSServerHandshakeError)
from .client_reqrep import * # noqa
from .client_reqrep import ClientRequest, ClientResponse
Expand Down Expand Up @@ -275,10 +274,9 @@ def _request(self, method, url, *,
r_url = (resp.headers.get(hdrs.LOCATION) or
resp.headers.get(hdrs.URI))
if r_url is None:
raise ClientRedirectError(
resp.request_info,
resp.history,
resp.status)
# see github.com/aio-libs/aiohttp/issues/2022
break

r_url = URL(
r_url, encoded=not self.requote_redirect_url)

Expand Down
14 changes: 1 addition & 13 deletions aiohttp/client_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
'ServerConnectionError', 'ServerTimeoutError', 'ServerDisconnectedError',
'ServerFingerprintMismatch',

'ClientResponseError', 'ClientRedirectError', 'ClientPayloadError',
'ClientResponseError', 'ClientPayloadError',
'ClientHttpProxyError', 'WSServerHandshakeError')


Expand All @@ -37,18 +37,6 @@ def __init__(self, request_info, history, *,
super().__init__("%s, message='%s'" % (code, message))


class ClientRedirectError(ClientResponseError):
"""Redirection error.

Response is a redirect but Location or URI HTTP headers are
missing

"""
def __init__(self, request_info, history, code):
super().__init__(request_info, history, code=code,
message="Response has no Location or URI header")


class WSServerHandshakeError(ClientResponseError):
"""websocket server handshake error."""

Expand Down
3 changes: 0 additions & 3 deletions changes/2009.feature

This file was deleted.

2 changes: 2 additions & 0 deletions changes/2030.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Responses to redirects without Location header are returned instead of raising
a RuntimeError
7 changes: 0 additions & 7 deletions docs/client_reference.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1447,12 +1447,6 @@ Response errors
handle redirection responses.


.. class:: ClientRedirectError

Response is a redirect but ``Location`` or ``URI`` headers are missing.

Derived from :exc:`ClientResponseError`

.. class:: WSServerHandshakeError

Web socket server response error.
Expand Down Expand Up @@ -1540,7 +1534,6 @@ Hierarchy of exceptions

* :exc:`ClientResponseError`

* :exc:`ClientRedirectError`
* :exc:`WSServerHandshakeError`
* :exc:`ClientHttpProxyError`

Expand Down
13 changes: 7 additions & 6 deletions tests/test_client_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from multidict import MultiDict

import aiohttp
from aiohttp import ClientRedirectError, ServerFingerprintMismatch, hdrs, web
from aiohttp import ServerFingerprintMismatch, hdrs, web
from aiohttp.helpers import create_future
from aiohttp.multipart import MultipartWriter

Expand Down Expand Up @@ -2097,18 +2097,19 @@ def redirect(request):

@asyncio.coroutine
def test_redirect_without_location_header(loop, test_client):
body = b'redirect'

@asyncio.coroutine
def handler_redirect(request):
return web.Response(status=301)
return web.Response(status=301, body=body)

app = web.Application()
app.router.add_route('GET', '/redirect', handler_redirect)
client = yield from test_client(app)

with pytest.raises(ClientRedirectError) as ctx:
yield from client.get('/redirect')
expected_msg = 'Response has no Location or URI header'
assert str(ctx.value.message) == expected_msg
resp = yield from client.get('/redirect')
data = yield from resp.read()
assert data == body


@asyncio.coroutine
Expand Down