Skip to content

Commit fa9e513

Browse files
tests: Add sync/async test coverage for create_from_paths behavior
- Add tests ensuring `create_from_paths` supports content-type-only uploads. - Add async test verifying `create_from_paths` supports metadata in requests. - These directly exercise the two missing branches in _parse_path_spec and validate that the multipart payload includes the right Content-Type and custom headers. Assisted-by: Codex
1 parent ec26190 commit fa9e513

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

tests/test_files.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,45 @@ def handler(request: httpx.Request) -> httpx.Response:
605605
_assert_file_matches_payload(response[0], info_payload)
606606

607607

608+
def test_files_create_from_paths_supports_content_type_only() -> None:
609+
uploaded_file_id = str(uuid.uuid4())
610+
info_payload = _build_file_info_payload(uploaded_file_id, "report.pdf")
611+
612+
def handler(request: httpx.Request) -> httpx.Response:
613+
if request.method == "POST" and request.url.path == "/upload":
614+
body = request.content
615+
assert b'filename="report.pdf"' in body
616+
assert b"Content-Type: application/test-pdf" in body
617+
return httpx.Response(
618+
200,
619+
json={
620+
"files": [
621+
{"name": "report.pdf", "id": uploaded_file_id},
622+
]
623+
},
624+
)
625+
if request.method == "GET":
626+
assert request.url.params["format"] == "info"
627+
return httpx.Response(200, json=info_payload)
628+
msg = f"Unexpected request: {request.method} {request.url}"
629+
raise AssertionError(msg)
630+
631+
transport = httpx.MockTransport(handler)
632+
report_pdf = get_test_resource_path("report.pdf")
633+
with PdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
634+
response = client.files.create_from_paths(
635+
[
636+
(
637+
report_pdf,
638+
"application/test-pdf",
639+
)
640+
]
641+
)
642+
643+
assert len(response) == 1
644+
_assert_file_matches_payload(response[0], info_payload)
645+
646+
608647
class TestDownloadHelpers:
609648
@pytest.fixture
610649
def client(self) -> Iterator[tuple[PdfRestClient, bytes, dict[str, Any]]]:
@@ -1237,6 +1276,48 @@ def handler(request: httpx.Request) -> httpx.Response:
12371276
_assert_file_matches_payload(response[0], info_payload)
12381277

12391278

1279+
@pytest.mark.asyncio
1280+
async def test_async_files_create_from_paths_supports_metadata() -> None:
1281+
uploaded_file_id = str(uuid.uuid4())
1282+
info_payload = _build_file_info_payload(uploaded_file_id, "report.pdf")
1283+
1284+
def handler(request: httpx.Request) -> httpx.Response:
1285+
if request.method == "POST" and request.url.path == "/upload":
1286+
body = request.content
1287+
assert b'filename="report.pdf"' in body
1288+
assert b"Content-Type: application/test-pdf" in body
1289+
assert b"X-Custom: header" in body
1290+
return httpx.Response(
1291+
200,
1292+
json={
1293+
"files": [
1294+
{"name": "report.pdf", "id": uploaded_file_id},
1295+
]
1296+
},
1297+
)
1298+
if request.method == "GET":
1299+
assert request.url.params["format"] == "info"
1300+
return httpx.Response(200, json=info_payload)
1301+
msg = f"Unexpected request: {request.method} {request.url}"
1302+
raise AssertionError(msg)
1303+
1304+
transport = httpx.MockTransport(handler)
1305+
report_pdf = get_test_resource_path("report.pdf")
1306+
async with AsyncPdfRestClient(api_key=VALID_API_KEY, transport=transport) as client:
1307+
response = await client.files.create_from_paths(
1308+
[
1309+
(
1310+
report_pdf,
1311+
"application/test-pdf",
1312+
{"X-Custom": "header"},
1313+
)
1314+
]
1315+
)
1316+
1317+
assert len(response) == 1
1318+
_assert_file_matches_payload(response[0], info_payload)
1319+
1320+
12401321
def test_live_file_create(pdfrest_api_key: str, pdfrest_live_base_url: str) -> None:
12411322
with PdfRestClient(
12421323
api_key=pdfrest_api_key, base_url=pdfrest_live_base_url

0 commit comments

Comments
 (0)