Skip to content

Commit

Permalink
Override method and/or body only for the first matching request (#297)
Browse files Browse the repository at this point in the history
  • Loading branch information
elacuesta authored Jul 11, 2024
1 parent e26d3c1 commit 1e3f184
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions scrapy_playwright/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,13 @@ async def _download_request(self, request: Request, spider: Spider) -> Response:
page=page, request=request, spider=spider, context_name=context_name
)

# We need to identify the Playwright request that matches the Scrapy request
# in order to override method and body if necessary.
# Checking the URL and Request.is_navigation_request() is not enough, e.g.
# requests produced by submitting forms can produce false positives.
# Let's track only the first request that matches the above conditions.
initial_request_done = asyncio.Event()

await page.unroute("**")
await page.route(
"**",
Expand All @@ -375,6 +382,7 @@ async def _download_request(self, request: Request, spider: Spider) -> Response:
body=request.body,
encoding=request.encoding,
spider=spider,
initial_request_done=initial_request_done,
),
)

Expand Down Expand Up @@ -652,6 +660,7 @@ def _make_request_handler(
body: Optional[bytes],
encoding: str,
spider: Spider,
initial_request_done: asyncio.Event,
) -> Callable:
async def _request_handler(route: Route, playwright_request: PlaywrightRequest) -> None:
"""Override request headers, method and body."""
Expand Down Expand Up @@ -691,7 +700,9 @@ async def _request_handler(route: Route, playwright_request: PlaywrightRequest)
if (
playwright_request.url.rstrip("/") == url.rstrip("/")
and playwright_request.is_navigation_request()
and not initial_request_done.is_set()
):
initial_request_done.set()
if method.upper() != playwright_request.method.upper():
overrides["method"] = method
if body:
Expand Down
3 changes: 3 additions & 0 deletions tests/tests_asyncio/test_playwright_requests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import json
import logging
import platform
Expand Down Expand Up @@ -112,6 +113,7 @@ async def test_route_continue_exception(self, logger):
async with make_handler({"PLAYWRIGHT_BROWSER_TYPE": self.browser_type}) as handler:
scrapy_request = Request(url="https://example.org", method="GET")
spider = Spider("foo")
initial_request_done = asyncio.Event()
req_handler = handler._make_request_handler(
context_name=DEFAULT_CONTEXT_NAME,
method=scrapy_request.method,
Expand All @@ -120,6 +122,7 @@ async def test_route_continue_exception(self, logger):
body=None,
encoding="utf-8",
spider=spider,
initial_request_done=initial_request_done,
)
route = MagicMock()
playwright_request = AsyncMock()
Expand Down

0 comments on commit 1e3f184

Please sign in to comment.