Skip to content

Commit b266159

Browse files
cliffhallclaude
andcommitted
test(fetch): wire-level isError, bounded pagination walk, proxy-free invalid-URL test
Address Copilot round 1 on #4971. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent d85dfcf commit b266159

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

‎src/fetch/tests/test_protocol.py‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
from __future__ import annotations
1717

18+
import os
1819
from importlib.metadata import version
1920
from typing import Any
2021

@@ -195,7 +196,7 @@ async def test_schema_violations_are_rejected_by_the_sdk(
195196
# of the message is jsonschema's wording, which varies by version.
196197
async with connect() as (session, _):
197198
result = await call(session, arguments)
198-
assert result.isError is True
199+
assert wire(result)["isError"] is True
199200
text = text_of(result)
200201
assert text.startswith("Input validation error: ")
201202
assert offending in text
@@ -208,7 +209,7 @@ async def test_invalid_url_is_rejected_by_pydantic(web: FakeWeb, url: str) -> No
208209
# and its ValidationError (a ValueError) becomes an isError result.
209210
async with connect() as (session, _):
210211
result = await call(session, {"url": url})
211-
assert result.isError is True
212+
assert wire(result)["isError"] is True
212213
text = text_of(result)
213214
assert "1 validation error for Fetch" in text
214215
assert "url" in text
@@ -223,7 +224,7 @@ async def test_call_tool_never_checks_the_tool_name(web: FakeWeb) -> None:
223224
web.add(PAGE, plain("fetched anyway"))
224225
async with connect() as (session, _):
225226
result = await session.call_tool("nope", {"url": PAGE})
226-
assert result.isError is False
227+
assert wire(result)["isError"] is False
227228
assert text_of(result).endswith("fetched anyway")
228229
assert web.urls() == [ROBOTS, PAGE]
229230

@@ -233,7 +234,7 @@ async def test_unknown_tool_name_still_validates_through_pydantic(
233234
) -> None:
234235
async with connect() as (session, _):
235236
result = await session.call_tool("nope", {})
236-
assert result.isError is True
237+
assert wire(result)["isError"] is True
237238
assert "url" in text_of(result)
238239
assert "Field required" in text_of(result)
239240

@@ -251,7 +252,7 @@ async def test_robots_4xx_other_than_401_403_allows_fetch(
251252
web.add(PAGE, plain("ok"))
252253
async with connect() as (session, _):
253254
result = await call(session, {"url": PAGE})
254-
assert result.isError is False
255+
assert wire(result)["isError"] is False
255256
assert text_of(result).endswith("ok")
256257

257258

@@ -298,7 +299,7 @@ async def test_robots_disallow_blocks_fetch(web: FakeWeb) -> None:
298299
web.add(ROBOTS, plain(robots))
299300
async with connect() as (session, _):
300301
result = await call(session, {"url": PAGE})
301-
assert result.isError is True
302+
assert wire(result)["isError"] is True
302303
assert text_of(result) == (
303304
f"The sites robots.txt ({ROBOTS}), specifies that autonomous fetching of this page is not allowed, "
304305
f"<useragent>{DEFAULT_USER_AGENT_AUTONOMOUS}</useragent>\n"
@@ -317,7 +318,7 @@ async def test_robots_allow_then_fetches_page(web: FakeWeb) -> None:
317318
web.add(PAGE, plain("public"))
318319
async with connect() as (session, _):
319320
result = await call(session, {"url": PAGE})
320-
assert result.isError is False
321+
assert wire(result)["isError"] is False
321322
assert web.urls() == [ROBOTS, PAGE]
322323
for request in web.requests:
323324
assert request.headers["user-agent"] == DEFAULT_USER_AGENT_AUTONOMOUS
@@ -328,15 +329,15 @@ async def test_robots_comment_lines_are_ignored(web: FakeWeb) -> None:
328329
web.add(PAGE, plain("ok"))
329330
async with connect() as (session, _):
330331
result = await call(session, {"url": PAGE})
331-
assert result.isError is False
332+
assert wire(result)["isError"] is False
332333

333334

334335
async def test_robots_5xx_body_is_parsed_as_rules(web: FakeWeb) -> None:
335336
# Only 4xx short-circuits; a 5xx response body is still parsed as rules.
336337
web.add(ROBOTS, plain("User-agent: *\nDisallow: /", status=503))
337338
async with connect() as (session, _):
338339
result = await call(session, {"url": PAGE})
339-
assert result.isError is True
340+
assert wire(result)["isError"] is True
340341
assert "specifies that autonomous fetching of this page is not allowed" in (
341342
text_of(result)
342343
)
@@ -350,15 +351,15 @@ async def test_robots_redirect_is_followed(web: FakeWeb) -> None:
350351
web.add("https://www.example.com/robots.txt", plain("User-agent: *\nDisallow: /"))
351352
async with connect() as (session, _):
352353
result = await call(session, {"url": PAGE})
353-
assert result.isError is True
354+
assert wire(result)["isError"] is True
354355
assert web.urls() == [ROBOTS, "https://www.example.com/robots.txt"]
355356

356357

357358
async def test_robots_rules_match_the_custom_user_agent(web: FakeWeb) -> None:
358359
web.add(ROBOTS, plain("User-agent: MyBot\nDisallow: /\n\nUser-agent: *\nAllow: /"))
359360
async with connect(custom_user_agent="MyBot") as (session, _):
360361
result = await call(session, {"url": PAGE})
361-
assert result.isError is True
362+
assert wire(result)["isError"] is True
362363
assert "<useragent>MyBot</useragent>" in text_of(result)
363364
assert web.requests[0].headers["user-agent"] == "MyBot"
364365

@@ -368,7 +369,7 @@ async def test_ignore_robots_txt_skips_the_check(web: FakeWeb) -> None:
368369
web.add(PAGE, plain("ok"))
369370
async with connect(ignore_robots_txt=True) as (session, _):
370371
result = await call(session, {"url": PAGE})
371-
assert result.isError is False
372+
assert wire(result)["isError"] is False
372373
assert web.urls() == [PAGE]
373374

374375

@@ -382,7 +383,7 @@ async def test_html_is_extracted_to_markdown(web: FakeWeb) -> None:
382383
web.add(PAGE, html(ARTICLE_HTML))
383384
async with connect(ignore_robots_txt=True) as (session, _):
384385
result = await call(session, {"url": PAGE})
385-
assert result.isError is False
386+
assert wire(result)["isError"] is False
386387
text = text_of(result)
387388
assert text.startswith(f"Contents of {PAGE}:\n")
388389
assert "[link](https://example.com/x)" in text
@@ -585,7 +586,9 @@ async def test_pagination_walks_the_whole_document(web: FakeWeb) -> None:
585586
pieces: list[str] = []
586587
async with connect(ignore_robots_txt=True) as (session, _):
587588
start = 0
588-
while True:
589+
# Four pages plus the exhaustion call. Bounded, so a regression that
590+
# ignores start_index fails here instead of looping forever.
591+
for _ in range(5):
589592
text = text_of(
590593
await call(
591594
session, {"url": PAGE, "start_index": start, "max_length": 7}
@@ -597,6 +600,8 @@ async def test_pagination_walks_the_whole_document(web: FakeWeb) -> None:
597600
chunk = body.split("\n\n<error>", 1)[0]
598601
pieces.append(chunk)
599602
start += len(chunk)
603+
else:
604+
pytest.fail(f"pagination did not reach the end; pages so far: {pieces}")
600605
assert "".join(pieces) == ALPHABET
601606
assert pieces == ["abcdefg", "hijklmn", "opqrstu", "vwxyz"]
602607

@@ -615,7 +620,7 @@ async def test_redirect_to_private_address_is_followed(web: FakeWeb) -> None:
615620
web.add(metadata, plain("instance-secret"))
616621
async with connect() as (session, _):
617622
result = await call(session, {"url": PAGE})
618-
assert result.isError is False
623+
assert wire(result)["isError"] is False
619624
assert text_of(result) == f"{RAW_PREFIX_PLAIN}Contents of {PAGE}:\ninstance-secret"
620625
# robots.txt is only consulted for the original host, never the target.
621626
assert web.urls() == [ROBOTS, PAGE, metadata]
@@ -650,7 +655,7 @@ async def test_proxy_url_is_passed_to_every_client(web: FakeWeb) -> None:
650655
async with connect(proxy_url=proxy) as (session, _):
651656
result = await call(session, {"url": PAGE})
652657
await session.get_prompt("fetch", {"url": PAGE})
653-
assert result.isError is False
658+
assert wire(result)["isError"] is False
654659
assert web.client_kwargs == [{"proxy": proxy}, {"proxy": proxy}, {"proxy": proxy}]
655660

656661

@@ -781,10 +786,17 @@ async def test_get_prompt_never_checks_the_prompt_name(web: FakeWeb) -> None:
781786
assert wire(result)["description"] == f"Contents of {PAGE}"
782787

783788

784-
async def test_get_prompt_does_not_validate_the_url() -> None:
789+
async def test_get_prompt_does_not_validate_the_url(
790+
monkeypatch: pytest.MonkeyPatch,
791+
) -> None:
785792
# The prompt passes the raw string to httpx; an unusable URL comes back as
786793
# a "Failed to fetch" prompt message rather than a validation error.
787794
# No `web` fixture: the real transport rejects the URL before any I/O.
795+
# Proxy variables are cleared so the real client cannot pick up one from
796+
# the environment (a socks:// proxy would fail on the missing socksio).
797+
for name in list(os.environ):
798+
if name.lower() in ("http_proxy", "https_proxy", "all_proxy"):
799+
monkeypatch.delenv(name)
788800
async with connect() as (session, _):
789801
result = await session.get_prompt("fetch", {"url": "not a url"})
790802
data = wire(result)

0 commit comments

Comments
 (0)