|
| 1 | +"""Shared harness for the fetch server tests (conftest.py re-exports the fixtures). |
| 2 | +
|
| 3 | +Two pieces: |
| 4 | +
|
| 5 | +- ``connect(...)`` runs the real ``serve()`` in-process and yields an |
| 6 | + initialized ``ClientSession`` linked to it over in-memory streams, so tests |
| 7 | + go through the SDK's validation and error mapping exactly as a client would. |
| 8 | +- ``web`` replaces ``httpx.AsyncClient`` with one backed by an |
| 9 | + ``httpx.MockTransport``, so no test touches the network. It records every |
| 10 | + request and the keyword arguments each client was constructed with. |
| 11 | +""" |
| 12 | + |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import tempfile |
| 16 | +from contextlib import asynccontextmanager |
| 17 | +from dataclasses import dataclass, field |
| 18 | +from pathlib import Path |
| 19 | +from typing import Any, AsyncIterator, Callable, Union |
| 20 | + |
| 21 | +import anyio |
| 22 | +import httpx |
| 23 | +import pytest |
| 24 | +import readabilipy.simple_json |
| 25 | +from mcp import ClientSession |
| 26 | +from mcp.shared.memory import create_client_server_memory_streams |
| 27 | +from mcp.types import InitializeResult |
| 28 | + |
| 29 | +import mcp_server_fetch.server as server_module |
| 30 | + |
| 31 | +Responder = Union[httpx.Response, Callable[[httpx.Request], httpx.Response]] |
| 32 | + |
| 33 | + |
| 34 | +@dataclass |
| 35 | +class FakeWeb: |
| 36 | + """A router of URL -> response used as the transport of every AsyncClient.""" |
| 37 | + |
| 38 | + routes: dict[str, Responder] = field(default_factory=dict) |
| 39 | + requests: list[httpx.Request] = field(default_factory=list) |
| 40 | + client_kwargs: list[dict[str, Any]] = field(default_factory=list) |
| 41 | + |
| 42 | + def add(self, url: str, response: Responder) -> None: |
| 43 | + self.routes[url] = response |
| 44 | + |
| 45 | + def handle(self, request: httpx.Request) -> httpx.Response: |
| 46 | + self.requests.append(request) |
| 47 | + responder = self.routes.get(str(request.url)) |
| 48 | + if responder is None: |
| 49 | + return httpx.Response(404, content=b"no route in FakeWeb") |
| 50 | + if isinstance(responder, httpx.Response): |
| 51 | + return responder |
| 52 | + return responder(request) |
| 53 | + |
| 54 | + def urls(self) -> list[str]: |
| 55 | + return [str(r.url) for r in self.requests] |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +def web(monkeypatch: pytest.MonkeyPatch) -> FakeWeb: |
| 60 | + fake = FakeWeb() |
| 61 | + real_async_client = httpx.AsyncClient |
| 62 | + |
| 63 | + def factory(**kwargs: Any) -> httpx.AsyncClient: |
| 64 | + fake.client_kwargs.append(dict(kwargs)) |
| 65 | + # A proxy would mount a real proxy transport in front of ours, so it is |
| 66 | + # recorded above and dropped here. Passing a transport also stops httpx |
| 67 | + # from picking up HTTP(S)_PROXY from the environment. |
| 68 | + kwargs.pop("proxy", None) |
| 69 | + return real_async_client(transport=httpx.MockTransport(fake.handle), **kwargs) |
| 70 | + |
| 71 | + # server.py imports AsyncClient from httpx inside each function, so |
| 72 | + # patching the attribute on the httpx module is what it picks up. |
| 73 | + monkeypatch.setattr(httpx, "AsyncClient", factory) |
| 74 | + return fake |
| 75 | + |
| 76 | + |
| 77 | +@asynccontextmanager |
| 78 | +async def connect( |
| 79 | + **serve_kwargs: Any, |
| 80 | +) -> AsyncIterator[tuple[ClientSession, InitializeResult]]: |
| 81 | + """Run ``serve(**serve_kwargs)`` in-process and yield a connected client.""" |
| 82 | + async with create_client_server_memory_streams() as ( |
| 83 | + client_streams, |
| 84 | + server_streams, |
| 85 | + ): |
| 86 | + |
| 87 | + @asynccontextmanager |
| 88 | + async def fake_stdio_server() -> AsyncIterator[Any]: |
| 89 | + yield server_streams |
| 90 | + |
| 91 | + original = server_module.stdio_server |
| 92 | + server_module.stdio_server = fake_stdio_server # type: ignore[assignment] |
| 93 | + try: |
| 94 | + async with anyio.create_task_group() as tg: |
| 95 | + tg.start_soon(lambda: server_module.serve(**serve_kwargs)) |
| 96 | + async with ClientSession(*client_streams) as session: |
| 97 | + init = await session.initialize() |
| 98 | + yield session, init |
| 99 | + tg.cancel_scope.cancel() |
| 100 | + finally: |
| 101 | + server_module.stdio_server = original |
| 102 | + |
| 103 | + |
| 104 | +@pytest.fixture |
| 105 | +def node_readability() -> None: |
| 106 | + """Require the Readability.js path that readabilipy takes when Node is on PATH. |
| 107 | +
|
| 108 | + readabilipy silently falls back to a pure-Python extractor without Node, |
| 109 | + and the two produce different Markdown, so each HTML test names the mode |
| 110 | + it pins. Node is a prerequisite of this repository's gate, so a missing |
| 111 | + Node fails the test rather than skipping it. |
| 112 | + """ |
| 113 | + if not readabilipy.simple_json.have_node(): |
| 114 | + pytest.fail("Node.js >= 10 must be on PATH for the Readability.js path") |
| 115 | + |
| 116 | + |
| 117 | +@pytest.fixture |
| 118 | +def python_readability(monkeypatch: pytest.MonkeyPatch) -> None: |
| 119 | + """Force readabilipy's pure-Python fallback, as on a machine without Node.""" |
| 120 | + monkeypatch.setattr(readabilipy.simple_json, "have_node", lambda: False) |
| 121 | + |
| 122 | + |
| 123 | +@pytest.fixture(autouse=True) |
| 124 | +def private_tempdir(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> None: |
| 125 | + """Give each test its own ``tempfile.gettempdir()``. |
| 126 | +
|
| 127 | + readabilipy's Readability.js path writes its input and output to fixed |
| 128 | + names (``full.html``, ``article.json``) in the shared temp directory, so |
| 129 | + two test runs at once (parallel gate runs in separate worktrees, say) read |
| 130 | + each other's pages. |
| 131 | + """ |
| 132 | + monkeypatch.setattr(tempfile, "tempdir", str(tmp_path)) |
0 commit comments