From 640538adcb81fd5930eadb1ecf47dcb8a0a62ab3 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 16 Feb 2012 20:20:20 +0000 Subject: [PATCH] Fail if unsupported schemas are used. requests only supports http and https. This change enforces that. --- requests/models.py | 5 ++++- requests/utils.py | 7 +++++++ tests/test_requests.py | 13 +++++-------- 3 files changed, 16 insertions(+), 9 deletions(-) diff --git a/requests/models.py b/requests/models.py index 8ff4ad8b..d1d1aa57 100644 --- a/requests/models.py +++ b/requests/models.py @@ -26,7 +26,7 @@ from .exceptions import ( URLRequired, SSLError) from .utils import ( get_encoding_from_headers, stream_untransfer, guess_filename, requote_uri, - dict_from_string) + dict_from_string, supported_schemes) from .compat import urlparse, urlunparse, urljoin, urlsplit, urlencode, quote, unquote, str, bytes, SimpleCookie, is_py3, is_py2 @@ -316,6 +316,9 @@ class Request(object): if not scheme: raise ValueError("Invalid URL %r: No schema supplied" % url) + if not scheme in supported_schemes(): + raise ValueError("Invalid scheme %r" % scheme) + netloc = netloc.encode('idna').decode('utf-8') if not path: diff --git a/requests/utils.py b/requests/utils.py index d35b332b..c0900cf8 100644 --- a/requests/utils.py +++ b/requests/utils.py @@ -416,3 +416,10 @@ def requote_uri(uri): # or '%') return quote(unquote_unreserved(uri), safe="!#$%&'()*+,/:;=?@[]~") return "/".join(parts) + +def supported_schemes(): + """A list of schemes supported by requests. + + return: a list of strings. + """ + return ["http","https"] diff --git a/tests/test_requests.py b/tests/test_requests.py index de89f68c..196e5607 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -815,15 +815,12 @@ class RequestsTestSuite(TestSetup, unittest.TestCase): def test_useful_exception_for_invalid_schema(self): - try: - self.assertRaises( - requests.exceptions.URLRequired, + # If we pass a legitimate URL with a schema not supported + # by requests, we should fail. + self.assertRaises( + ValueError, get, - 'http://http://') - # To make this test as minimal as possible, only catch the - # exception raised in issue #380. - except ValueError: - self.fail() + 'ftp://ftp.kernel.org/pub/')