Fail if unsupported schemas are used.

requests only supports http and https. This change enforces that.
This commit is contained in:
Cory Benfield
2012-02-16 20:20:20 +00:00
parent 1ded0ad3d5
commit 640538adcb
3 changed files with 16 additions and 9 deletions
+4 -1
View 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:
+7
View 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"]
+5 -8
View File
@@ -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/')