@@ -234,3 +234,73 @@ def handler(request: httpx.Request) -> httpx.Response:
234234 assert response .output_file .name == "async-converted.pdf"
235235 assert response .output_file .type == "application/pdf"
236236 assert str (response .input_id ) == str (input_file .id )
237+
238+
239+ @pytest .mark .asyncio
240+ async def test_async_convert_office_to_pdf_request_customization (
241+ monkeypatch : pytest .MonkeyPatch ,
242+ ) -> None :
243+ monkeypatch .delenv ("PDFREST_API_KEY" , raising = False )
244+ input_file = make_source_file (
245+ str (PdfRestFileID .generate (2 )),
246+ "application/vnd.ms-excel" ,
247+ "sheet.xls" ,
248+ )
249+ output_id = str (PdfRestFileID .generate ())
250+ captured_timeout : dict [str , float | dict [str , float ] | None ] = {}
251+
252+ def handler (request : httpx .Request ) -> httpx .Response :
253+ if request .method == "POST" and request .url .path == "/pdf" :
254+ assert request .url .params ["trace" ] == "async"
255+ assert request .headers ["X-Debug" ] == "async"
256+ captured_timeout ["value" ] = request .extensions .get ("timeout" )
257+ payload = json .loads (request .content .decode ("utf-8" ))
258+ assert payload ["debug" ] is True
259+ assert payload ["compression" ] == "lossy"
260+ assert payload ["downsample" ] == 600
261+ assert payload ["locale" ] == "US"
262+ assert payload ["id" ] == str (input_file .id )
263+ assert payload ["output" ] == "async-custom"
264+ return httpx .Response (
265+ 200 ,
266+ json = {
267+ "inputId" : [input_file .id ],
268+ "outputId" : [output_id ],
269+ },
270+ )
271+ if request .method == "GET" and request .url .path == f"/resource/{ output_id } " :
272+ assert request .url .params ["trace" ] == "async"
273+ assert request .headers ["X-Debug" ] == "async"
274+ return httpx .Response (
275+ 200 ,
276+ json = build_file_info_payload (
277+ output_id , "async-custom.pdf" , "application/pdf"
278+ ),
279+ )
280+ msg = f"Unexpected request { request .method } { request .url } "
281+ raise AssertionError (msg )
282+
283+ transport = httpx .MockTransport (handler )
284+ async with AsyncPdfRestClient (api_key = ASYNC_API_KEY , transport = transport ) as client :
285+ response = await client .convert_office_to_pdf (
286+ input_file ,
287+ output = "async-custom" ,
288+ compression = "lossy" ,
289+ downsample = 600 ,
290+ locale = "US" ,
291+ extra_query = {"trace" : "async" },
292+ extra_headers = {"X-Debug" : "async" },
293+ extra_body = {"debug" : True },
294+ timeout = 0.6 ,
295+ )
296+
297+ assert isinstance (response , PdfRestFileBasedResponse )
298+ assert response .output_file .name == "async-custom.pdf"
299+ timeout_value = captured_timeout ["value" ]
300+ assert timeout_value is not None
301+ if isinstance (timeout_value , dict ):
302+ assert all (
303+ component == pytest .approx (0.6 ) for component in timeout_value .values ()
304+ )
305+ else :
306+ assert timeout_value == pytest .approx (0.6 )
0 commit comments