Skip to content

Commit

Permalink
Add Content-Disposition automatically (#8335)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamsorcerer authored Apr 15, 2024
1 parent 482e6cd commit 5a6949d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/8335.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added default Content-Disposition in multipart/form-data responses -- by :user:`Dreamsorcerer`.
4 changes: 4 additions & 0 deletions aiohttp/multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,10 @@ def append_payload(self, payload: Payload) -> Payload:
not {CONTENT_ENCODING, CONTENT_LENGTH, CONTENT_TRANSFER_ENCODING}
& payload.headers.keys()
)
# Set default Content-Disposition in case user doesn't create one
if CONTENT_DISPOSITION not in payload.headers:
name = f"section-{len(self._parts)}"
payload.set_content_disposition("form-data", name=name)
else:
# compression
encoding = payload.headers.get(CONTENT_ENCODING, "").lower()
Expand Down
22 changes: 17 additions & 5 deletions tests/test_multipart.py
Original file line number Diff line number Diff line change
Expand Up @@ -1165,12 +1165,24 @@ 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):
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]
part = writer.append("some-data")
part.set_content_disposition("form-data", name="method")
assert 'name="method"' in part.headers[CONTENT_DISPOSITION]

def test_automatic_content_disposition(self):
writer = aiohttp.MultipartWriter("form-data")
writer.append_json(())
part = payload.StringPayload("foo")
part.set_content_disposition("form-data", name="second")
writer.append_payload(part)
writer.append("foo")

disps = tuple(p[0].headers[CONTENT_DISPOSITION] for p in writer._parts)
assert 'name="section-0"' in disps[0]
assert 'name="second"' in disps[1]
assert 'name="section-2"' in disps[2]

def test_with(self) -> None:
with aiohttp.MultipartWriter(boundary=":") as writer:
Expand Down

0 comments on commit 5a6949d

Please sign in to comment.