forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_browsercontext_request_intercept.py
176 lines (141 loc) · 5.72 KB
/
test_browsercontext_request_intercept.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import asyncio
from pathlib import Path
from twisted.web import http
from playwright.async_api import BrowserContext, Page, Route
from tests.server import Server
async def test_should_fulfill_intercepted_response(
page: Page, context: BrowserContext, server: Server
):
async def handle(route: Route):
response = await page.request.fetch(route.request)
await route.fulfill(
response=response,
status=201,
headers={"foo": "bar"},
content_type="text/plain",
body="Yo, page!",
)
await context.route("**/*", handle)
response = await page.goto(server.PREFIX + "/empty.html")
assert response.status == 201
assert response.headers["foo"] == "bar"
assert response.headers["content-type"] == "text/plain"
assert await page.evaluate("() => document.body.textContent") == "Yo, page!"
async def test_should_fulfill_response_with_empty_body(
page: Page, context: BrowserContext, server: Server
):
async def handle(route: Route):
response = await page.request.fetch(route.request)
await route.fulfill(
response=response, status=201, body="", headers={"content-length": "0"}
)
await context.route("**/*", handle)
response = await page.goto(server.PREFIX + "/title.html")
assert response.status == 201
assert await response.text() == ""
async def test_should_override_with_defaults_when_intercepted_response_not_provided(
page: Page, context: BrowserContext, server: Server, browser_name: str
):
def server_handler(request: http.Request):
request.setHeader("foo", "bar")
request.write("my content".encode())
request.finish()
server.set_route("/empty.html", server_handler)
async def handle(route: Route):
await page.request.fetch(route.request)
await route.fulfill(status=201)
await context.route("**/*", handle)
response = await page.goto(server.EMPTY_PAGE)
assert response.status == 201
assert await response.text() == ""
if browser_name == "webkit":
assert response.headers == {"content-type": "text/plain"}
else:
assert response.headers == {}
async def test_should_fulfill_with_any_response(
page: Page, context: BrowserContext, server: Server
):
def server_handler(request: http.Request):
request.setHeader("foo", "bar")
request.write("Woo-hoo".encode())
request.finish()
server.set_route("/sample", server_handler)
sample_response = await page.request.get(server.PREFIX + "/sample")
await context.route(
"**/*",
lambda route: route.fulfill(
response=sample_response, status=201, content_type="text/plain"
),
)
response = await page.goto(server.EMPTY_PAGE)
assert response.status == 201
assert await response.text() == "Woo-hoo"
assert response.headers["foo"] == "bar"
async def test_should_support_fulfill_after_intercept(
page: Page, context: BrowserContext, server: Server, assetdir: Path
):
request_future = asyncio.create_task(server.wait_for_request("/title.html"))
async def handle_route(route: Route):
response = await page.request.fetch(route.request)
await route.fulfill(response=response)
await context.route("**", handle_route)
response = await page.goto(server.PREFIX + "/title.html")
request = await request_future
assert request.uri.decode() == "/title.html"
original = (assetdir / "title.html").read_text()
assert await response.text() == original
async def test_should_give_access_to_the_intercepted_response(
page: Page, context: BrowserContext, server: Server
):
await page.goto(server.EMPTY_PAGE)
route_task = asyncio.Future()
await context.route("**/title.html", lambda route: route_task.set_result(route))
eval_task = asyncio.create_task(
page.evaluate("url => fetch(url)", server.PREFIX + "/title.html")
)
route = await route_task
response = await page.request.fetch(route.request)
assert response.status == 200
assert response.status_text == "OK"
assert response.ok is True
assert response.url.endswith("/title.html") is True
assert response.headers["content-type"] == "text/html; charset=utf-8"
assert list(
filter(
lambda header: header["name"].lower() == "content-type",
response.headers_array,
)
) == [{"name": "Content-Type", "value": "text/html; charset=utf-8"}]
await asyncio.gather(
route.fulfill(response=response),
eval_task,
)
async def test_should_give_access_to_the_intercepted_response_body(
page: Page, context: BrowserContext, server: Server
):
await page.goto(server.EMPTY_PAGE)
route_task = asyncio.Future()
await context.route("**/simple.json", lambda route: route_task.set_result(route))
eval_task = asyncio.create_task(
page.evaluate("url => fetch(url)", server.PREFIX + "/simple.json")
)
route = await route_task
response = await page.request.fetch(route.request)
assert await response.text() == '{"foo": "bar"}\n'
await asyncio.gather(
route.fulfill(response=response),
eval_task,
)