2024-02-29 10:16:42 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-10-29 14:46:42 +00:00
|
|
|
import os
|
2022-01-11 08:28:39 +00:00
|
|
|
import typing
|
2022-05-19 18:33:03 +00:00
|
|
|
from contextlib import nullcontext as does_not_raise
|
2024-02-06 22:01:44 +00:00
|
|
|
from pathlib import Path
|
2018-10-29 14:46:42 +00:00
|
|
|
|
2022-02-03 11:34:25 +00:00
|
|
|
import pytest
|
|
|
|
|
2022-05-19 18:33:03 +00:00
|
|
|
from starlette.applications import Starlette
|
2023-07-23 21:41:50 +00:00
|
|
|
from starlette.datastructures import UploadFile
|
|
|
|
from starlette.formparsers import MultiPartException, _user_safe_decode
|
2018-10-12 17:15:04 +00:00
|
|
|
from starlette.requests import Request
|
2018-10-29 14:46:42 +00:00
|
|
|
from starlette.responses import JSONResponse
|
2022-05-19 18:33:03 +00:00
|
|
|
from starlette.routing import Mount
|
2024-02-06 22:01:44 +00:00
|
|
|
from starlette.types import ASGIApp, Receive, Scope, Send
|
2024-07-27 09:31:16 +00:00
|
|
|
from tests.types import TestClientFactory
|
2018-10-12 17:15:04 +00:00
|
|
|
|
|
|
|
|
2023-07-23 21:41:50 +00:00
|
|
|
class ForceMultipartDict(typing.Dict[typing.Any, typing.Any]):
|
2024-02-06 22:01:44 +00:00
|
|
|
def __bool__(self) -> bool:
|
2018-10-12 17:15:04 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# FORCE_MULTIPART is an empty dict that boolean-evaluates as `True`.
|
|
|
|
FORCE_MULTIPART = ForceMultipartDict()
|
|
|
|
|
|
|
|
|
2024-02-06 22:01:44 +00:00
|
|
|
async def app(scope: Scope, receive: Receive, send: Send) -> None:
|
2019-03-19 16:03:19 +00:00
|
|
|
request = Request(scope, receive)
|
|
|
|
data = await request.form()
|
2024-02-29 10:16:42 +00:00
|
|
|
output: dict[str, typing.Any] = {}
|
2019-03-19 16:03:19 +00:00
|
|
|
for key, value in data.items():
|
|
|
|
if isinstance(value, UploadFile):
|
|
|
|
content = await value.read()
|
|
|
|
output[key] = {
|
|
|
|
"filename": value.filename,
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": value.size,
|
2019-03-19 16:03:19 +00:00
|
|
|
"content": content.decode(),
|
|
|
|
"content_type": value.content_type,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
output[key] = value
|
|
|
|
await request.close()
|
|
|
|
response = JSONResponse(output)
|
|
|
|
await response(scope, receive, send)
|
|
|
|
|
|
|
|
|
2024-02-06 22:01:44 +00:00
|
|
|
async def multi_items_app(scope: Scope, receive: Receive, send: Send) -> None:
|
2019-03-19 16:03:19 +00:00
|
|
|
request = Request(scope, receive)
|
|
|
|
data = await request.form()
|
2024-02-29 10:16:42 +00:00
|
|
|
output: dict[str, list[typing.Any]] = {}
|
2019-03-19 16:03:19 +00:00
|
|
|
for key, value in data.multi_items():
|
|
|
|
if key not in output:
|
|
|
|
output[key] = []
|
|
|
|
if isinstance(value, UploadFile):
|
|
|
|
content = await value.read()
|
|
|
|
output[key].append(
|
|
|
|
{
|
2019-02-01 09:06:33 +00:00
|
|
|
"filename": value.filename,
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": value.size,
|
2019-02-01 09:06:33 +00:00
|
|
|
"content": content.decode(),
|
|
|
|
"content_type": value.content_type,
|
|
|
|
}
|
2019-03-19 16:03:19 +00:00
|
|
|
)
|
|
|
|
else:
|
|
|
|
output[key].append(value)
|
|
|
|
await request.close()
|
|
|
|
response = JSONResponse(output)
|
|
|
|
await response(scope, receive, send)
|
|
|
|
|
|
|
|
|
2024-02-06 22:01:44 +00:00
|
|
|
async def app_with_headers(scope: Scope, receive: Receive, send: Send) -> None:
|
2022-01-07 19:06:37 +00:00
|
|
|
request = Request(scope, receive)
|
|
|
|
data = await request.form()
|
2024-02-29 10:16:42 +00:00
|
|
|
output: dict[str, typing.Any] = {}
|
2022-01-07 19:06:37 +00:00
|
|
|
for key, value in data.items():
|
|
|
|
if isinstance(value, UploadFile):
|
|
|
|
content = await value.read()
|
|
|
|
output[key] = {
|
|
|
|
"filename": value.filename,
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": value.size,
|
2022-01-07 19:06:37 +00:00
|
|
|
"content": content.decode(),
|
|
|
|
"content_type": value.content_type,
|
|
|
|
"headers": list(value.headers.items()),
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
output[key] = value
|
|
|
|
await request.close()
|
|
|
|
response = JSONResponse(output)
|
|
|
|
await response(scope, receive, send)
|
|
|
|
|
|
|
|
|
2024-02-06 22:01:44 +00:00
|
|
|
async def app_read_body(scope: Scope, receive: Receive, send: Send) -> None:
|
2019-03-19 16:03:19 +00:00
|
|
|
request = Request(scope, receive)
|
|
|
|
# Read bytes, to force request.stream() to return the already parsed body
|
2019-05-13 14:22:59 +00:00
|
|
|
await request.body()
|
2019-03-19 16:03:19 +00:00
|
|
|
data = await request.form()
|
|
|
|
output = {}
|
|
|
|
for key, value in data.items():
|
|
|
|
output[key] = value
|
|
|
|
await request.close()
|
|
|
|
response = JSONResponse(output)
|
|
|
|
await response(scope, receive, send)
|
2019-01-04 09:44:42 +00:00
|
|
|
|
|
|
|
|
2024-02-06 22:01:44 +00:00
|
|
|
def make_app_max_parts(max_files: int = 1000, max_fields: int = 1000) -> ASGIApp:
|
|
|
|
async def app(scope: Scope, receive: Receive, send: Send) -> None:
|
2023-02-14 08:01:32 +00:00
|
|
|
request = Request(scope, receive)
|
|
|
|
data = await request.form(max_files=max_files, max_fields=max_fields)
|
2024-02-29 10:16:42 +00:00
|
|
|
output: dict[str, typing.Any] = {}
|
2023-02-14 08:01:32 +00:00
|
|
|
for key, value in data.items():
|
|
|
|
if isinstance(value, UploadFile):
|
|
|
|
content = await value.read()
|
|
|
|
output[key] = {
|
|
|
|
"filename": value.filename,
|
|
|
|
"size": value.size,
|
|
|
|
"content": content.decode(),
|
|
|
|
"content_type": value.content_type,
|
|
|
|
}
|
|
|
|
else:
|
|
|
|
output[key] = value
|
|
|
|
await request.close()
|
|
|
|
response = JSONResponse(output)
|
|
|
|
await response(scope, receive, send)
|
|
|
|
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_request_data(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2018-10-12 17:15:04 +00:00
|
|
|
response = client.post("/", data={"some": "data"}, files=FORCE_MULTIPART)
|
|
|
|
assert response.json() == {"some": "data"}
|
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_request_files(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2018-10-12 17:15:04 +00:00
|
|
|
path = os.path.join(tmpdir, "test.txt")
|
|
|
|
with open(path, "wb") as file:
|
|
|
|
file.write(b"<file content>")
|
|
|
|
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2019-01-08 15:12:26 +00:00
|
|
|
with open(path, "rb") as f:
|
|
|
|
response = client.post("/", files={"test": f})
|
|
|
|
assert response.json() == {
|
2019-02-01 09:06:33 +00:00
|
|
|
"test": {
|
|
|
|
"filename": "test.txt",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 14,
|
2019-02-01 09:06:33 +00:00
|
|
|
"content": "<file content>",
|
2022-09-06 05:43:32 +00:00
|
|
|
"content_type": "text/plain",
|
2019-02-01 09:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_request_files_with_content_type(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2019-02-01 09:06:33 +00:00
|
|
|
path = os.path.join(tmpdir, "test.txt")
|
|
|
|
with open(path, "wb") as file:
|
|
|
|
file.write(b"<file content>")
|
|
|
|
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2019-02-01 09:06:33 +00:00
|
|
|
with open(path, "rb") as f:
|
|
|
|
response = client.post("/", files={"test": ("test.txt", f, "text/plain")})
|
|
|
|
assert response.json() == {
|
|
|
|
"test": {
|
|
|
|
"filename": "test.txt",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 14,
|
2019-02-01 09:06:33 +00:00
|
|
|
"content": "<file content>",
|
|
|
|
"content_type": "text/plain",
|
|
|
|
}
|
2019-01-08 15:12:26 +00:00
|
|
|
}
|
2018-10-12 17:15:04 +00:00
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_request_multiple_files(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2018-12-18 10:26:06 +00:00
|
|
|
path1 = os.path.join(tmpdir, "test1.txt")
|
|
|
|
with open(path1, "wb") as file:
|
|
|
|
file.write(b"<file1 content>")
|
|
|
|
|
|
|
|
path2 = os.path.join(tmpdir, "test2.txt")
|
|
|
|
with open(path2, "wb") as file:
|
|
|
|
file.write(b"<file2 content>")
|
|
|
|
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2019-01-08 15:12:26 +00:00
|
|
|
with open(path1, "rb") as f1, open(path2, "rb") as f2:
|
2024-09-01 13:11:01 +00:00
|
|
|
response = client.post("/", files={"test1": f1, "test2": ("test2.txt", f2, "text/plain")})
|
2019-01-08 15:12:26 +00:00
|
|
|
assert response.json() == {
|
2019-02-01 09:06:33 +00:00
|
|
|
"test1": {
|
|
|
|
"filename": "test1.txt",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 15,
|
2019-02-01 09:06:33 +00:00
|
|
|
"content": "<file1 content>",
|
2022-09-06 05:43:32 +00:00
|
|
|
"content_type": "text/plain",
|
2019-02-01 09:06:33 +00:00
|
|
|
},
|
|
|
|
"test2": {
|
|
|
|
"filename": "test2.txt",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 15,
|
2019-02-01 09:06:33 +00:00
|
|
|
"content": "<file2 content>",
|
|
|
|
"content_type": "text/plain",
|
|
|
|
},
|
2019-01-08 15:12:26 +00:00
|
|
|
}
|
2018-12-18 10:26:06 +00:00
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_request_multiple_files_with_headers(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2022-01-07 19:06:37 +00:00
|
|
|
path1 = os.path.join(tmpdir, "test1.txt")
|
|
|
|
with open(path1, "wb") as file:
|
|
|
|
file.write(b"<file1 content>")
|
|
|
|
|
|
|
|
path2 = os.path.join(tmpdir, "test2.txt")
|
|
|
|
with open(path2, "wb") as file:
|
|
|
|
file.write(b"<file2 content>")
|
|
|
|
|
|
|
|
client = test_client_factory(app_with_headers)
|
|
|
|
with open(path1, "rb") as f1, open(path2, "rb") as f2:
|
|
|
|
response = client.post(
|
|
|
|
"/",
|
|
|
|
files=[
|
|
|
|
("test1", (None, f1)),
|
|
|
|
("test2", ("test2.txt", f2, "text/plain", {"x-custom": "f2"})),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
assert response.json() == {
|
|
|
|
"test1": "<file1 content>",
|
|
|
|
"test2": {
|
|
|
|
"filename": "test2.txt",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 15,
|
2022-01-07 19:06:37 +00:00
|
|
|
"content": "<file2 content>",
|
|
|
|
"content_type": "text/plain",
|
|
|
|
"headers": [
|
|
|
|
[
|
|
|
|
"content-disposition",
|
|
|
|
'form-data; name="test2"; filename="test2.txt"',
|
|
|
|
],
|
|
|
|
["x-custom", "f2"],
|
2022-09-06 05:43:32 +00:00
|
|
|
["content-type", "text/plain"],
|
2022-01-07 19:06:37 +00:00
|
|
|
],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_multi_items(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2019-01-22 19:47:47 +00:00
|
|
|
path1 = os.path.join(tmpdir, "test1.txt")
|
|
|
|
with open(path1, "wb") as file:
|
|
|
|
file.write(b"<file1 content>")
|
|
|
|
|
|
|
|
path2 = os.path.join(tmpdir, "test2.txt")
|
|
|
|
with open(path2, "wb") as file:
|
|
|
|
file.write(b"<file2 content>")
|
|
|
|
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(multi_items_app)
|
2019-01-22 19:47:47 +00:00
|
|
|
with open(path1, "rb") as f1, open(path2, "rb") as f2:
|
|
|
|
response = client.post(
|
2019-02-01 09:06:33 +00:00
|
|
|
"/",
|
2022-09-06 05:43:32 +00:00
|
|
|
data={"test1": "abc"},
|
2019-02-01 09:06:33 +00:00
|
|
|
files=[("test1", f1), ("test1", ("test2.txt", f2, "text/plain"))],
|
2019-01-22 19:47:47 +00:00
|
|
|
)
|
|
|
|
assert response.json() == {
|
|
|
|
"test1": [
|
|
|
|
"abc",
|
2019-02-01 09:06:33 +00:00
|
|
|
{
|
|
|
|
"filename": "test1.txt",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 15,
|
2019-02-01 09:06:33 +00:00
|
|
|
"content": "<file1 content>",
|
2022-09-06 05:43:32 +00:00
|
|
|
"content_type": "text/plain",
|
2019-02-01 09:06:33 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"filename": "test2.txt",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 15,
|
2019-02-01 09:06:33 +00:00
|
|
|
"content": "<file2 content>",
|
|
|
|
"content_type": "text/plain",
|
|
|
|
},
|
2019-01-22 19:47:47 +00:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_request_mixed_files_and_data(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2018-12-18 10:26:06 +00:00
|
|
|
response = client.post(
|
|
|
|
"/",
|
|
|
|
data=(
|
|
|
|
# data
|
2024-02-06 22:01:44 +00:00
|
|
|
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n" # type: ignore
|
2018-12-18 10:26:06 +00:00
|
|
|
b'Content-Disposition: form-data; name="field0"\r\n\r\n'
|
|
|
|
b"value0\r\n"
|
|
|
|
# file
|
|
|
|
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n"
|
|
|
|
b'Content-Disposition: form-data; name="file"; filename="file.txt"\r\n'
|
|
|
|
b"Content-Type: text/plain\r\n\r\n"
|
|
|
|
b"<file content>\r\n"
|
|
|
|
# data
|
|
|
|
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n"
|
|
|
|
b'Content-Disposition: form-data; name="field1"\r\n\r\n'
|
|
|
|
b"value1\r\n"
|
|
|
|
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c--\r\n"
|
|
|
|
),
|
2024-09-01 13:11:01 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=a7f7ac8d4e2e437c877bb7b8d7cc549c")},
|
2018-12-18 10:26:06 +00:00
|
|
|
)
|
|
|
|
assert response.json() == {
|
2019-02-01 09:06:33 +00:00
|
|
|
"file": {
|
|
|
|
"filename": "file.txt",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 14,
|
2019-02-01 09:06:33 +00:00
|
|
|
"content": "<file content>",
|
|
|
|
"content_type": "text/plain",
|
|
|
|
},
|
2018-12-18 10:26:06 +00:00
|
|
|
"field0": "value0",
|
|
|
|
"field1": "value1",
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_request_with_charset_for_filename(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2019-06-24 17:59:09 +00:00
|
|
|
response = client.post(
|
|
|
|
"/",
|
|
|
|
data=(
|
|
|
|
# file
|
2024-02-06 22:01:44 +00:00
|
|
|
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n" # type: ignore
|
2024-09-01 13:11:01 +00:00
|
|
|
b'Content-Disposition: form-data; name="file"; filename="\xe6\x96\x87\xe6\x9b\xb8.txt"\r\n'
|
2019-06-24 17:59:09 +00:00
|
|
|
b"Content-Type: text/plain\r\n\r\n"
|
|
|
|
b"<file content>\r\n"
|
|
|
|
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c--\r\n"
|
|
|
|
),
|
2024-09-01 13:11:01 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; charset=utf-8; boundary=a7f7ac8d4e2e437c877bb7b8d7cc549c")},
|
2019-06-24 17:59:09 +00:00
|
|
|
)
|
|
|
|
assert response.json() == {
|
|
|
|
"file": {
|
2019-07-28 14:19:47 +00:00
|
|
|
"filename": "文書.txt",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 14,
|
2019-06-24 17:59:09 +00:00
|
|
|
"content": "<file content>",
|
|
|
|
"content_type": "text/plain",
|
|
|
|
}
|
|
|
|
}
|
2019-07-28 14:19:47 +00:00
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_request_without_charset_for_filename(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2019-07-28 14:19:47 +00:00
|
|
|
response = client.post(
|
|
|
|
"/",
|
|
|
|
data=(
|
|
|
|
# file
|
2024-02-06 22:01:44 +00:00
|
|
|
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n" # type: ignore
|
2024-09-01 13:11:01 +00:00
|
|
|
b'Content-Disposition: form-data; name="file"; filename="\xe7\x94\xbb\xe5\x83\x8f.jpg"\r\n'
|
2019-07-28 14:19:47 +00:00
|
|
|
b"Content-Type: image/jpeg\r\n\r\n"
|
|
|
|
b"<file content>\r\n"
|
|
|
|
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c--\r\n"
|
|
|
|
),
|
2024-09-01 13:11:01 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=a7f7ac8d4e2e437c877bb7b8d7cc549c")},
|
2019-07-28 14:19:47 +00:00
|
|
|
)
|
|
|
|
assert response.json() == {
|
|
|
|
"file": {
|
|
|
|
"filename": "画像.jpg",
|
2023-02-06 12:02:29 +00:00
|
|
|
"size": 14,
|
2019-07-28 14:19:47 +00:00
|
|
|
"content": "<file content>",
|
|
|
|
"content_type": "image/jpeg",
|
|
|
|
}
|
|
|
|
}
|
2019-06-24 17:59:09 +00:00
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_request_with_encoded_value(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2019-06-24 17:59:09 +00:00
|
|
|
response = client.post(
|
|
|
|
"/",
|
|
|
|
data=(
|
2024-02-06 22:01:44 +00:00
|
|
|
b"--20b303e711c4ab8c443184ac833ab00f\r\n" # type: ignore
|
2019-06-24 17:59:09 +00:00
|
|
|
b"Content-Disposition: form-data; "
|
|
|
|
b'name="value"\r\n\r\n'
|
|
|
|
b"Transf\xc3\xa9rer\r\n"
|
|
|
|
b"--20b303e711c4ab8c443184ac833ab00f--\r\n"
|
|
|
|
),
|
2024-09-01 13:11:01 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; charset=utf-8; boundary=20b303e711c4ab8c443184ac833ab00f")},
|
2019-06-24 17:59:09 +00:00
|
|
|
)
|
|
|
|
assert response.json() == {"value": "Transférer"}
|
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_urlencoded_request_data(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2018-10-12 17:15:04 +00:00
|
|
|
response = client.post("/", data={"some": "data"})
|
|
|
|
assert response.json() == {"some": "data"}
|
|
|
|
|
|
|
|
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_no_request_data(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2018-10-12 17:15:04 +00:00
|
|
|
response = client.post("/")
|
|
|
|
assert response.json() == {}
|
2019-01-04 09:44:42 +00:00
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_urlencoded_percent_encoding(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2019-01-14 12:28:16 +00:00
|
|
|
response = client.post("/", data={"some": "da ta"})
|
|
|
|
assert response.json() == {"some": "da ta"}
|
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_urlencoded_percent_encoding_keys(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app)
|
2019-01-14 12:28:16 +00:00
|
|
|
response = client.post("/", data={"so me": "data"})
|
|
|
|
assert response.json() == {"so me": "data"}
|
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_urlencoded_multi_field_app_reads_body(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app_read_body)
|
2019-01-04 09:44:42 +00:00
|
|
|
response = client.post("/", data={"some": "data", "second": "key pair"})
|
|
|
|
assert response.json() == {"some": "data", "second": "key pair"}
|
|
|
|
|
|
|
|
|
2024-09-01 13:11:01 +00:00
|
|
|
def test_multipart_multi_field_app_reads_body(tmpdir: Path, test_client_factory: TestClientFactory) -> None:
|
2021-06-28 20:36:13 +00:00
|
|
|
client = test_client_factory(app_read_body)
|
2024-09-01 13:11:01 +00:00
|
|
|
response = client.post("/", data={"some": "data", "second": "key pair"}, files=FORCE_MULTIPART)
|
2019-01-04 09:44:42 +00:00
|
|
|
assert response.json() == {"some": "data", "second": "key pair"}
|
2019-06-24 17:59:09 +00:00
|
|
|
|
|
|
|
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_user_safe_decode_helper() -> None:
|
2019-06-24 17:59:09 +00:00
|
|
|
result = _user_safe_decode(b"\xc4\x99\xc5\xbc\xc4\x87", "utf-8")
|
|
|
|
assert result == "ężć"
|
|
|
|
|
|
|
|
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_user_safe_decode_ignores_wrong_charset() -> None:
|
2019-06-24 17:59:09 +00:00
|
|
|
result = _user_safe_decode(b"abc", "latin-8")
|
|
|
|
assert result == "abc"
|
2022-02-03 11:34:25 +00:00
|
|
|
|
|
|
|
|
2022-05-19 18:33:03 +00:00
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app,expectation",
|
|
|
|
[
|
|
|
|
(app, pytest.raises(MultiPartException)),
|
|
|
|
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
|
|
|
|
],
|
|
|
|
)
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_missing_boundary_parameter(
|
|
|
|
app: ASGIApp,
|
|
|
|
expectation: typing.ContextManager[Exception],
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
2022-02-03 11:34:25 +00:00
|
|
|
client = test_client_factory(app)
|
2022-05-19 18:33:03 +00:00
|
|
|
with expectation:
|
|
|
|
res = client.post(
|
2022-02-03 11:34:25 +00:00
|
|
|
"/",
|
|
|
|
data=(
|
|
|
|
# file
|
2024-09-01 13:11:01 +00:00
|
|
|
b'Content-Disposition: form-data; name="file"; filename="\xe6\x96\x87\xe6\x9b\xb8.txt"\r\n' # type: ignore
|
2022-02-03 11:34:25 +00:00
|
|
|
b"Content-Type: text/plain\r\n\r\n"
|
|
|
|
b"<file content>\r\n"
|
|
|
|
),
|
|
|
|
headers={"Content-Type": "multipart/form-data; charset=utf-8"},
|
|
|
|
)
|
2022-05-19 18:33:03 +00:00
|
|
|
assert res.status_code == 400
|
|
|
|
assert res.text == "Missing boundary in multipart."
|
2022-05-24 05:31:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app,expectation",
|
|
|
|
[
|
|
|
|
(app, pytest.raises(MultiPartException)),
|
|
|
|
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_missing_name_parameter_on_content_disposition(
|
2024-02-06 22:01:44 +00:00
|
|
|
app: ASGIApp,
|
|
|
|
expectation: typing.ContextManager[Exception],
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
2022-05-24 05:31:57 +00:00
|
|
|
client = test_client_factory(app)
|
|
|
|
with expectation:
|
|
|
|
res = client.post(
|
|
|
|
"/",
|
|
|
|
data=(
|
|
|
|
# data
|
2024-02-06 22:01:44 +00:00
|
|
|
b"--a7f7ac8d4e2e437c877bb7b8d7cc549c\r\n" # type: ignore
|
2022-05-24 05:31:57 +00:00
|
|
|
b'Content-Disposition: form-data; ="field0"\r\n\r\n'
|
|
|
|
b"value0\r\n"
|
|
|
|
),
|
2024-09-01 13:11:01 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=a7f7ac8d4e2e437c877bb7b8d7cc549c")},
|
2022-05-24 05:31:57 +00:00
|
|
|
)
|
|
|
|
assert res.status_code == 400
|
2024-09-01 13:11:01 +00:00
|
|
|
assert res.text == 'The Content-Disposition header field "name" must be provided.'
|
2023-02-14 08:01:32 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app,expectation",
|
|
|
|
[
|
|
|
|
(app, pytest.raises(MultiPartException)),
|
|
|
|
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
|
|
|
|
],
|
|
|
|
)
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_too_many_fields_raise(
|
|
|
|
app: ASGIApp,
|
|
|
|
expectation: typing.ContextManager[Exception],
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
2023-02-14 08:01:32 +00:00
|
|
|
client = test_client_factory(app)
|
|
|
|
fields = []
|
|
|
|
for i in range(1001):
|
2024-09-01 13:11:01 +00:00
|
|
|
fields.append("--B\r\n" f'Content-Disposition: form-data; name="N{i}";\r\n\r\n' "\r\n")
|
2023-02-14 08:01:32 +00:00
|
|
|
data = "".join(fields).encode("utf-8")
|
|
|
|
with expectation:
|
|
|
|
res = client.post(
|
|
|
|
"/",
|
2024-02-06 22:01:44 +00:00
|
|
|
data=data, # type: ignore
|
2023-02-14 08:01:32 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=B")},
|
|
|
|
)
|
|
|
|
assert res.status_code == 400
|
|
|
|
assert res.text == "Too many fields. Maximum number of fields is 1000."
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app,expectation",
|
|
|
|
[
|
|
|
|
(app, pytest.raises(MultiPartException)),
|
|
|
|
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
|
|
|
|
],
|
|
|
|
)
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_too_many_files_raise(
|
|
|
|
app: ASGIApp,
|
|
|
|
expectation: typing.ContextManager[Exception],
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
2023-02-14 08:01:32 +00:00
|
|
|
client = test_client_factory(app)
|
|
|
|
fields = []
|
|
|
|
for i in range(1001):
|
2024-09-01 13:11:01 +00:00
|
|
|
fields.append("--B\r\n" f'Content-Disposition: form-data; name="N{i}"; filename="F{i}";\r\n\r\n' "\r\n")
|
2023-02-14 08:01:32 +00:00
|
|
|
data = "".join(fields).encode("utf-8")
|
|
|
|
with expectation:
|
|
|
|
res = client.post(
|
|
|
|
"/",
|
2024-02-06 22:01:44 +00:00
|
|
|
data=data, # type: ignore
|
2023-02-14 08:01:32 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=B")},
|
|
|
|
)
|
|
|
|
assert res.status_code == 400
|
|
|
|
assert res.text == "Too many files. Maximum number of files is 1000."
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app,expectation",
|
|
|
|
[
|
|
|
|
(app, pytest.raises(MultiPartException)),
|
|
|
|
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
|
|
|
|
],
|
|
|
|
)
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_too_many_files_single_field_raise(
|
|
|
|
app: ASGIApp,
|
|
|
|
expectation: typing.ContextManager[Exception],
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
2023-02-14 08:01:32 +00:00
|
|
|
client = test_client_factory(app)
|
|
|
|
fields = []
|
|
|
|
for i in range(1001):
|
|
|
|
# This uses the same field name "N" for all files, equivalent to a
|
|
|
|
# multifile upload form field
|
2024-09-01 13:11:01 +00:00
|
|
|
fields.append("--B\r\n" f'Content-Disposition: form-data; name="N"; filename="F{i}";\r\n\r\n' "\r\n")
|
2023-02-14 08:01:32 +00:00
|
|
|
data = "".join(fields).encode("utf-8")
|
|
|
|
with expectation:
|
|
|
|
res = client.post(
|
|
|
|
"/",
|
2024-02-06 22:01:44 +00:00
|
|
|
data=data, # type: ignore
|
2023-02-14 08:01:32 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=B")},
|
|
|
|
)
|
|
|
|
assert res.status_code == 400
|
|
|
|
assert res.text == "Too many files. Maximum number of files is 1000."
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app,expectation",
|
|
|
|
[
|
|
|
|
(app, pytest.raises(MultiPartException)),
|
|
|
|
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
|
|
|
|
],
|
|
|
|
)
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_too_many_files_and_fields_raise(
|
|
|
|
app: ASGIApp,
|
|
|
|
expectation: typing.ContextManager[Exception],
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
2023-02-14 08:01:32 +00:00
|
|
|
client = test_client_factory(app)
|
|
|
|
fields = []
|
|
|
|
for i in range(1001):
|
2024-09-01 13:11:01 +00:00
|
|
|
fields.append("--B\r\n" f'Content-Disposition: form-data; name="F{i}"; filename="F{i}";\r\n\r\n' "\r\n")
|
|
|
|
fields.append("--B\r\n" f'Content-Disposition: form-data; name="N{i}";\r\n\r\n' "\r\n")
|
2023-02-14 08:01:32 +00:00
|
|
|
data = "".join(fields).encode("utf-8")
|
|
|
|
with expectation:
|
|
|
|
res = client.post(
|
|
|
|
"/",
|
2024-02-06 22:01:44 +00:00
|
|
|
data=data, # type: ignore
|
2023-02-14 08:01:32 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=B")},
|
|
|
|
)
|
|
|
|
assert res.status_code == 400
|
|
|
|
assert res.text == "Too many files. Maximum number of files is 1000."
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app,expectation",
|
|
|
|
[
|
|
|
|
(make_app_max_parts(max_fields=1), pytest.raises(MultiPartException)),
|
|
|
|
(
|
|
|
|
Starlette(routes=[Mount("/", app=make_app_max_parts(max_fields=1))]),
|
|
|
|
does_not_raise(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_max_fields_is_customizable_low_raises(
|
|
|
|
app: ASGIApp,
|
|
|
|
expectation: typing.ContextManager[Exception],
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
2023-02-14 08:01:32 +00:00
|
|
|
client = test_client_factory(app)
|
|
|
|
fields = []
|
|
|
|
for i in range(2):
|
2024-09-01 13:11:01 +00:00
|
|
|
fields.append("--B\r\n" f'Content-Disposition: form-data; name="N{i}";\r\n\r\n' "\r\n")
|
2023-02-14 08:01:32 +00:00
|
|
|
data = "".join(fields).encode("utf-8")
|
|
|
|
with expectation:
|
|
|
|
res = client.post(
|
|
|
|
"/",
|
2024-02-06 22:01:44 +00:00
|
|
|
data=data, # type: ignore
|
2023-02-14 08:01:32 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=B")},
|
|
|
|
)
|
|
|
|
assert res.status_code == 400
|
|
|
|
assert res.text == "Too many fields. Maximum number of fields is 1."
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app,expectation",
|
|
|
|
[
|
|
|
|
(make_app_max_parts(max_files=1), pytest.raises(MultiPartException)),
|
|
|
|
(
|
|
|
|
Starlette(routes=[Mount("/", app=make_app_max_parts(max_files=1))]),
|
|
|
|
does_not_raise(),
|
|
|
|
),
|
|
|
|
],
|
|
|
|
)
|
2024-02-06 22:01:44 +00:00
|
|
|
def test_max_files_is_customizable_low_raises(
|
|
|
|
app: ASGIApp,
|
|
|
|
expectation: typing.ContextManager[Exception],
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
2023-02-14 08:01:32 +00:00
|
|
|
client = test_client_factory(app)
|
|
|
|
fields = []
|
|
|
|
for i in range(2):
|
2024-09-01 13:11:01 +00:00
|
|
|
fields.append("--B\r\n" f'Content-Disposition: form-data; name="F{i}"; filename="F{i}";\r\n\r\n' "\r\n")
|
2023-02-14 08:01:32 +00:00
|
|
|
data = "".join(fields).encode("utf-8")
|
|
|
|
with expectation:
|
|
|
|
res = client.post(
|
|
|
|
"/",
|
2024-02-06 22:01:44 +00:00
|
|
|
data=data, # type: ignore
|
2023-02-14 08:01:32 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=B")},
|
|
|
|
)
|
|
|
|
assert res.status_code == 400
|
|
|
|
assert res.text == "Too many files. Maximum number of files is 1."
|
|
|
|
|
|
|
|
|
2024-10-15 06:40:51 +00:00
|
|
|
def test_max_fields_is_customizable_high(test_client_factory: TestClientFactory) -> None:
|
2023-02-14 08:01:32 +00:00
|
|
|
client = test_client_factory(make_app_max_parts(max_fields=2000, max_files=2000))
|
|
|
|
fields = []
|
|
|
|
for i in range(2000):
|
2024-09-01 13:11:01 +00:00
|
|
|
fields.append("--B\r\n" f'Content-Disposition: form-data; name="N{i}";\r\n\r\n' "\r\n")
|
|
|
|
fields.append("--B\r\n" f'Content-Disposition: form-data; name="F{i}"; filename="F{i}";\r\n\r\n' "\r\n")
|
2023-02-14 08:01:32 +00:00
|
|
|
data = "".join(fields).encode("utf-8")
|
|
|
|
data += b"--B--\r\n"
|
|
|
|
res = client.post(
|
|
|
|
"/",
|
2024-02-06 22:01:44 +00:00
|
|
|
data=data, # type: ignore
|
2023-02-14 08:01:32 +00:00
|
|
|
headers={"Content-Type": ("multipart/form-data; boundary=B")},
|
|
|
|
)
|
|
|
|
assert res.status_code == 200
|
|
|
|
res_data = res.json()
|
|
|
|
assert res_data["N1999"] == ""
|
|
|
|
assert res_data["F1999"] == {
|
|
|
|
"filename": "F1999",
|
|
|
|
"size": 0,
|
|
|
|
"content": "",
|
|
|
|
"content_type": None,
|
|
|
|
}
|
2024-10-15 06:40:51 +00:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"app,expectation",
|
|
|
|
[
|
|
|
|
(app, pytest.raises(MultiPartException)),
|
|
|
|
(Starlette(routes=[Mount("/", app=app)]), does_not_raise()),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_max_part_size_exceeds_limit(
|
|
|
|
app: ASGIApp,
|
|
|
|
expectation: typing.ContextManager[Exception],
|
|
|
|
test_client_factory: TestClientFactory,
|
|
|
|
) -> None:
|
|
|
|
client = test_client_factory(app)
|
|
|
|
boundary = "------------------------4K1ON9fZkj9uCUmqLHRbbR"
|
|
|
|
|
|
|
|
multipart_data = (
|
|
|
|
f"--{boundary}\r\n"
|
|
|
|
f'Content-Disposition: form-data; name="small"\r\n\r\n'
|
|
|
|
"small content\r\n"
|
|
|
|
f"--{boundary}\r\n"
|
|
|
|
f'Content-Disposition: form-data; name="large"\r\n\r\n'
|
|
|
|
+ ("x" * 1024 * 1024 + "x") # 1MB + 1 byte of data
|
|
|
|
+ "\r\n"
|
|
|
|
f"--{boundary}--\r\n"
|
|
|
|
).encode("utf-8")
|
|
|
|
|
|
|
|
headers = {
|
|
|
|
"Content-Type": f"multipart/form-data; boundary={boundary}",
|
|
|
|
"Transfer-Encoding": "chunked",
|
|
|
|
}
|
|
|
|
|
|
|
|
with expectation:
|
|
|
|
response = client.post("/", data=multipart_data, headers=headers) # type: ignore
|
|
|
|
assert response.status_code == 400
|
|
|
|
assert response.text == "Part exceeded maximum size of 1024KB."
|