From d83dc6e96a4ce9ffa8b0e95667866320702142fb Mon Sep 17 00:00:00 2001 From: Max Schmitt Date: Mon, 1 Jul 2024 18:32:16 +0200 Subject: [PATCH] fix(fetch): serialise empty array in 'data' as JSON (#2476) --- playwright/_impl/_fetch.py | 2 +- tests/async/test_fetch_global.py | 14 ++++++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/playwright/_impl/_fetch.py b/playwright/_impl/_fetch.py index 3a71a5ff5..da17c169c 100644 --- a/playwright/_impl/_fetch.py +++ b/playwright/_impl/_fetch.py @@ -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) diff --git a/tests/async/test_fetch_global.py b/tests/async/test_fetch_global.py index eda3145ee..82ecf38ec 100644 --- a/tests/async/test_fetch_global.py +++ b/tests/async/test_fetch_global.py @@ -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()