Skip to content

Commit

Permalink
fix for redirect issue (#2030)
Browse files Browse the repository at this point in the history
* fix for http://github.com/aio-libs/aiohttp/issues/2022

- fixes Makefile to run when you have py2 + py3 installed
- removes ClientRedirectError
- ran isort based on recommendations from make test

* apparently local isort is different than remote isort

* revert changes

* revert change

* add new changes file

* update based on review
  • Loading branch information
thehesiod authored and asvetlov committed Jun 29, 2017
1 parent afbff7b commit 384a183
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 35 deletions.
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

0 comments on commit 384a183

Please sign in to comment.