Skip to content

Commit 500a4fb

Browse files
committed
Fixes
1 parent 0332916 commit 500a4fb

File tree

3 files changed

+13
-25
lines changed

3 files changed

+13
-25
lines changed

aiohttp/client_reqrep.py

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,7 +1179,7 @@ def _update_body_from_data(self, body: Any, _stacklevel: int = 3) -> None:
11791179
_warn_if_unclosed_payload(self._body, stacklevel=_stacklevel)
11801180

11811181
if body is None:
1182-
self._body = None
1182+
self._body = payload.PAYLOAD_REGISTRY.get(b"", disposition=None)
11831183
# Set Content-Length to 0 when body is None for methods that expect a body
11841184
if (
11851185
self.method not in self.GET_METHODS
@@ -1413,14 +1413,7 @@ async def _write_bytes(
14131413
protocol = conn.protocol
14141414
assert protocol is not None
14151415
try:
1416-
# This should be a rare case but the
1417-
# self._body can be set to None while
1418-
# the task is being started or we wait above
1419-
# for the 100-continue response.
1420-
# The more likely case is we have an empty
1421-
# payload, but 100-continue is still expected.
1422-
if self._body is not None:
1423-
await self._body.write_with_length(writer, content_length)
1416+
await self._body.write_with_length(writer, content_length)
14241417
except OSError as underlying_exc:
14251418
reraised_exc = underlying_exc
14261419

tests/test_client_request.py

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from aiohttp.connector import Connection
4141
from aiohttp.hdrs import METH_DELETE
4242
from aiohttp.helpers import TimerNoop
43-
from aiohttp.http import HttpVersion10, HttpVersion11, StreamWriter
43+
from aiohttp.http import HttpVersion, HttpVersion10, HttpVersion11, StreamWriter
4444
from aiohttp.multipart import MultipartWriter
4545
from aiohttp.typedefs import LooseCookies
4646

@@ -1205,8 +1205,6 @@ async def test_body_payload_with_size_no_content_length(
12051205
loop=loop,
12061206
)
12071207

1208-
# Initially no body should be set
1209-
assert req._body is None
12101208
# POST method with None body should have Content-Length: 0
12111209
assert req.headers[hdrs.CONTENT_LENGTH] == "0"
12121210

@@ -1217,7 +1215,6 @@ async def test_body_payload_with_size_no_content_length(
12171215
assert req.headers[hdrs.CONTENT_LENGTH] == str(len(data))
12181216
assert req.body is bytes_payload
12191217
assert req._body is bytes_payload # Access _body which is the Payload
1220-
assert req._body is not None # type: ignore[unreachable]
12211218
assert req._body.size == len(data)
12221219

12231220
# Set body back to None
@@ -1968,7 +1965,7 @@ async def test_warn_if_unclosed_payload_via_body_setter(
19681965
io.BufferedReader(io.BytesIO(b"test data")),
19691966
encoding="utf-8",
19701967
)
1971-
req.body = file_payload
1968+
req.update_body(file_payload)
19721969

19731970
# Setting body again should trigger the warning for the previous payload
19741971
with pytest.warns(
@@ -1988,7 +1985,7 @@ async def test_no_warn_for_autoclose_payload_via_body_setter(
19881985

19891986
# First set BytesIOPayload which has autoclose=True
19901987
bytes_payload = payload.BytesIOPayload(io.BytesIO(b"test data"))
1991-
req.body = bytes_payload
1988+
req.update_body(bytes_payload)
19921989

19931990
# Setting body again should not trigger warning since previous payload has autoclose=True
19941991
with warnings.catch_warnings(record=True) as warning_list:
@@ -2015,7 +2012,7 @@ async def test_no_warn_for_consumed_payload_via_body_setter(
20152012
io.BufferedReader(io.BytesIO(b"test data")),
20162013
encoding="utf-8",
20172014
)
2018-
req.body = file_payload
2015+
req.update_body(file_payload)
20192016

20202017
# Properly close the payload to mark it as consumed
20212018
await file_payload.close()
@@ -2265,7 +2262,7 @@ async def test_warn_stacklevel_points_to_user_code(
22652262
io.BufferedReader(io.BytesIO(b"test data")),
22662263
encoding="utf-8",
22672264
)
2268-
req.body = file_payload
2265+
req.update_body(file_payload)
22692266

22702267
# Capture warnings with their details
22712268
with warnings.catch_warnings(record=True) as warning_list:
@@ -2326,10 +2323,10 @@ async def test_warn_stacklevel_update_body_from_data(
23262323
await req._close()
23272324

23282325

2329-
async def test_expect100_with_body_becomes_none(
2326+
async def test_expect100_with_body_becomes_empty(
23302327
make_client_request: _RequestMaker,
23312328
) -> None:
2332-
"""Test that write_bytes handles body becoming None after expect100 handling."""
2329+
"""Test that write_bytes handles body becoming empty after expect100 handling."""
23332330
# Create a mock writer and connection
23342331
mock_writer = mock.AsyncMock()
23352332
mock_conn = mock.Mock()
@@ -2340,9 +2337,9 @@ async def test_expect100_with_body_becomes_none(
23402337
)
23412338
req._body = mock.Mock() # Start with a body
23422339

2343-
# Now set body to None to simulate a race condition
2340+
# Now set body to empty payload to simulate a race condition
23442341
# where req._body is set to None after expect100 handling
2345-
req._body = None
2342+
req._body = payload.PAYLOAD_REGISTRY.get(b"", disposition=None)
23462343

23472344
await req._write_bytes(mock_writer, mock_conn, None)
23482345

@@ -2493,7 +2490,6 @@ async def test_update_body_none_sets_content_length_zero(
24932490
# Update body to None
24942491
await req.update_body(None)
24952492
assert req.headers[hdrs.CONTENT_LENGTH] == "0"
2496-
assert req._body is None
24972493
await req._close()
24982494

24992495

@@ -2513,5 +2509,4 @@ async def test_update_body_none_no_content_length_for_get_methods(
25132509
# Update body to None
25142510
await req.update_body(None)
25152511
assert hdrs.CONTENT_LENGTH not in req.headers
2516-
assert req._body is None
25172512
await req._close()

tests/test_proxy.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,8 +158,8 @@ async def make_conn() -> aiohttp.TCPConnector:
158158
autospec=True,
159159
spec_set=True,
160160
)
161-
def test_proxy_auth(
162-
start_connection: mock.Mock, # type: ignore[misc]
161+
def test_proxy_auth( # type: ignore[misc]
162+
start_connection: mock.Mock,
163163
make_client_request: _RequestMaker,
164164
) -> None:
165165
msg = r"proxy_auth must be None or BasicAuth\(\) tuple"

0 commit comments

Comments
 (0)