From: Chris Dary Date: Thu, 12 Apr 2012 16:35:34 +0000 (-0400) Subject: A get with an invalid port should wrap urllib3's LocationParseError exception with... X-Git-Tag: v0.12.0~52^2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3a2eaa67a2931024c1caafe9395469a25c9b5ccb;p=services%2Fpython-requests.git A get with an invalid port should wrap urllib3's LocationParseError exception with our own. --- diff --git a/requests/exceptions.py b/requests/exceptions.py index 3c262e3..1cffa80 100644 --- a/requests/exceptions.py +++ b/requests/exceptions.py @@ -35,4 +35,7 @@ class MissingSchema(RequestException, ValueError): """The URL schema (e.g. http or https) is missing.""" class InvalidSchema(RequestException, ValueError): - """See defaults.py for valid schemas.""" \ No newline at end of file + """See defaults.py for valid schemas.""" + +class InvalidURL(RequestException, ValueError): + """ The URL provided was somehow invalid. """ \ No newline at end of file diff --git a/requests/models.py b/requests/models.py index e117cb0..8eb7fae 100644 --- a/requests/models.py +++ b/requests/models.py @@ -16,7 +16,7 @@ from .status_codes import codes from .auth import HTTPBasicAuth, HTTPProxyAuth from .packages.urllib3.response import HTTPResponse -from .packages.urllib3.exceptions import MaxRetryError +from .packages.urllib3.exceptions import MaxRetryError, LocationParseError from .packages.urllib3.exceptions import SSLError as _SSLError from .packages.urllib3.exceptions import HTTPError as _HTTPError from .packages.urllib3 import connectionpool, poolmanager @@ -24,7 +24,7 @@ from .packages.urllib3.filepost import encode_multipart_formdata from .defaults import SCHEMAS from .exceptions import ( ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects, - URLRequired, SSLError, MissingSchema, InvalidSchema) + URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL) from .utils import ( get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri, dict_from_string, stream_decode_response_unicode, get_netrc_auth) @@ -497,11 +497,14 @@ class Request(object): self.__dict__.update(r.__dict__) else: # Check to see if keep_alive is allowed. - if self.config.get('keep_alive'): - conn = self._poolmanager.connection_from_url(url) - else: - conn = connectionpool.connection_from_url(url) - + try: + if self.config.get('keep_alive'): + conn = self._poolmanager.connection_from_url(url) + else: + conn = connectionpool.connection_from_url(url) + except LocationParseError as e: + raise InvalidURL(e) + if url.startswith('https') and self.verify: cert_loc = None diff --git a/tests/test_requests.py b/tests/test_requests.py index 7ca30ba..cae344d 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -781,6 +781,13 @@ class RequestsTestSuite(TestSetup, unittest.TestCase): requests.get(httpbin('post'), auth=('a', 'b'), data='\xff') + def test_useful_exception_for_invalid_port(self): + # If we pass a legitimate URL with an invalid port, we should fail. + self.assertRaises( + ValueError, + get, + 'http://google.com:banana') + def test_useful_exception_for_invalid_scheme(self): # If we pass a legitimate URL with a scheme not supported