|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from playwright.sync_api import Page, Request, Route |
| 16 | +from tests.server import Server |
| 17 | + |
| 18 | + |
| 19 | +def test_should_return_last_requests(page: Page, server: Server) -> None: |
| 20 | + page.goto(server.PREFIX + "/title.html") |
| 21 | + for i in range(200): |
| 22 | + |
| 23 | + def _handle_route(route: Route) -> None: |
| 24 | + route.fulfill( |
| 25 | + status=200, |
| 26 | + body=f"url:{route.request.url}", |
| 27 | + ) |
| 28 | + |
| 29 | + page.route(f"**/fetch?{i}", _handle_route) |
| 30 | + |
| 31 | + # #0 is the navigation request, so start with #1. |
| 32 | + for i in range(1, 100): |
| 33 | + page.evaluate("url => fetch(url)", server.PREFIX + f"/fetch?{i}") |
| 34 | + first_100_requests_with_goto = page.requests() |
| 35 | + first_100_requests = first_100_requests_with_goto[1:] |
| 36 | + |
| 37 | + for i in range(100, 200): |
| 38 | + page.evaluate("url => fetch(url)", server.PREFIX + f"/fetch?{i}") |
| 39 | + last_100_requests = page.requests() |
| 40 | + |
| 41 | + all_requests = first_100_requests + last_100_requests |
| 42 | + |
| 43 | + def gather_response(request: Request) -> dict: |
| 44 | + response = request.response() |
| 45 | + assert response |
| 46 | + return {"text": response.text(), "url": request.url} |
| 47 | + |
| 48 | + # All 199 requests are fully functional. |
| 49 | + received = [gather_response(request) for request in all_requests] |
| 50 | + expected = [] |
| 51 | + for i in range(1, 200): |
| 52 | + url = server.PREFIX + f"/fetch?{i}" |
| 53 | + expected.append({"url": url, "text": f"url:{url}"}) |
| 54 | + assert received == expected |
0 commit comments