allow IO[bytes] for send_file argument
This commit is contained in:
parent
85cc710464
commit
439f1f2997
3 changed files with 37 additions and 1 deletions
|
|
@ -1,6 +1,14 @@
|
|||
Version 3.1.1
|
||||
-------------
|
||||
|
||||
Unreleased
|
||||
|
||||
- Fix ``helpers.send_file`` type hint to allow ``IO[bytes]``, not just
|
||||
``BinaryIO``. Fixes :issue:`5776`.
|
||||
|
||||
Version 3.1.1
|
||||
-------------
|
||||
|
||||
Released 2025-05-13
|
||||
|
||||
- Fix signing key selection order when key rotation is enabled via
|
||||
|
|
|
|||
|
|
@ -398,7 +398,7 @@ def _prepare_send_file_kwargs(**kwargs: t.Any) -> dict[str, t.Any]:
|
|||
|
||||
|
||||
def send_file(
|
||||
path_or_file: os.PathLike[t.AnyStr] | str | t.BinaryIO,
|
||||
path_or_file: os.PathLike[t.AnyStr] | str | t.IO[bytes],
|
||||
mimetype: str | None = None,
|
||||
as_attachment: bool = False,
|
||||
download_name: str | None = None,
|
||||
|
|
|
|||
|
|
@ -98,6 +98,34 @@ class TestSendfile:
|
|||
assert rv.data.strip() == b"Hello Subdomain"
|
||||
rv.close()
|
||||
|
||||
def test_send_file_io_bytes(self, app, req_ctx):
|
||||
"""
|
||||
Tests that send_file can handle file-like objects typed as IO[bytes].
|
||||
This verifies the fix for issue #5776.
|
||||
"""
|
||||
file_content = b"this is file content"
|
||||
file_obj: IO[bytes] = io.BytesIO(file_content)
|
||||
|
||||
# 2. send_file 함수에 as_attachment=True 인자를 추가합니다.
|
||||
rv = flask.send_file(
|
||||
file_obj,
|
||||
download_name="test.txt",
|
||||
mimetype="text/plain",
|
||||
as_attachment=True, # <-- 이 부분을 추가하세요.
|
||||
)
|
||||
|
||||
# 3. 응답 검증
|
||||
assert rv.status_code == 200
|
||||
assert rv.direct_passthrough
|
||||
|
||||
rv.direct_passthrough = False
|
||||
|
||||
assert rv.data == file_content
|
||||
assert rv.mimetype == "text/plain"
|
||||
# 이제 이 assert문이 정상적으로 통과합니다.
|
||||
assert "attachment; filename=test.txt" in rv.headers["Content-Disposition"]
|
||||
rv.close()
|
||||
|
||||
|
||||
class TestUrlFor:
|
||||
def test_url_for_with_anchor(self, app, req_ctx):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue