Skip to content

Commit 69d18bf

Browse files
committed
follow-ups
1 parent 91c4fc3 commit 69d18bf

File tree

6 files changed

+133
-22
lines changed

6 files changed

+133
-22
lines changed

playwright/_impl/_helper.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,11 +294,12 @@ async def handle(self, route: "Route") -> bool:
294294
if self._ignore_exception:
295295
return False
296296
if is_target_closed_error(e):
297-
# We are failing in the handler because the target close closed.
297+
# We are failing in the handler because the target has closed.
298298
# Give user a hint!
299+
optional_async_prefix = "await " if not self._is_sync else ""
299300
raise rewrite_error(
300301
e,
301-
f"\"{str(e)}\" while running route callback.\nConsider awaiting `await page.unroute_all(behavior='ignoreErrors')`\nbefore the end of the test to ignore remaining routes in flight.",
302+
f"\"{str(e)}\" while running route callback.\nConsider awaiting `{optional_async_prefix}page.unroute_all(behavior='ignoreErrors')`\nbefore the end of the test to ignore remaining routes in flight.",
302303
)
303304
raise e
304305
finally:

playwright/_impl/_network.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,6 @@ def __init__(
111111
self._fallback_overrides: SerializedFallbackOverrides = (
112112
SerializedFallbackOverrides()
113113
)
114-
base64_post_data = initializer.get("postData")
115-
if base64_post_data is not None:
116-
self._fallback_overrides.post_data_buffer = base64.b64decode(
117-
base64_post_data
118-
)
119114

120115
def __repr__(self) -> str:
121116
return f"<Request url={self.url!r} method={self.method!r}>"
@@ -159,9 +154,12 @@ async def sizes(self) -> RequestSizes:
159154
@property
160155
def post_data(self) -> Optional[str]:
161156
data = self._fallback_overrides.post_data_buffer
162-
if not data:
163-
return None
164-
return data.decode()
157+
if data:
158+
return data.decode()
159+
base64_post_data = self._initializer.get("postData")
160+
if base64_post_data is not None:
161+
return base64.b64decode(base64_post_data).decode()
162+
return None
165163

166164
@property
167165
def post_data_json(self) -> Optional[Any]:

tests/async/test_browsertype_connect.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,15 @@ async def test_should_upload_a_folder(
428428
(dir / "sub-dir").mkdir()
429429
(dir / "sub-dir" / "really.txt").write_text("sub-dir file content")
430430
await input.set_input_files(dir)
431-
assert await input.evaluate("e => [...e.files].map(f => f.webkitRelativePath)") == [
432-
"file-upload-test/file1.txt",
433-
"file-upload-test/file2",
434-
"file-upload-test/sub-dir/really.txt",
435-
]
431+
assert set(
432+
await input.evaluate("e => [...e.files].map(f => f.webkitRelativePath)")
433+
) == set(
434+
[
435+
"file-upload-test/file1.txt",
436+
"file-upload-test/file2",
437+
"file-upload-test/sub-dir/really.txt",
438+
]
439+
)
436440
webkit_relative_paths = await input.evaluate(
437441
"e => [...e.files].map(f => f.webkitRelativePath)"
438442
)

tests/async/test_defaultbrowsercontext.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,14 @@ async def test_context_add_cookies_should_work(
8383
(page, context) = await launch_persistent()
8484
await page.goto(server.EMPTY_PAGE)
8585
await page.context.add_cookies(
86-
[{"url": server.EMPTY_PAGE, "name": "username", "value": "John Doe"}]
86+
[
87+
{
88+
"url": server.EMPTY_PAGE,
89+
"name": "username",
90+
"value": "John Doe",
91+
"sameSite": "Lax",
92+
}
93+
]
8794
)
8895
assert await page.evaluate("() => document.cookie") == "username=John Doe"
8996
assert await page.context.cookies() == [

tests/async/test_input.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,11 +427,15 @@ async def test_should_upload_a_folder(
427427
(dir / "sub-dir").mkdir()
428428
(dir / "sub-dir" / "really.txt").write_text("sub-dir file content")
429429
await input.set_input_files(dir)
430-
assert await input.evaluate("e => [...e.files].map(f => f.webkitRelativePath)") == [
431-
"file-upload-test/file1.txt",
432-
"file-upload-test/file2",
433-
"file-upload-test/sub-dir/really.txt",
434-
]
430+
assert set(
431+
await input.evaluate("e => [...e.files].map(f => f.webkitRelativePath)")
432+
) == (
433+
[
434+
"file-upload-test/file1.txt",
435+
"file-upload-test/file2",
436+
"file-upload-test/sub-dir/really.txt",
437+
]
438+
)
435439
webkit_relative_paths = await input.evaluate(
436440
"e => [...e.files].map(f => f.webkitRelativePath)"
437441
)

tests/async/test_request_continue.py

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
from typing import Optional
1717

1818
from playwright.async_api import Page, Route
19-
from tests.server import Server
19+
from tests.server import Server, TestServerRequest
2020

2121

2222
async def test_request_continue_should_work(page: Page, server: Server) -> None:
@@ -145,3 +145,100 @@ async def test_should_amend_binary_post_data(page: Page, server: Server) -> None
145145
)
146146
assert server_request.method == b"POST"
147147
assert server_request.post_body == b"\x00\x01\x02\x03\x04"
148+
149+
150+
# it('continue should not change multipart/form-data body', async ({ page, server, browserName }) => {
151+
# it.info().annotations.push({ type: 'issue', description: 'https://github.com/microsoft/playwright/issues/19158' });
152+
# await page.goto(server.EMPTY_PAGE);
153+
# server.setRoute('/upload', (request, response) => {
154+
# response.writeHead(200, { 'Content-Type': 'text/plain' });
155+
# response.end('done');
156+
# });
157+
# async function sendFormData() {
158+
# const reqPromise = server.waitForRequest('/upload');
159+
# const status = await page.evaluate(async () => {
160+
# const newFile = new File(['file content'], 'file.txt');
161+
# const formData = new FormData();
162+
# formData.append('file', newFile);
163+
# const response = await fetch('/upload', {
164+
# method: 'POST',
165+
# credentials: 'include',
166+
# body: formData,
167+
# });
168+
# return response.status;
169+
# });
170+
# const req = await reqPromise;
171+
# expect(status).toBe(200);
172+
# return req;
173+
# }
174+
# const reqBefore = await sendFormData();
175+
# await page.route('**/*', async route => {
176+
# await route.continue();
177+
# });
178+
# const reqAfter = await sendFormData();
179+
# const fileContent = [
180+
# 'Content-Disposition: form-data; name=\"file\"; filename=\"file.txt\"',
181+
# 'Content-Type: application/octet-stream',
182+
# '',
183+
# 'file content',
184+
# '------'].join('\r\n');
185+
# expect.soft((await reqBefore.postBody).toString('utf8')).toContain(fileContent);
186+
# expect.soft((await reqAfter.postBody).toString('utf8')).toContain(fileContent);
187+
# // Firefox sends a bit longer boundary.
188+
# const expectedLength = browserName === 'firefox' ? '246' : '208';
189+
# expect.soft(reqBefore.headers['content-length']).toBe(expectedLength);
190+
# expect.soft(reqAfter.headers['content-length']).toBe(expectedLength);
191+
# });
192+
193+
194+
async def test_continue_should_not_change_multipart_form_data_body(
195+
page: Page, server: Server, browser_name: str
196+
) -> None:
197+
await page.goto(server.EMPTY_PAGE)
198+
server.set_route(
199+
"/upload",
200+
lambda context: (
201+
context.write(b"done"),
202+
context.setHeader("Content-Type", "text/plain"),
203+
context.finish(),
204+
),
205+
)
206+
207+
async def send_form_data() -> TestServerRequest:
208+
req_task = asyncio.create_task(server.wait_for_request("/upload"))
209+
status = await page.evaluate(
210+
"""async () => {
211+
const newFile = new File(['file content'], 'file.txt');
212+
const formData = new FormData();
213+
formData.append('file', newFile);
214+
const response = await fetch('/upload', {
215+
method: 'POST',
216+
credentials: 'include',
217+
body: formData,
218+
});
219+
return response.status;
220+
}"""
221+
)
222+
req = await req_task
223+
assert status == 200
224+
return req
225+
226+
req_before = await send_form_data()
227+
await page.route("**/*", lambda route: route.continue_())
228+
req_after = await send_form_data()
229+
230+
file_content = (
231+
'Content-Disposition: form-data; name="file"; filename="file.txt"\r\n'
232+
"Content-Type: application/octet-stream\r\n"
233+
"\r\n"
234+
"file content\r\n"
235+
"------"
236+
)
237+
assert req_before.post_body
238+
assert req_after.post_body
239+
assert file_content in req_before.post_body.decode()
240+
assert file_content in req_after.post_body.decode()
241+
# Firefox sends a bit longer boundary.
242+
expected_length = "246" if browser_name == "firefox" else "208"
243+
assert req_before.getHeader("content-length") == expected_length
244+
assert req_after.getHeader("content-length") == expected_length

0 commit comments

Comments
 (0)