diff --git a/CHANGES/8332.bugfix.rst b/CHANGES/8332.bugfix.rst new file mode 100644 index 00000000000..70cad26b426 --- /dev/null +++ b/CHANGES/8332.bugfix.rst @@ -0,0 +1 @@ +Fixed regression with adding Content-Disposition to form-data part after appending to writer -- by :user:`Dreamsorcerer`/:user:`Olegt0rr`. diff --git a/aiohttp/multipart.py b/aiohttp/multipart.py index fe3ad6bacaa..6a441ba6456 100644 --- a/aiohttp/multipart.py +++ b/aiohttp/multipart.py @@ -875,8 +875,6 @@ def append_payload(self, payload: Payload) -> Payload: if self._is_form_data: # https://datatracker.ietf.org/doc/html/rfc7578#section-4.7 # https://datatracker.ietf.org/doc/html/rfc7578#section-4.8 - assert CONTENT_DISPOSITION in payload.headers - assert "name=" in payload.headers[CONTENT_DISPOSITION] assert ( not {CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TRANSFER_ENCODING} & payload.headers.keys() @@ -957,6 +955,11 @@ def size(self) -> Optional[int]: async def write(self, writer: Any, close_boundary: bool = True) -> None: """Write body.""" for part, encoding, te_encoding in self._parts: + if self._is_form_data: + # https://datatracker.ietf.org/doc/html/rfc7578#section-4.2 + assert CONTENT_DISPOSITION in part.headers + assert "name=" in part.headers[CONTENT_DISPOSITION] + await writer.write(b"--" + self._boundary + b"\r\n") await writer.write(part._binary_headers) diff --git a/tests/test_multipart.py b/tests/test_multipart.py index d7e23592a75..79ee464c0e0 100644 --- a/tests/test_multipart.py +++ b/tests/test_multipart.py @@ -1165,6 +1165,13 @@ def test_append_multipart(self, writer: Any) -> None: part = writer._parts[0][0] assert part.headers[CONTENT_TYPE] == "test/passed" + async def test_set_content_disposition_after_append(self): + writer = aiohttp.MultipartWriter("form-data") + payload = writer.append("some-data") + payload.set_content_disposition("form-data", name="method") + assert CONTENT_DISPOSITION in payload.headers + assert "name=" in payload.headers[CONTENT_DISPOSITION] + def test_with(self) -> None: with aiohttp.MultipartWriter(boundary=":") as writer: writer.append("foo")