@@ -160,6 +160,78 @@ async def test_http_fetch_size_limit() -> None:
160160 assert len (content ) == 500
161161
162162
163+ @pytest .mark .asyncio
164+ @pytest .mark .parametrize (("requested" , "expected" ), [(- 20 , 1 ), (10 ** 9 , 100_000 )])
165+ async def test_http_fetch_clamps_max_chars (requested : int , expected : int ) -> None :
166+ response = httpx .Response (
167+ 200 , text = "x" * 100_001 , request = httpx .Request ("GET" , "https://example.com" )
168+ )
169+ with (
170+ patch ("agentkit.tools.builtin.http_fetch.resolve_host_ips" , return_value = [PUBLIC_IP ]),
171+ _patch_stream (response ),
172+ ):
173+ assert len (await http_fetch ("https://example.com" , max_chars = requested )) == expected
174+
175+
176+ @pytest .mark .asyncio
177+ @pytest .mark .parametrize (("requested" , "expected" ), [(- 1.0 , 0.1 ), (10 ** 9 , 30.0 )])
178+ async def test_http_fetch_clamps_timeout (requested : float , expected : float ) -> None :
179+ response = httpx .Response (200 , text = "ok" , request = httpx .Request ("GET" , "https://example.com" ))
180+ original_client = httpx .AsyncClient
181+ with (
182+ patch ("agentkit.tools.builtin.http_fetch.resolve_host_ips" , return_value = [PUBLIC_IP ]),
183+ _patch_stream (response ),
184+ patch (
185+ "agentkit.tools.builtin.http_fetch.httpx.AsyncClient" , wraps = original_client
186+ ) as client ,
187+ ):
188+ assert await http_fetch ("https://example.com" , timeout_s = requested ) == "ok"
189+ assert client .call_args .kwargs ["timeout" ].connect == expected
190+
191+
192+ @pytest .mark .asyncio
193+ @pytest .mark .parametrize ("timeout" , [float ("nan" ), float ("inf" ), float ("-inf" )])
194+ async def test_http_fetch_rejects_nonfinite_timeout (timeout : float ) -> None :
195+ with pytest .raises (ValueError , match = "finite" ):
196+ await http_fetch ("https://example.com" , timeout_s = timeout )
197+
198+
199+ @pytest .mark .asyncio
200+ @pytest .mark .parametrize ("content_type" , ["application/octet-stream" , "image/png" ])
201+ async def test_http_fetch_rejects_binary_before_reading (content_type : str ) -> None :
202+ response = httpx .Response (
203+ 200 ,
204+ headers = {"Content-Type" : content_type },
205+ content = b"\x00 \xff " ,
206+ request = httpx .Request ("GET" , "https://example.com" ),
207+ )
208+ with (
209+ patch ("agentkit.tools.builtin.http_fetch.resolve_host_ips" , return_value = [PUBLIC_IP ]),
210+ _patch_stream (response ),
211+ patch .object (response , "aiter_bytes" , side_effect = AssertionError ("body read" )),
212+ pytest .raises (ValueError , match = "Unsupported content type" ),
213+ ):
214+ await http_fetch ("https://example.com" )
215+
216+
217+ @pytest .mark .asyncio
218+ @pytest .mark .parametrize (
219+ "content_type" , ["application/json; charset=utf-8" , "application/problem+json" , "text/html" ]
220+ )
221+ async def test_http_fetch_accepts_textual_media_types (content_type : str ) -> None :
222+ response = httpx .Response (
223+ 200 ,
224+ headers = {"Content-Type" : content_type },
225+ content = b"hello" ,
226+ request = httpx .Request ("GET" , "https://example.com" ),
227+ )
228+ with (
229+ patch ("agentkit.tools.builtin.http_fetch.resolve_host_ips" , return_value = [PUBLIC_IP ]),
230+ _patch_stream (response ),
231+ ):
232+ assert await http_fetch ("https://example.com" ) == "hello"
233+
234+
163235@pytest .mark .asyncio
164236async def test_http_fetch_rejects_rebind_to_unvalidated_peer () -> None :
165237 """A connection that lands off the validated addresses must be refused.
0 commit comments