Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Override method and/or body only for the first matching request #297

Merged
merged 2 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions scrapy_playwright/handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,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 @@ -368,6 +375,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 @@ -637,6 +645,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 @@ -676,7 +685,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