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

bug with invalid redir #1396

Closed
wumpus opened this issue Nov 13, 2016 · 5 comments
Closed

bug with invalid redir #1396

wumpus opened this issue Nov 13, 2016 · 5 comments
Labels

Comments

@wumpus
Copy link

wumpus commented Nov 13, 2016

This host lacks a Location: in this redir -- a not unusual thing on the Internets:

$ curl -D /dev/tty https://www.enterprisecarshare.com/robots.txt
HTTP/1.1 302 Moved Temporarily
Content-Type: text/html;charset=UTF-8
Dispatcher: dispatcher 1d
Server: Apache
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
Content-Length: 0
Cache-Control: max-age=168018
Expires: Tue, 15 Nov 2016 02:30:09 GMT
Date: Sun, 13 Nov 2016 03:49:51 GMT
Connection: keep-alive

I updated to 1.1.0, and if there's allow_redirects=True the code now calls yarl.URL(None) which throws

 TypeError: Constructor parameter should be str

which is not so super-useful to the programmer! May I suggest instead looking to see if we're about to pass None into yarl.URL and in this case raise ValueError(url + ' is a redirect but lacks a Location: or URI: header')

$ python bug.py
Traceback (most recent call last):
  File "bug.py", line 11, in <module>
    loop.run_until_complete(main(url))
  File "/home/lindahl/.pyenv/versions/3.5.2/lib/python3.5/asyncio/base_events.py", line 387, in run_until_complete
    return future.result()
  File "/home/lindahl/.pyenv/versions/3.5.2/lib/python3.5/asyncio/futures.py", line 274, in result
    raise self._exception
  File "/home/lindahl/.pyenv/versions/3.5.2/lib/python3.5/asyncio/tasks.py", line 239, in _step
    result = coro.send(None)
  File "bug.py", line 6, in main
    async with session.get(url, allow_redirects=True) as response:
  File "/home/lindahl/.pyenv/versions/3.5.2/lib/python3.5/site-packages/aiohttp/client.py", line 558, in __aenter__
    self._resp = yield from self._coro
  File "/home/lindahl/.pyenv/versions/3.5.2/lib/python3.5/site-packages/aiohttp/client.py", line 245, in _request
    resp.headers.get(hdrs.URI))
  File "/home/lindahl/.pyenv/versions/3.5.2/lib/python3.5/site-packages/yarl/__init__.py", line 150, in __init__
    raise TypeError("Constructor parameter should be str")
TypeError: Constructor parameter should be str
$ more bug.py
import asyncio
import aiohttp

async def main(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url, allow_redirects=True) as response:
            await response.read()

url = 'https://www.enterprisecarshare.com/robots.txt'
loop = asyncio.get_event_loop()
loop.run_until_complete(main(url))
@asvetlov
Copy link
Member

Makes sense.

Would you make a Pull Request?

@asvetlov
Copy link
Member

asvetlov commented Nov 13, 2016

Fixed by f137b18

Now it raises RuntimeError with text like 'GET http://127.0.0.1:56346/redirect returns a redirect [301] status but response lacks a Location or URI HTTP header'.
Exception type is RuntimeError because ValueError is about wrong value passed to function but for this case real error source is a server answer.

@wumpus is error text looks good and informative enough?

asvetlov added a commit that referenced this issue Nov 13, 2016
@wumpus
Copy link
Author

wumpus commented Nov 13, 2016

Thank you, that looks perfect.

(Thanks for the test example, I haven't gotten through the learning curve of your test suite yet)

@wumpus wumpus closed this as completed Nov 13, 2016
@asvetlov
Copy link
Member

The test suite is based on pytest tool with adding several pytest fixtures and hooks.
It's described by http://aiohttp.readthedocs.io/en/stable/testing.html but I pretty sure the chapter is not perfect. It needs a love for polishing.

Please make a Pull Request (or just an issue) with proposal for solving dark documentation's corners.

I really need feedbacks for documentation.
Maybe somebody will become an awesome docs writer for the project but now it's just a dream.

@lock
Copy link

lock bot commented Oct 29, 2019

This thread has been automatically locked since there has not been
any recent activity after it was closed. Please open a new issue for
related bugs.

If you feel like there's important points made in this discussion,
please include those exceprts into that new issue.

@lock lock bot added the outdated label Oct 29, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Oct 29, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants