A get with an invalid port should wrap urllib3's LocationParseError exception with our own.
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Chris Dary · committed by Chris Dary · parent 8a055c5 · Browse files · 3 files·+21 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+21 added−8 removed
- tests:+7 added−0 removed
- other:+14 added−8 removed
Provenance
- Commit
3a2eaa6- Tree
800c290- Author
Chris Dary <umbrae@gmail.com>- Committer
Chris Dary <umbrae@gmail.com>- Parent
8a055c5
3 files changed+21 added−8 removed
modified:requests/exceptions.py+4 added-1 removed
33 unmodified lines34 class MissingSchema(RequestException, ValueError):35 """The URL schema (e.g. http or https) is missing."""36 37 class InvalidSchema(RequestException, ValueError):38- """See defaults.py for valid schemas."""38+ """See defaults.py for valid schemas."""39+ 40+class InvalidURL(RequestException, ValueError):41+ """ The URL provided was somehow invalid. """modified:requests/models.py+10 added-7 removed
14 unmodified lines15 from .status_codes import codes16 17 from .auth import HTTPBasicAuth, HTTPProxyAuth18 from .packages.urllib3.response import HTTPResponse19-from .packages.urllib3.exceptions import MaxRetryError19+from .packages.urllib3.exceptions import MaxRetryError, LocationParseError20 from .packages.urllib3.exceptions import SSLError as _SSLError21 from .packages.urllib3.exceptions import HTTPError as _HTTPError22 from .packages.urllib3 import connectionpool, poolmanager23 from .packages.urllib3.filepost import encode_multipart_formdata24 from .defaults import SCHEMAS25 from .exceptions import (26 ConnectionError, HTTPError, RequestException, Timeout, TooManyRedirects,27- URLRequired, SSLError, MissingSchema, InvalidSchema)27+ URLRequired, SSLError, MissingSchema, InvalidSchema, InvalidURL)28 from .utils import (29 get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri,30 dict_from_string, stream_decode_response_unicode, get_netrc_auth)31 from .compat import (464 unmodified lines496 r = self.proxy_auth(self)497 self.__dict__.update(r.__dict__)498 else:499 # Check to see if keep_alive is allowed.500+ try:500- if self.config.get('keep_alive'):501- conn = self._poolmanager.connection_from_url(url)502- else:503- conn = connectionpool.connection_from_url(url)504-501+ if self.config.get('keep_alive'):502+ conn = self._poolmanager.connection_from_url(url)503+ else:504+ conn = connectionpool.connection_from_url(url)505+ except LocationParseError as e:506+ raise InvalidURL(e)507+ 508 if url.startswith('https') and self.verify:509 510 cert_loc = None511 310 unmodified linesmodified:tests/test_requests.py+7 added
779 unmodified lines780 def test_upload_binary_data(self):781 782 requests.get(httpbin('post'), auth=('a', 'b'), data='\xff')783 784+ def test_useful_exception_for_invalid_port(self):785+ # If we pass a legitimate URL with an invalid port, we should fail.786+ self.assertRaises(787+ ValueError,788+ get,789+ 'http://google.com:banana')790+791 def test_useful_exception_for_invalid_scheme(self):792 793 # If we pass a legitimate URL with a scheme not supported794 # by requests, we should fail.29 unmodified lines