forked from microsoft/playwright-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_fetch_global.py
279 lines (238 loc) · 9.31 KB
/
test_fetch_global.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# 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
import json
import sys
from pathlib import Path
from typing import Any
import pytest
from playwright.async_api import APIResponse, Error, Playwright
from tests.server import Server
@pytest.mark.parametrize(
"method", ["fetch", "delete", "get", "head", "patch", "post", "put"]
)
async def test_should_work(playwright: Playwright, method: str, server: Server):
request = await playwright.request.new_context()
response: APIResponse = await getattr(request, method)(
server.PREFIX + "/simple.json"
)
assert response.status == 200
assert response.status_text == "OK"
assert response.ok is True
assert response.url == server.PREFIX + "/simple.json"
assert response.headers["content-type"] == "application/json"
assert {
"name": "Content-Type",
"value": "application/json",
} in response.headers_array
assert await response.text() == ("" if method == "head" else '{"foo": "bar"}\n')
async def test_should_dispose_global_request(playwright: Playwright, server: Server):
request = await playwright.request.new_context()
response = await request.get(server.PREFIX + "/simple.json")
assert await response.json() == {"foo": "bar"}
await response.dispose()
with pytest.raises(Error, match="Response has been disposed"):
await response.body()
async def test_should_support_global_user_agent_option(
playwright: Playwright, server: Server
):
request = await playwright.request.new_context(user_agent="My Agent")
response = await request.get(server.PREFIX + "/empty.html")
[request, _] = await asyncio.gather(
server.wait_for_request("/empty.html"),
request.get(server.EMPTY_PAGE),
)
assert response.ok is True
assert response.url == server.EMPTY_PAGE
assert request.getHeader("user-agent") == "My Agent"
async def test_should_support_global_timeout_option(
playwright: Playwright, server: Server
):
request = await playwright.request.new_context(timeout=1)
server.set_route("/empty.html", lambda req: None)
with pytest.raises(Error, match="Request timed out after 1ms"):
await request.get(server.EMPTY_PAGE)
async def test_should_propagate_extra_http_headers_with_redirects(
playwright: Playwright, server: Server
):
server.set_redirect("/a/redirect1", "/b/c/redirect2")
server.set_redirect("/b/c/redirect2", "/simple.json")
request = await playwright.request.new_context(
extra_http_headers={"My-Secret": "Value"}
)
[req1, req2, req3, _] = await asyncio.gather(
server.wait_for_request("/a/redirect1"),
server.wait_for_request("/b/c/redirect2"),
server.wait_for_request("/simple.json"),
request.get(f"{server.PREFIX}/a/redirect1"),
)
assert req1.getHeader("my-secret") == "Value"
assert req2.getHeader("my-secret") == "Value"
assert req3.getHeader("my-secret") == "Value"
async def test_should_support_global_http_credentials_option(
playwright: Playwright, server: Server
):
server.set_auth("/empty.html", "user", "pass")
request1 = await playwright.request.new_context()
response1 = await request1.get(server.EMPTY_PAGE)
assert response1.status == 401
await response1.dispose()
request2 = await playwright.request.new_context(
http_credentials={"username": "user", "password": "pass"}
)
response2 = await request2.get(server.EMPTY_PAGE)
assert response2.status == 200
assert response2.ok is True
await response2.dispose()
async def test_should_return_error_with_wrong_credentials(
playwright: Playwright, server: Server
):
server.set_auth("/empty.html", "user", "pass")
request = await playwright.request.new_context(
http_credentials={"username": "user", "password": "wrong"}
)
response = await request.get(server.EMPTY_PAGE)
assert response.status == 401
assert response.ok is False
async def test_should_support_global_ignore_https_errors_option(
playwright: Playwright, https_server: Server
):
request = await playwright.request.new_context(ignore_https_errors=True)
response = await request.get(https_server.EMPTY_PAGE)
assert response.status == 200
assert response.ok is True
assert response.url == https_server.EMPTY_PAGE
await response.dispose()
async def test_should_resolve_url_relative_to_global_base_url_option(
playwright: Playwright, server: Server
):
request = await playwright.request.new_context(base_url=server.PREFIX)
response = await request.get("/empty.html")
assert response.status == 200
assert response.ok is True
assert response.url == server.EMPTY_PAGE
await response.dispose()
async def test_should_use_playwright_as_a_user_agent(
playwright: Playwright, server: Server
):
request = await playwright.request.new_context()
[server_req, _] = await asyncio.gather(
server.wait_for_request("/empty.html"),
request.get(server.EMPTY_PAGE),
)
assert str(server_req.getHeader("User-Agent")).startswith("Playwright/")
await request.dispose()
async def test_should_return_empty_body(playwright: Playwright, server: Server):
request = await playwright.request.new_context()
response = await request.get(server.EMPTY_PAGE)
body = await response.body()
assert len(body) == 0
assert await response.text() == ""
await request.dispose()
with pytest.raises(Error, match="Response has been disposed"):
await response.body()
async def test_storage_state_should_round_trip_through_file(
playwright: Playwright, tmpdir: Path
):
expected = {
"cookies": [
{
"name": "a",
"value": "b",
"domain": "a.b.one.com",
"path": "/",
"expires": -1,
"httpOnly": False,
"secure": False,
"sameSite": "Lax",
}
],
"origins": [],
}
request = await playwright.request.new_context(storage_state=expected)
path = tmpdir / "storage-state.json"
actual = await request.storage_state(path=path)
assert actual == expected
written = path.read_text("utf8")
assert json.loads(written) == expected
request2 = await playwright.request.new_context(storage_state=path)
state2 = await request2.storage_state()
assert state2 == expected
serialization_data = [
[{"foo": "bar"}],
[["foo", "bar", 2021]],
["foo"],
[True],
[2021],
]
@pytest.mark.parametrize("serialization", serialization_data)
async def test_should_json_stringify_body_when_content_type_is_application_json(
playwright: Playwright, server: Server, serialization: Any
):
request = await playwright.request.new_context()
[req, _] = await asyncio.gather(
server.wait_for_request("/empty.html"),
request.post(
server.EMPTY_PAGE,
headers={"content-type": "application/json"},
data=serialization,
),
)
body = req.post_body
assert body.decode() == json.dumps(serialization, separators=(",", ":"))
await request.dispose()
@pytest.mark.parametrize("serialization", serialization_data)
async def test_should_not_double_stringify_body_when_content_type_is_application_json(
playwright: Playwright, server: Server, serialization: Any
):
request = await playwright.request.new_context()
stringified_value = json.dumps(serialization, separators=(",", ":"))
[req, _] = await asyncio.gather(
server.wait_for_request("/empty.html"),
request.post(
server.EMPTY_PAGE,
headers={"content-type": "application/json"},
data=stringified_value,
),
)
body = req.post_body
assert body.decode() == stringified_value
await request.dispose()
async def test_should_accept_already_serialized_data_as_bytes_when_content_type_is_application_json(
playwright: Playwright, server: Server
):
request = await playwright.request.new_context()
stringified_value = json.dumps({"foo": "bar"}, separators=(",", ":")).encode()
[req, _] = await asyncio.gather(
server.wait_for_request("/empty.html"),
request.post(
server.EMPTY_PAGE,
headers={"content-type": "application/json"},
data=stringified_value,
),
)
body = req.post_body
assert body == stringified_value
await request.dispose()
async def test_should_contain_default_user_agent(
playwright: Playwright, server: Server
):
request = await playwright.request.new_context()
[request, _] = await asyncio.gather(
server.wait_for_request("/empty.html"),
request.get(server.EMPTY_PAGE),
)
user_agent = request.getHeader("user-agent")
assert "python" in user_agent
assert f"{sys.version_info.major}.{sys.version_info.minor}" in user_agent