A get with an invalid port should wrap urllib3's LocationParseError exception with...
authorChris Dary <umbrae@gmail.com>
Thu, 12 Apr 2012 16:35:34 +0000 (12:35 -0400)
committerChris Dary <umbrae@gmail.com>
Thu, 12 Apr 2012 16:35:34 +0000 (12:35 -0400)
requests/exceptions.py
requests/models.py
tests/test_requests.py

index 3c262e3..1cffa80 100644 (file)
@@ -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
index e117cb0..8eb7fae 100644 (file)
@@ -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
index 7ca30ba..cae344d 100755 (executable)
@@ -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