From 6b3dd6fe22843816c11d83dc665841d338ff2cbe Mon Sep 17 00:00:00 2001 From: suraj-self Date: Fri, 2 Oct 2026 11:56:31 +0530 Subject: [PATCH] fix(api): enforce mutual exclusivity between data and form options --- playwright/_impl/_fetch.py | 4 +++- tests/async/test_fetch_global.py | 16 ++++++++++++++++ tests/sync/test_fetch_global.py | 16 ++++++++++++++++ 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/playwright/_impl/_fetch.py b/playwright/_impl/_fetch.py index 6181f22b2..fd25393e8 100644 --- a/playwright/_impl/_fetch.py +++ b/playwright/_impl/_fetch.py @@ -369,7 +369,9 @@ async def _inner_fetch( if self._close_reason: raise TargetClosedError(self._close_reason) assert ( - (1 if data else 0) + (1 if form else 0) + (1 if multipart else 0) + (1 if data is not None else 0) + + (1 if form is not None else 0) + + (1 if multipart is not None else 0) ) <= 1, "Only one of 'data', 'form' or 'multipart' can be specified" assert ( maxRedirects is None or maxRedirects >= 0 diff --git a/tests/async/test_fetch_global.py b/tests/async/test_fetch_global.py index 10f82583c..bdb9dc771 100644 --- a/tests/async/test_fetch_global.py +++ b/tests/async/test_fetch_global.py @@ -474,6 +474,22 @@ async def test_should_serialize_request_data( await request.dispose() +async def test_should_reject_empty_data_with_form( + playwright: Playwright, server: Server +) -> None: + request = await playwright.request.new_context() + with pytest.raises( + AssertionError, + match="Only one of 'data', 'form' or 'multipart' can be specified", + ): + await request.post( + server.EMPTY_PAGE, + data="", + form={"name": "value"}, + ) + await request.dispose() + + async def test_should_retry_ECONNRESET(playwright: Playwright, server: Server) -> None: request_count = 0 diff --git a/tests/sync/test_fetch_global.py b/tests/sync/test_fetch_global.py index 15a11fca8..08a7d207c 100644 --- a/tests/sync/test_fetch_global.py +++ b/tests/sync/test_fetch_global.py @@ -334,6 +334,22 @@ def test_should_serialize_null_values_in_json( request.dispose() +def test_should_reject_empty_data_with_form( + playwright: Playwright, server: Server +) -> None: + request = playwright.request.new_context() + with pytest.raises( + AssertionError, + match="Only one of 'data', 'form' or 'multipart' can be specified", + ): + request.post( + server.EMPTY_PAGE, + data="", + form={"name": "value"}, + ) + request.dispose() + + def test_should_throw_when_fail_on_status_code_is_true( playwright: Playwright, server: Server ) -> None: