Skip to content

Commit da29568

Browse files
authored
Cleanup deprecated header handling (#362)
1 parent e7dbf9c commit da29568

File tree

2 files changed

+2
-63
lines changed

2 files changed

+2
-63
lines changed

scrapy_playwright/handler.py

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
import asyncio
2-
import inspect
32
import logging
43
import platform
5-
import warnings
64
from contextlib import suppress
75
from dataclasses import dataclass, field as dataclass_field
86
from functools import partial
@@ -26,7 +24,7 @@
2624
from scrapy import Spider, signals, version_info as scrapy_version_info
2725
from scrapy.core.downloader.handlers.http11 import HTTP11DownloadHandler
2826
from scrapy.crawler import Crawler
29-
from scrapy.exceptions import NotSupported, ScrapyDeprecationWarning
27+
from scrapy.exceptions import NotSupported
3028
from scrapy.http import Request, Response
3129
from scrapy.http.headers import Headers
3230
from scrapy.responsetypes import responsetypes
@@ -782,11 +780,7 @@ async def _request_handler(route: Route, playwright_request: PlaywrightRequest)
782780

783781
if self.process_request_headers is None:
784782
final_headers = await playwright_request.all_headers()
785-
elif (sig := inspect.signature(self.process_request_headers)) and (
786-
"browser_type_name" in sig.parameters
787-
and "playwright_request" in sig.parameters
788-
and "scrapy_request_data" in sig.parameters
789-
):
783+
else:
790784
overrides["headers"] = final_headers = await _maybe_await(
791785
self.process_request_headers(
792786
browser_type_name=self.config.browser_type_name,
@@ -800,24 +794,6 @@ async def _request_handler(route: Route, playwright_request: PlaywrightRequest)
800794
},
801795
)
802796
)
803-
else:
804-
warnings.warn(
805-
"Accepting positional arguments in the function passed to the"
806-
" PLAYWRIGHT_PROCESS_REQUEST_HEADERS setting is deprecated. The function"
807-
" should accept three (3) keyword arguments instead:"
808-
" browser_type_name: str,"
809-
" playwright_request: playwright.async_api.Request,"
810-
" scrapy_request_data: dict",
811-
category=ScrapyDeprecationWarning,
812-
stacklevel=1,
813-
)
814-
overrides["headers"] = final_headers = await _maybe_await(
815-
self.process_request_headers(
816-
self.config.browser_type_name,
817-
playwright_request,
818-
headers,
819-
)
820-
)
821797

822798
# if the current request corresponds to the original scrapy one
823799
if (

tests/tests_asyncio/test_headers.py

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -104,43 +104,6 @@ async def important_headers(
104104
assert headers.get("user-agent") not in (self.browser_type, "foobar")
105105
assert "asdf" not in headers
106106

107-
@allow_windows
108-
async def test_use_custom_headers_deprecated_arg_handling(self):
109-
"""Custom header processing function that receives deprecated args"""
110-
111-
async def deprecated_args(
112-
browser_name, pw_req, headers # pylint: disable=unused-argument
113-
) -> dict:
114-
return {"foo": "bar"}
115-
116-
settings_dict = {
117-
"PLAYWRIGHT_BROWSER_TYPE": self.browser_type,
118-
"PLAYWRIGHT_CONTEXTS": {"default": {"user_agent": self.browser_type}},
119-
"PLAYWRIGHT_PROCESS_REQUEST_HEADERS": deprecated_args,
120-
}
121-
async with make_handler(settings_dict) as handler:
122-
with MockServer() as server:
123-
req = Request(
124-
url=server.urljoin("/headers"),
125-
meta={"playwright": True},
126-
headers={"User-Agent": "foobar", "Asdf": "qwerty"},
127-
)
128-
with warnings.catch_warnings(record=True) as warning_list:
129-
resp = await handler._download_request(req, Spider("foo"))
130-
headers = json.loads(resp.css("pre::text").get())
131-
headers = {key.lower(): value for key, value in headers.items()}
132-
assert headers["foo"] == "bar"
133-
assert headers.get("user-agent") not in (self.browser_type, "foobar")
134-
assert "asdf" not in headers
135-
assert str(warning_list[0].message) == (
136-
"Accepting positional arguments in the function passed to the"
137-
" PLAYWRIGHT_PROCESS_REQUEST_HEADERS setting is deprecated. The function"
138-
" should accept three (3) keyword arguments instead:"
139-
" browser_type_name: str,"
140-
" playwright_request: playwright.async_api.Request,"
141-
" scrapy_request_data: dict"
142-
)
143-
144107

145108
class TestProcessHeadersChromium(IsolatedAsyncioTestCase, MixinProcessHeadersTestCase):
146109
browser_type = "chromium"

0 commit comments

Comments
 (0)