From 1f743996ff00a7b728d59b93d0967e1eb50072f0 Mon Sep 17 00:00:00 2001 From: orangain Date: Sun, 7 Feb 2016 14:19:27 +0900 Subject: [PATCH] PY3: Implement some attributes of WrappedRequest required in Python 3 This will fix #1770. --- scrapy/http/cookies.py | 22 +++++++++++++++++++--- tests/test_http_cookies.py | 4 ++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/scrapy/http/cookies.py b/scrapy/http/cookies.py index e92c3fe73..a1e95102e 100644 --- a/scrapy/http/cookies.py +++ b/scrapy/http/cookies.py @@ -137,13 +137,29 @@ class WrappedRequest(object): """ return self.request.meta.get('is_unverifiable', False) - # python3 uses request.unverifiable + def get_origin_req_host(self): + return urlparse_cached(self.request).hostname + + # python3 uses attributes instead of methods + @property + def full_url(self): + return self.get_full_url() + + @property + def host(self): + return self.get_host() + + @property + def type(self): + return self.get_type() + @property def unverifiable(self): return self.is_unverifiable() - def get_origin_req_host(self): - return urlparse_cached(self.request).hostname + @property + def origin_req_host(self): + return self.get_origin_req_host() def has_header(self, name): return name in self.request.headers diff --git a/tests/test_http_cookies.py b/tests/test_http_cookies.py index d529f609b..549f779d8 100644 --- a/tests/test_http_cookies.py +++ b/tests/test_http_cookies.py @@ -14,12 +14,15 @@ class WrappedRequestTest(TestCase): def test_get_full_url(self): self.assertEqual(self.wrapped.get_full_url(), self.request.url) + self.assertEqual(self.wrapped.full_url, self.request.url) def test_get_host(self): self.assertEqual(self.wrapped.get_host(), urlparse(self.request.url).netloc) + self.assertEqual(self.wrapped.host, urlparse(self.request.url).netloc) def test_get_type(self): self.assertEqual(self.wrapped.get_type(), urlparse(self.request.url).scheme) + self.assertEqual(self.wrapped.type, urlparse(self.request.url).scheme) def test_is_unverifiable(self): self.assertFalse(self.wrapped.is_unverifiable()) @@ -32,6 +35,7 @@ class WrappedRequestTest(TestCase): def test_get_origin_req_host(self): self.assertEqual(self.wrapped.get_origin_req_host(), 'www.example.com') + self.assertEqual(self.wrapped.origin_req_host, 'www.example.com') def test_has_header(self): self.assertTrue(self.wrapped.has_header('content-type'))