Skip to content

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

Fix WrappedRequest.get_header raising TypeError if default is None (#6310)

Víctor Ruiz48c5a8c98f54
diff --git a/scrapy/http/cookies.py b/scrapy/http/cookies.py
index 2595f328e..72855bad5 100644
--- a/scrapy/http/cookies.py
+++ b/scrapy/http/cookies.py
@@ -170,7 +170,8 @@ class WrappedRequest:
return name in self.request.headers
def get_header(self, name, default=None):
- return to_unicode(self.request.headers.get(name, default), errors="replace")
+ value = self.request.headers.get(name, default)
+ return to_unicode(value, errors="replace") if value is not None else None
def header_items(self):
return [
diff --git a/tests/test_http_cookies.py b/tests/test_http_cookies.py
index 8b5554914..932644320 100644
--- a/tests/test_http_cookies.py
+++ b/tests/test_http_cookies.py
@@ -43,6 +43,13 @@ class WrappedRequestTest(TestCase):
def test_get_header(self):
self.assertEqual(self.wrapped.get_header("content-type"), "text/html")
self.assertEqual(self.wrapped.get_header("xxxxx", "def"), "def")
+ self.assertEqual(self.wrapped.get_header("xxxxx"), None)
+ wrapped = WrappedRequest(
+ Request(
+ "http://www.example.com/page.html", headers={"empty-binary-header": b""}
+ )
+ )
+ self.assertEqual(wrapped.get_header("empty-binary-header"), "")
def test_header_items(self):
self.assertEqual(self.wrapped.header_items(), [("Content-Type", ["text/html"])])