|
| 1 | +"""Tests for the raw-content prefix and the declared dependencies (#5321). |
| 2 | +
|
| 3 | +A body fetch returns raw names its Content-Type in the prefix it adds. A |
| 4 | +response with no Content-Type left that slot empty ("Content type cannot be |
| 5 | +simplified"), so the prefix now says the type was not declared. The prefix tests |
| 6 | +drive ``serve()`` through an MCP client with HTTP served by ``harness.FakeWeb``. |
| 7 | +
|
| 8 | +``server.py`` imports ``bs4`` directly, so ``beautifulsoup4`` must be a declared |
| 9 | +dependency of the package rather than one that only arrives through |
| 10 | +readabilipy; the last test reads the installed package's metadata to pin that. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import re |
| 16 | +from importlib.metadata import requires |
| 17 | + |
| 18 | +import httpx |
| 19 | + |
| 20 | +from .harness import FakeWeb, connect |
| 21 | + |
| 22 | +PAGE = "https://example.com/data" |
| 23 | + |
| 24 | + |
| 25 | +async def fetch_text(web: FakeWeb, response: httpx.Response) -> str: |
| 26 | + web.add(PAGE, response) |
| 27 | + async with connect(ignore_robots_txt=True) as (session, _): |
| 28 | + result = await session.call_tool("fetch", {"url": PAGE}) |
| 29 | + data = result.model_dump(by_alias=True, mode="json", exclude_none=True) |
| 30 | + assert data["isError"] is False |
| 31 | + return data["content"][0]["text"] |
| 32 | + |
| 33 | + |
| 34 | +async def test_a_missing_content_type_is_named_as_not_declared( |
| 35 | + web: FakeWeb, |
| 36 | +) -> None: |
| 37 | + response = httpx.Response(200, content=b'{"key": "value"}') |
| 38 | + assert "content-type" not in response.headers |
| 39 | + |
| 40 | + text = await fetch_text(web, response) |
| 41 | + |
| 42 | + assert text.startswith( |
| 43 | + "Content type (not declared) cannot be simplified to markdown, " |
| 44 | + "but here is the raw content:\n" |
| 45 | + ) |
| 46 | + assert "Content type " not in text |
| 47 | + |
| 48 | + |
| 49 | +async def test_a_declared_content_type_is_still_named(web: FakeWeb) -> None: |
| 50 | + response = httpx.Response( |
| 51 | + 200, content=b"a,b\n1,2\n", headers={"content-type": "text/csv"} |
| 52 | + ) |
| 53 | + |
| 54 | + text = await fetch_text(web, response) |
| 55 | + |
| 56 | + assert text.startswith( |
| 57 | + "Content type text/csv cannot be simplified to markdown, " |
| 58 | + "but here is the raw content:\n" |
| 59 | + ) |
| 60 | + |
| 61 | + |
| 62 | +def test_beautifulsoup4_is_a_declared_dependency() -> None: |
| 63 | + names = { |
| 64 | + re.split(r"[\s\[<>=!~;]", requirement, maxsplit=1)[0].lower() |
| 65 | + for requirement in requires("mcp-server-fetch") or [] |
| 66 | + } |
| 67 | + assert "beautifulsoup4" in names |
0 commit comments