Skip to content

fix: reverse route handlers order #804

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 1 commit into from
Jul 14, 2021
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
4 changes: 2 additions & 2 deletions playwright/_impl/_browser_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,8 @@ async def expose_function(self, name: str, callback: Callable) -> None:
await self.expose_binding(name, lambda source, *args: callback(*args))

async def route(self, url: URLMatch, handler: RouteHandler) -> None:
self._routes.append(
RouteHandlerEntry(URLMatcher(self._options.get("baseURL"), url), handler)
self._routes.insert(
0, RouteHandlerEntry(URLMatcher(self._options.get("baseURL"), url), handler)
)
if len(self._routes) == 1:
await self._channel.send(
Expand Down
5 changes: 3 additions & 2 deletions playwright/_impl/_page.py
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,11 @@ async def add_init_script(
await self._channel.send("addInitScript", dict(source=script))

async def route(self, url: URLMatch, handler: RouteHandler) -> None:
self._routes.append(
self._routes.insert(
0,
RouteHandlerEntry(
URLMatcher(self._browser_context._options.get("baseURL"), url), handler
)
),
)
if len(self._routes) == 1:
await self._channel.send(
Expand Down
19 changes: 10 additions & 9 deletions tests/async/test_browsercontext.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,30 +464,31 @@ def handler(route, request, ordinal):
intercepted.append(ordinal)
asyncio.create_task(route.continue_())

def handler1(route, request):
handler(route, request, 1)

await context.route("**/empty.html", handler1)
await context.route("**/*", lambda route, request: handler(route, request, 1))
await context.route(
"**/empty.html", lambda route, request: handler(route, request, 2)
)
await context.route(
"**/empty.html", lambda route, request: handler(route, request, 3)
)
await context.route("**/*", lambda route, request: handler(route, request, 4))

def handler4(route, request):
handler(route, request, 4)

await context.route("**/empty.html", handler4)

await page.goto(server.EMPTY_PAGE)
assert intercepted == [1]
assert intercepted == [4]

intercepted = []
await context.unroute("**/empty.html", handler1)
await context.unroute("**/empty.html", handler4)
await page.goto(server.EMPTY_PAGE)
assert intercepted == [2]
assert intercepted == [3]

intercepted = []
await context.unroute("**/empty.html")
await page.goto(server.EMPTY_PAGE)
assert intercepted == [4]
assert intercepted == [1]


async def test_route_should_yield_to_page_route(context, server):
Expand Down
29 changes: 15 additions & 14 deletions tests/async/test_interception.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,47 +46,48 @@ async def handle_request(route, request):
async def test_page_route_should_unroute(page: Page, server):
intercepted = []

def handler1(route):
intercepted.append(1)
asyncio.create_task(route.continue_())

await page.route("**/empty.html", handler1)
await page.route(
"**/empty.html",
"**/*",
lambda route: (
intercepted.append(2), # type: ignore
intercepted.append(1),
asyncio.create_task(route.continue_()),
),
)

await page.route(
"**/empty.html",
lambda route: (
intercepted.append(3), # type: ignore
intercepted.append(2),
asyncio.create_task(route.continue_()),
),
)

await page.route(
"**/*",
"**/empty.html",
lambda route: (
intercepted.append(4), # type: ignore
intercepted.append(3),
asyncio.create_task(route.continue_()),
),
)

def handler4(route):
intercepted.append(4)
asyncio.create_task(route.continue_())

await page.route("**/empty.html", handler4)

await page.goto(server.EMPTY_PAGE)
assert intercepted == [1]
assert intercepted == [4]

intercepted = []
await page.unroute("**/empty.html", handler1)
await page.unroute("**/empty.html", handler4)
await page.goto(server.EMPTY_PAGE)
assert intercepted == [2]
assert intercepted == [3]

intercepted = []
await page.unroute("**/empty.html")
await page.goto(server.EMPTY_PAGE)
assert intercepted == [4]
assert intercepted == [1]


async def test_page_route_should_work_when_POST_is_redirected_with_302(page, server):
Expand Down