🐛 Fix: 设置 file request 默认 filename (#2284)

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Ju4tCode <42488585+yanyongyu@users.noreply.github.com>
This commit is contained in:
eya46 2023-08-23 14:14:07 +08:00 committed by GitHub
parent 4bfea99e54
commit 2e8230e9f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View File

@ -125,7 +125,7 @@ class Request:
files_ = files.items() if isinstance(files, dict) else files files_ = files.items() if isinstance(files, dict) else files
for name, file_info in files_: for name, file_info in files_:
if not isinstance(file_info, tuple): if not isinstance(file_info, tuple):
self.files.append((name, (None, file_info, None))) self.files.append((name, (name, file_info, None)))
elif len(file_info) == 2: elif len(file_info) == 2:
self.files.append((name, (file_info[0], file_info[1], None))) self.files.append((name, (file_info[0], file_info[1], None)))
else: else:

View File

@ -233,6 +233,23 @@ async def test_http_client(driver: Driver, server_url: URL):
content="test", content="test",
) )
response = await driver.request(request) response = await driver.request(request)
assert server_url.host is not None
request_raw_url = Request(
"POST",
(
server_url.scheme.encode("ascii"),
server_url.host.encode("ascii"),
server_url.port,
server_url.path.encode("ascii"),
),
params={"param": "test"},
headers={"X-Test": "test"},
cookies={"session": "test"},
content="test",
)
assert (
request.url == request_raw_url.url
), "request.url should be equal to request_raw_url.url"
assert response.status_code == 200 assert response.status_code == 200
assert response.content assert response.content
data = json.loads(response.content) data = json.loads(response.content)
@ -265,7 +282,11 @@ async def test_http_client(driver: Driver, server_url: URL):
"POST", "POST",
server_url, server_url,
data={"form": "test"}, data={"form": "test"},
files={"test": ("test.txt", b"test")}, files=[
("test1", b"test"),
("test2", ("test.txt", b"test")),
("test3", ("test.txt", b"test", "text/plain")),
],
) )
response = await driver.request(request) response = await driver.request(request)
assert response.status_code == 200 assert response.status_code == 200
@ -273,7 +294,11 @@ async def test_http_client(driver: Driver, server_url: URL):
data = json.loads(response.content) data = json.loads(response.content)
assert data["method"] == "POST" assert data["method"] == "POST"
assert data["form"] == {"form": "test"} assert data["form"] == {"form": "test"}
assert data["files"] == {"test": "test"} assert data["files"] == {
"test1": "test",
"test2": "test",
"test3": "test",
}, "file parsing error"
await asyncio.sleep(1) await asyncio.sleep(1)