Skip to content

Commit 593dab9

Browse files
cliffhallclaude
andcommitted
test(fetch): skip the BOM extraction case on Windows, check its detection directly
readabilipy 0.2.0 writes the page to a temp file in the locale encoding, so a page with a byte-order mark cannot be extracted on Windows (#5303). That crash predates #5134 and is not what it changes, so the protocol case is skipped on Windows and the BOM handling of _body_looks_like_html is asserted directly on every platform. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent adc62f7 commit 593dab9

1 file changed

Lines changed: 45 additions & 5 deletions

File tree

‎src/fetch/tests/test_missing_content_type.py‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,18 @@
1212

1313
from __future__ import annotations
1414

15+
import sys
16+
1517
import httpx
1618
import pytest
1719
from mcp.types import CallToolResult
1820

21+
from mcp_server_fetch.server import _body_looks_like_html
22+
1923
from .harness import FakeWeb, connect
2024

2125
PAGE = "https://example.com/page"
26+
BOM = "\ufeff"
2227

2328
ARTICLE_BODY = """<head><title>Test Page</title></head><body>
2429
<article>
@@ -96,13 +101,23 @@ async def test_empty_body_without_content_type_is_not_simplified(
96101
@pytest.mark.parametrize(
97102
"body",
98103
[
99-
f"<!DOCTYPE html>\n{ARTICLE_BODY}",
100-
f"<!doctype html>{ARTICLE_BODY}",
101-
f"\n <!DocType HTML>\n{ARTICLE_BODY}",
104+
pytest.param(f"<!DOCTYPE html>\n{ARTICLE_BODY}", id="doctype-upper"),
105+
pytest.param(f"<!doctype html>{ARTICLE_BODY}", id="doctype-lower"),
106+
pytest.param(
107+
f"{BOM}\n <!DocType HTML>\n{ARTICLE_BODY}",
108+
id="bom-and-whitespace",
109+
# readabilipy 0.2.0 writes the page to a temp file in the locale
110+
# encoding, which cannot hold a BOM on Windows (cp1252). The
111+
# detection itself is covered on every platform by
112+
# test_body_detection_skips_bom_and_whitespace below.
113+
marks=pytest.mark.skipif(
114+
sys.platform == "win32",
115+
reason="readabilipy 0.2.0 cannot extract non-cp1252 pages on Windows (#5303)",
116+
),
117+
),
102118
# More leading whitespace than the 100-character "<html" window.
103-
f"{' ' * 120}<HTML>{ARTICLE_BODY}</HTML>",
119+
pytest.param(f"{' ' * 120}<HTML>{ARTICLE_BODY}</HTML>", id="long-indent"),
104120
],
105-
ids=["doctype-upper", "doctype-lower", "bom-and-whitespace", "long-indent"],
106121
)
107122
async def test_html_without_content_type_is_still_simplified(
108123
web: FakeWeb, body: str
@@ -136,3 +151,28 @@ async def test_doctype_alone_does_not_override_a_declared_content_type(
136151
200, content=body.encode(), headers={"content-type": "text/plain"}
137152
)
138153
assert_raw(await fetch(web, response), body)
154+
155+
156+
@pytest.mark.parametrize(
157+
"body, expected",
158+
[
159+
(f"{BOM}<!doctype html><p>x</p>", True),
160+
(f"{BOM}\n <!DocType HTML>\n<p>x</p>", True),
161+
(f"{BOM} \t\r\n<HTML><p>x</p></HTML>", True),
162+
(f'{BOM}{{"key": "value"}}', False),
163+
(f"{BOM}plain text", False),
164+
("", False),
165+
],
166+
ids=[
167+
"bom-doctype",
168+
"bom-whitespace-doctype",
169+
"bom-html",
170+
"bom-json",
171+
"bom-text",
172+
"empty",
173+
],
174+
)
175+
def test_body_detection_skips_bom_and_whitespace(body: str, expected: bool) -> None:
176+
# Runs on every platform: what #5134 decides, without readabilipy's
177+
# extraction (which cannot handle a BOM on Windows, #5303).
178+
assert _body_looks_like_html(body) is expected

0 commit comments

Comments
 (0)