Skip to content

Commit 482e6cd

Browse files
authored
Fix set_content_disposition() after append() (#8332)
1 parent 4a8fd08 commit 482e6cd

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

CHANGES/8332.bugfix.rst

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed regression with adding Content-Disposition to form-data part after appending to writer -- by :user:`Dreamsorcerer`/:user:`Olegt0rr`.

aiohttp/multipart.py

+5-2
Original file line numberDiff line numberDiff line change
@@ -875,8 +875,6 @@ def append_payload(self, payload: Payload) -> Payload:
875875
if self._is_form_data:
876876
# https://datatracker.ietf.org/doc/html/rfc7578#section-4.7
877877
# https://datatracker.ietf.org/doc/html/rfc7578#section-4.8
878-
assert CONTENT_DISPOSITION in payload.headers
879-
assert "name=" in payload.headers[CONTENT_DISPOSITION]
880878
assert (
881879
not {CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TRANSFER_ENCODING}
882880
& payload.headers.keys()
@@ -957,6 +955,11 @@ def size(self) -> Optional[int]:
957955
async def write(self, writer: Any, close_boundary: bool = True) -> None:
958956
"""Write body."""
959957
for part, encoding, te_encoding in self._parts:
958+
if self._is_form_data:
959+
# https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
960+
assert CONTENT_DISPOSITION in part.headers
961+
assert "name=" in part.headers[CONTENT_DISPOSITION]
962+
960963
await writer.write(b"--" + self._boundary + b"\r\n")
961964
await writer.write(part._binary_headers)
962965

tests/test_multipart.py

+7
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,13 @@ def test_append_multipart(self, writer: Any) -> None:
11651165
part = writer._parts[0][0]
11661166
assert part.headers[CONTENT_TYPE] == "test/passed"
11671167

1168+
async def test_set_content_disposition_after_append(self):
1169+
writer = aiohttp.MultipartWriter("form-data")
1170+
payload = writer.append("some-data")
1171+
payload.set_content_disposition("form-data", name="method")
1172+
assert CONTENT_DISPOSITION in payload.headers
1173+
assert "name=" in payload.headers[CONTENT_DISPOSITION]
1174+
11681175
def test_with(self) -> None:
11691176
with aiohttp.MultipartWriter(boundary=":") as writer:
11701177
writer.append("foo")

0 commit comments

Comments
 (0)