Skip to content

Scrapy, a fast high-level web crawling & scraping framework for Python.

Raise ValueError if url has no scheme in Request constructor

Pablo Hoffmanc4a607fc787b
diff --git a/scrapy/http/request/__init__.py b/scrapy/http/request/__init__.py
index a9d81d8b6..ea6b76858 100644
--- a/scrapy/http/request/__init__.py
+++ b/scrapy/http/request/__init__.py
@@ -60,6 +60,8 @@ class Request(object_ref):
self._url = safe_url_string(unicode_url, self.encoding)
else:
raise TypeError('Request url must be str or unicode, got %s:' % type(url).__name__)
+ if ':' not in self._url:
+ raise ValueError('Missing scheme in request url: %s' % self._url)
url = property(_get_url, deprecated_setter(_set_url, 'url'))
diff --git a/scrapy/tests/test_http_request.py b/scrapy/tests/test_http_request.py
index 841568ed8..d95495617 100644
--- a/scrapy/tests/test_http_request.py
+++ b/scrapy/tests/test_http_request.py
@@ -42,6 +42,9 @@ class RequestTest(unittest.TestCase):
assert r.headers is not headers
self.assertEqual(r.headers["caca"], "coco")
+ def test_url_no_scheme(self):
+ self.assertRaises(ValueError, self.request_class, 'foo')
+
def test_headers(self):
# Different ways of setting headers attribute
url = 'http://www.scrapy.org'