From d339b0f64fb2ea53519366729371b3b497dd2286 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 16 Feb 2012 18:21:32 +0000 Subject: [PATCH 1/4] Added failing test for issue #380. --- tests/test_requests.py | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index 76df752a..14e76576 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -23,8 +23,7 @@ from requests.auth import HTTPBasicAuth, HTTPDigestAuth -if (sys.platform == 'win32') and ('HTTPBIN_URL' not in os.environ): - os.environ['HTTPBIN_URL'] = 'http://httpbin.org/' +os.environ['HTTPBIN_URL'] = 'http://httpbin.org/' # TODO: Detect an open port. PORT = os.environ.get('HTTPBIN_PORT', '7077') @@ -813,6 +812,18 @@ class RequestsTestSuite(TestSetup, unittest.TestCase): r = requests.get(httpbin('post'), auth=('a', 'b'), data='\xff') + def test_useful_exception_for_invalid_schema(self): + + try: + self.assertRaises( + requests.exceptions.URLRequired, + get, + 'http://http://') + # To make this test as minimal as possible, only catch the + # exception raised in issue #380. + except ValueError: + self.fail() + if __name__ == '__main__': From 1ded0ad3d55a100234f0f06e37315bd65664e8f5 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 16 Feb 2012 19:32:20 +0000 Subject: [PATCH 2/4] Remove a wayward change. --- tests/test_requests.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index 14e76576..de89f68c 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -23,7 +23,8 @@ from requests.auth import HTTPBasicAuth, HTTPDigestAuth -os.environ['HTTPBIN_URL'] = 'http://httpbin.org/' +if (sys.platform == 'win32') and ('HTTPBIN_URL' not in os.environ): + os.environ['HTTPBIN_URL'] = 'http://httpbin.org/' # TODO: Detect an open port. PORT = os.environ.get('HTTPBIN_PORT', '7077') From 640538adcb81fd5930eadb1ecf47dcb8a0a62ab3 Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 16 Feb 2012 20:20:20 +0000 Subject: [PATCH 3/4] 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/') From 544ebf402f50cd7afbbe0e04b553a64872111b6c Mon Sep 17 00:00:00 2001 From: Cory Benfield Date: Thu, 16 Feb 2012 20:42:05 +0000 Subject: [PATCH 4/4] Correct unfortunate typo. --- tests/test_requests.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_requests.py b/tests/test_requests.py index 196e5607..e75dea3a 100755 --- a/tests/test_requests.py +++ b/tests/test_requests.py @@ -813,9 +813,9 @@ class RequestsTestSuite(TestSetup, unittest.TestCase): r = requests.get(httpbin('post'), auth=('a', 'b'), data='\xff') - def test_useful_exception_for_invalid_schema(self): + def test_useful_exception_for_invalid_scheme(self): - # If we pass a legitimate URL with a schema not supported + # If we pass a legitimate URL with a scheme not supported # by requests, we should fail. self.assertRaises( ValueError,