-
Notifications
You must be signed in to change notification settings - Fork 1k
chore: port routeFromHar, roll to 1.23.0 driver #1384
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
87f36e4
add impl and tests for route_from_har
rwoll 25dc4e5
replace log comments
rwoll 54ab5b3
stop pre-commit from corrupting our HAR files
rwoll 3a53f4b
clean up bots and tests
rwoll 94d0577
update test based on upstream
rwoll 37e92aa
increase the flaky threshold
rwoll 5eaf5ab
Remove logs
rwoll e060383
check in changes from linter
rwoll File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
import asyncio | ||
import base64 | ||
from typing import TYPE_CHECKING, Optional, cast | ||
|
||
from playwright._impl._api_structures import HeadersArray | ||
from playwright._impl._helper import ( | ||
HarLookupResult, | ||
RouteFromHarNotFoundPolicy, | ||
URLMatch, | ||
) | ||
from playwright._impl._local_utils import LocalUtils | ||
|
||
if TYPE_CHECKING: # pragma: no cover | ||
from playwright._impl._browser_context import BrowserContext | ||
from playwright._impl._network import Route | ||
from playwright._impl._page import Page | ||
|
||
|
||
class HarRouter: | ||
def __init__( | ||
self, | ||
local_utils: LocalUtils, | ||
har_id: str, | ||
not_found_action: RouteFromHarNotFoundPolicy, | ||
url_matcher: Optional[URLMatch] = None, | ||
) -> None: | ||
self._local_utils: LocalUtils = local_utils | ||
self._har_id: str = har_id | ||
self._not_found_action: RouteFromHarNotFoundPolicy = not_found_action | ||
self._options_url_match: Optional[URLMatch] = url_matcher | ||
|
||
@staticmethod | ||
async def create( | ||
local_utils: LocalUtils, | ||
file: str, | ||
not_found_action: RouteFromHarNotFoundPolicy, | ||
url_matcher: Optional[URLMatch] = None, | ||
) -> "HarRouter": | ||
har_id = await local_utils._channel.send("harOpen", {"file": file}) | ||
return HarRouter( | ||
local_utils=local_utils, | ||
har_id=har_id, | ||
not_found_action=not_found_action, | ||
url_matcher=url_matcher, | ||
) | ||
|
||
async def _handle(self, route: "Route") -> None: | ||
request = route.request | ||
response: HarLookupResult = await self._local_utils.har_lookup( | ||
harId=self._har_id, | ||
url=request.url, | ||
method=request.method, | ||
headers=await request.headers_array(), | ||
postData=request.post_data_buffer, | ||
isNavigationRequest=request.is_navigation_request(), | ||
) | ||
action = response["action"] | ||
if action == "redirect": | ||
redirect_url = response["redirectURL"] | ||
assert redirect_url | ||
await route._redirected_navigation_request(redirect_url) | ||
return | ||
|
||
if action == "fulfill": | ||
body = response["body"] | ||
assert body is not None | ||
await route.fulfill( | ||
status=response.get("status"), | ||
headers={ | ||
v["name"]: v["value"] | ||
for v in cast(HeadersArray, response.get("headers", [])) | ||
}, | ||
body=base64.b64decode(body), | ||
) | ||
return | ||
|
||
if action == "error": | ||
pass | ||
# Report the error, but fall through to the default handler. | ||
|
||
if self._not_found_action == "abort": | ||
await route.abort() | ||
return | ||
|
||
await route.fallback() | ||
|
||
async def add_context_route(self, context: "BrowserContext") -> None: | ||
await context.route( | ||
url=self._options_url_match or "**/*", | ||
handler=lambda route, _: asyncio.create_task(self._handle(route)), | ||
) | ||
context.once("close", lambda _: self._dispose()) | ||
|
||
async def add_page_route(self, page: "Page") -> None: | ||
await page.route( | ||
url=self._options_url_match or "**/*", | ||
handler=lambda route, _: asyncio.create_task(self._handle(route)), | ||
) | ||
page.once("close", lambda _: self._dispose()) | ||
|
||
def _dispose(self) -> None: | ||
asyncio.create_task( | ||
self._local_utils._channel.send("harClose", {"harId": self._har_id}) | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.