Skip to content

Commit 7eecdff

Browse files
[PR #8332/482e6cdf backport][3.9] Add set_content_disposition test (#8333)
**This is a backport of PR #8332 as merged into master (482e6cd).** Co-authored-by: Oleg A <[email protected]>
1 parent 82fbe64 commit 7eecdff

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
@@ -848,8 +848,6 @@ def append_payload(self, payload: Payload) -> Payload:
848848
if self._is_form_data:
849849
# https://datatracker.ietf.org/doc/html/rfc7578#section-4.7
850850
# https://datatracker.ietf.org/doc/html/rfc7578#section-4.8
851-
assert CONTENT_DISPOSITION in payload.headers
852-
assert "name=" in payload.headers[CONTENT_DISPOSITION]
853851
assert (
854852
not {CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TRANSFER_ENCODING}
855853
& payload.headers.keys()
@@ -930,6 +928,11 @@ def size(self) -> Optional[int]:
930928
async def write(self, writer: Any, close_boundary: bool = True) -> None:
931929
"""Write body."""
932930
for part, encoding, te_encoding in self._parts:
931+
if self._is_form_data:
932+
# https://datatracker.ietf.org/doc/html/rfc7578#section-4.2
933+
assert CONTENT_DISPOSITION in part.headers
934+
assert "name=" in part.headers[CONTENT_DISPOSITION]
935+
933936
await writer.write(b"--" + self._boundary + b"\r\n")
934937
await writer.write(part._binary_headers)
935938

tests/test_multipart.py

+7
Original file line numberDiff line numberDiff line change
@@ -1282,6 +1282,13 @@ def test_append_multipart(self, writer) -> None:
12821282
part = writer._parts[0][0]
12831283
assert part.headers[CONTENT_TYPE] == "test/passed"
12841284

1285+
async def test_set_content_disposition_after_append(self):
1286+
writer = aiohttp.MultipartWriter("form-data")
1287+
payload = writer.append("some-data")
1288+
payload.set_content_disposition("form-data", name="method")
1289+
assert CONTENT_DISPOSITION in payload.headers
1290+
assert "name=" in payload.headers[CONTENT_DISPOSITION]
1291+
12851292
def test_with(self) -> None:
12861293
with aiohttp.MultipartWriter(boundary=":") as writer:
12871294
writer.append("foo")

0 commit comments

Comments
 (0)