Skip to content

Commit

Permalink
wrap url parsing exceptions from urllib3's PoolManager
Browse files Browse the repository at this point in the history
  • Loading branch information
nateprewitt committed Sep 30, 2018
1 parent 8f20b7d commit f3ec42f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
8 changes: 6 additions & 2 deletions HISTORY.md
Expand Up @@ -4,8 +4,12 @@ Release History
dev
---

**Bugfixes** - Content-Type header parsing is now case-insensitive (e.g.
charset=utf8 v Charset=utf8).
**Bugfixes**

- Content-Type header parsing is now case-insensitive (e.g.
charset=utf8 v Charset=utf8).
- Fixed exception leak where certain redirect urls would raise
uncaught urllib3 exceptions.

- \[Short description of non-trivial change.\]

Expand Down
9 changes: 7 additions & 2 deletions requests/adapters.py
Expand Up @@ -26,6 +26,7 @@
from urllib3.exceptions import ReadTimeoutError
from urllib3.exceptions import SSLError as _SSLError
from urllib3.exceptions import ResponseError
from urllib3.exceptions import LocationValueError

from .models import Response
from .compat import urlparse, basestring
Expand All @@ -35,7 +36,8 @@
from .structures import CaseInsensitiveDict
from .cookies import extract_cookies_to_jar
from .exceptions import (ConnectionError, ConnectTimeout, ReadTimeout, SSLError,
ProxyError, RetryError, InvalidSchema, InvalidProxyURL)
ProxyError, RetryError, InvalidSchema, InvalidProxyURL,
InvalidURL)
from .auth import _basic_auth_str

try:
Expand Down Expand Up @@ -407,7 +409,10 @@ def send(self, request, stream=False, timeout=None, verify=True, cert=None, prox
:rtype: requests.Response
"""

conn = self.get_connection(request.url, proxies)
try:
conn = self.get_connection(request.url, proxies)
except LocationValueError as e:
raise InvalidURL(e, request=request)

self.cert_verify(conn, request.url, verify, cert)
url = self.request_url(request, proxies)
Expand Down
10 changes: 10 additions & 0 deletions tests/test_requests.py
Expand Up @@ -2426,6 +2426,16 @@ def test_preparing_bad_url(self, url):
with pytest.raises(requests.exceptions.InvalidURL):
r.prepare()

@pytest.mark.parametrize(
'url, exception',
(
('http://localhost:-1', InvalidURL),
)
)
def test_redirecting_to_bad_url(self, httpbin, url, exception):
with pytest.raises(exception):
r = requests.get(httpbin('redirect-to'), params={'url': url})

@pytest.mark.parametrize(
'input, expected',
(
Expand Down

0 comments on commit f3ec42f

Please sign in to comment.