"""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
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
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)
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
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