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 Aug 11, 2018
1 parent 922aa40 commit 227eac2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 2 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ dev

**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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -2405,6 +2405,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 227eac2

Please sign in to comment.