Fail if unsupported schemas are used.
authorCory Benfield <lukasaoz@gmail.com>
Thu, 16 Feb 2012 20:20:20 +0000 (20:20 +0000)
committerCory Benfield <lukasaoz@gmail.com>
Thu, 16 Feb 2012 20:20:20 +0000 (20:20 +0000)
requests only supports http and https. This change enforces that.

requests/models.py
requests/utils.py
tests/test_requests.py

index 8ff4ad8..d1d1aa5 100644 (file)
@@ -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:
index d35b332..c0900cf 100644 (file)
@@ -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"]
index de89f68..196e560 100755 (executable)
@@ -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/')