Skip to content

Commit

Permalink
fix(fetch): serialise empty array in 'data' as JSON (#2476)
Browse files Browse the repository at this point in the history
  • Loading branch information
mxschmitt authored Jul 1, 2024
1 parent c6cc4c9 commit d83dc6e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
2 changes: 1 addition & 1 deletion playwright/_impl/_fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ async def _inner_fetch(
form_data: Optional[List[NameValue]] = None
multipart_data: Optional[List[FormField]] = None
post_data_buffer: Optional[bytes] = None
if data:
if data is not None:
if isinstance(data, str):
if is_json_content_type(serialized_headers):
json_data = data if is_json_parsable(data) else json.dumps(data)
Expand Down
14 changes: 10 additions & 4 deletions tests/async/test_fetch_global.py
Original file line number Diff line number Diff line change
Expand Up @@ -448,12 +448,18 @@ async def test_should_throw_an_error_when_max_redirects_is_less_than_0(
assert "'max_redirects' must be greater than or equal to '0'" in str(exc_info)


async def test_should_serialize_null_values_in_json(
async def test_should_serialize_request_data(
playwright: Playwright, server: Server
) -> None:
request = await playwright.request.new_context()
server.set_route("/echo", lambda req: (req.write(req.post_body), req.finish()))
response = await request.post(server.PREFIX + "/echo", data={"foo": None})
assert response.status == 200
assert await response.text() == '{"foo": null}'
for data, expected in [
({"foo": None}, '{"foo": null}'),
([], "[]"),
({}, "{}"),
("", ""),
]:
response = await request.post(server.PREFIX + "/echo", data=data)
assert response.status == 200
assert await response.text() == expected
await request.dispose()

0 comments on commit d83dc6e

Please sign in to comment.