From 087a27aba97d3eac017d321e37bf1970f8833c1a Mon Sep 17 00:00:00 2001 From: ContinuousFunction Date: Sat, 15 Nov 2014 16:58:25 -0800 Subject: [PATCH 1/3] Partially addresses Issue #1572 Addresses the LocationParseError but not the DecodeError from kennethreitz#1572. When running test_requests.py, I got an error in test_session_pickling which resulted in a TypeError. I'm not sure of the reason for the TypeError but I have commented out that test. --- requests/models.py | 7 +++++-- test_requests.py | 23 ++++++++++++++--------- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/requests/models.py b/requests/models.py index 2370b67f..b95b5beb 100644 --- a/requests/models.py +++ b/requests/models.py @@ -20,7 +20,7 @@ from .packages.urllib3.fields import RequestField from .packages.urllib3.filepost import encode_multipart_formdata from .packages.urllib3.util import parse_url from .packages.urllib3.exceptions import ( - DecodeError, ReadTimeoutError, ProtocolError) + DecodeError, ReadTimeoutError, ProtocolError, LocationParseError) from .exceptions import ( HTTPError, RequestException, MissingSchema, InvalidURL, ChunkedEncodingError, ContentDecodingError, ConnectionError, @@ -351,7 +351,10 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): return # Support for unicode domain names and paths. - scheme, auth, host, port, path, query, fragment = parse_url(url) + try: + scheme, auth, host, port, path, query, fragment = parse_url(url) + except LocationParseError as e: + raise ConnectionError(e.message) if not scheme: raise MissingSchema("Invalid URL {0!r}: No schema supplied. " diff --git a/test_requests.py b/test_requests.py index 4a05cb2e..6e49f027 100755 --- a/test_requests.py +++ b/test_requests.py @@ -309,6 +309,11 @@ class RequestsTestCase(unittest.TestCase): with pytest.raises(ConnectionError): requests.get("http://httpbin.org:1") + def test_LocationParseError(self): + """Inputing a URL that cannot be parsed should raise a ConnectionError""" + with pytest.raises(ConnectionError): + requests.get("http://fe80::5054:ff:fe5a:fc0") + def test_basicauth_with_netrc(self): auth = ('user', 'pass') wrong_auth = ('wronguser', 'wrongpass') @@ -820,15 +825,15 @@ class RequestsTestCase(unittest.TestCase): assert str(error) == 'message' assert error.response == response - def test_session_pickling(self): - r = requests.Request('GET', httpbin('get')) - s = requests.Session() - - s = pickle.loads(pickle.dumps(s)) - s.proxies = getproxies() - - r = s.send(r.prepare()) - assert r.status_code == 200 +## def test_session_pickling(self): +## r = requests.Request('GET', httpbin('get')) +## s = requests.Session() +## +## s = pickle.loads(pickle.dumps(s)) +## s.proxies = getproxies() +## +## r = s.send(r.prepare()) +## assert r.status_code == 200 def test_fixes_1329(self): """ From 3246b1fe172ca3d4f098e85467234ded2f833b31 Mon Sep 17 00:00:00 2001 From: ContinuousFunction Date: Sun, 16 Nov 2014 16:39:08 -0800 Subject: [PATCH 2/3] Changed ConnectionError to InvalidURL --- requests/models.py | 2 +- test_requests.py | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/requests/models.py b/requests/models.py index b95b5beb..8a71e28b 100644 --- a/requests/models.py +++ b/requests/models.py @@ -354,7 +354,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin): try: scheme, auth, host, port, path, query, fragment = parse_url(url) except LocationParseError as e: - raise ConnectionError(e.message) + raise InvalidURL(e.message) if not scheme: raise MissingSchema("Invalid URL {0!r}: No schema supplied. " diff --git a/test_requests.py b/test_requests.py index 6e49f027..d176ef45 100755 --- a/test_requests.py +++ b/test_requests.py @@ -310,8 +310,8 @@ class RequestsTestCase(unittest.TestCase): requests.get("http://httpbin.org:1") def test_LocationParseError(self): - """Inputing a URL that cannot be parsed should raise a ConnectionError""" - with pytest.raises(ConnectionError): + """Inputing a URL that cannot be parsed should raise an InvalidURL error""" + with pytest.raises(InvalidURL): requests.get("http://fe80::5054:ff:fe5a:fc0") def test_basicauth_with_netrc(self): From 4c61fef13f53db220f95032b72e6e374970bf272 Mon Sep 17 00:00:00 2001 From: ContinuousFunction Date: Mon, 15 Dec 2014 13:41:10 -0500 Subject: [PATCH 3/3] Uncommented test in test_requests.py Uncommented test_sesion_pickling in test_requests.py and ran the file to make sure the test passes. --- test_requests.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/test_requests.py b/test_requests.py index 51d071e5..34348d3e 100755 --- a/test_requests.py +++ b/test_requests.py @@ -825,15 +825,15 @@ class RequestsTestCase(unittest.TestCase): assert str(error) == 'message' assert error.response == response -## def test_session_pickling(self): -## r = requests.Request('GET', httpbin('get')) -## s = requests.Session() -## -## s = pickle.loads(pickle.dumps(s)) -## s.proxies = getproxies() -## -## r = s.send(r.prepare()) -## assert r.status_code == 200 + def test_session_pickling(self): + r = requests.Request('GET', httpbin('get')) + s = requests.Session() + + s = pickle.loads(pickle.dumps(s)) + s.proxies = getproxies() + + r = s.send(r.prepare()) + assert r.status_code == 200 def test_fixes_1329(self): """