From 915f40fa817f5f032939e01a8e586d3dd927c802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=ADctor=20Ruiz?= Date: Mon, 8 Apr 2024 11:47:53 +0200 Subject: [PATCH] Fix WrappedRequest.get_header raising TypeError if default is None (#6310) --- scrapy/http/cookies.py | 3 ++- tests/test_http_cookies.py | 7 +++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/scrapy/http/cookies.py b/scrapy/http/cookies.py index a5329ad51..15f25f69d 100644 --- a/scrapy/http/cookies.py +++ b/scrapy/http/cookies.py @@ -166,7 +166,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 9e43b72b0..db1c816d4 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"])])