Skip to content

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

py3: fix webclient tests after making ScrapyHTTPClientFactory use bytes as in twisted

Konstantin Lopuhin5c2241ccc7a8
diff --git a/scrapy/core/downloader/webclient.py b/scrapy/core/downloader/webclient.py
index 15d14ae49..d2cdd6f98 100644
--- a/scrapy/core/downloader/webclient.py
+++ b/scrapy/core/downloader/webclient.py
@@ -65,7 +65,7 @@ class ScrapyHTTPPageGetter(HTTPClient):
self.factory.noPage(reason)
def handleResponse(self, response):
- if self.factory.method.upper() == 'HEAD':
+ if self.factory.method.upper() == b'HEAD':
self.factory.page('')
elif self.length is not None and self.length > 0:
self.factory.noPage(self._connection_lost_reason)
@@ -123,7 +123,7 @@ class ScrapyHTTPClientFactory(HTTPClientFactory):
# just in case a broken http/1.1 decides to keep connection alive
self.headers.setdefault("Connection", "close")
# Content-Length must be specified in POST method even with no body
- elif self.method == 'POST':
+ elif self.method == b'POST':
self.headers['Content-Length'] = 0
def _build_response(self, body, request):
diff --git a/tests/test_webclient.py b/tests/test_webclient.py
index 66f8ed4cf..412e10c89 100644
--- a/tests/test_webclient.py
+++ b/tests/test_webclient.py
@@ -68,6 +68,8 @@ class ParseUrlTestCase(unittest.TestCase):
)
for url, test in tests:
+ test = tuple(
+ to_bytes(x) if not isinstance(x, int) else x for x in test)
self.assertEquals(client._parse(url), test, url)
def test_externalUnicodeInterference(self):
@@ -82,10 +84,10 @@ class ParseUrlTestCase(unittest.TestCase):
goodInput, badInput = badInput, goodInput
urlparse(badInput)
scheme, netloc, host, port, path = self._parse(goodInput)
- self.assertTrue(isinstance(scheme, str))
- self.assertTrue(isinstance(netloc, str))
- self.assertTrue(isinstance(host, str))
- self.assertTrue(isinstance(path, str))
+ self.assertTrue(isinstance(scheme, bytes))
+ self.assertTrue(isinstance(netloc, bytes))
+ self.assertTrue(isinstance(host, bytes))
+ self.assertTrue(isinstance(path, bytes))
self.assertTrue(isinstance(port, int))
@@ -317,9 +319,9 @@ class WebClientTestCase(unittest.TestCase):
def testFactoryInfo(self):
url = self.getURL('file')
- scheme, netloc, host, port, path = client._parse(url)
+ _, _, host, port, _ = client._parse(url)
factory = client.ScrapyHTTPClientFactory(Request(url))
- reactor.connectTCP(host, port, factory)
+ reactor.connectTCP(to_unicode(host), port, factory)
return factory.deferred.addCallback(self._cbFactoryInfo, factory)
def _cbFactoryInfo(self, ignoredResult, factory):