Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add set_content_disposition test #8332

Merged
merged 8 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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.payload.headers
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
assert "name=" in part.payload.headers[CONTENT_DISPOSITION]

await writer.write(b"--" + self._boundary + b"\r\n")
await writer.write(part._binary_headers)

Expand Down
7 changes: 7 additions & 0 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(self):
Dreamsorcerer marked this conversation as resolved.
Show resolved Hide resolved
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")
Expand Down