Files
claude-seo/tests/test_fetch_page_decoding.py
Agrici DanielandClaude Fable 5.1 3854391ecb test: skip cleanly instead of aborting collection on missing optional deps
17 scripts exit(1) at import time when an optional dependency (requests,
bs4, playwright, googleapiclient, google.analytics) is missing. Any test
module importing one of these at module scope turned a missing dependency
into a hard pytest collection error for the whole session instead of a
per-module skip.

Add pytest.importorskip guards to every test module that imports one of
these scripts at module scope and did not already guard it: 8 of the 17
scripts (bing_webmaster, commoncrawl_graph, crux_history, fetch_page,
moz_api, nlp_analyze, pagespeed_check, parse_html) plus every test module
that hits the same failure transitively through url_safety.py's own hard
requests requirement (domain_history, gbp_deprecation_lint, parasite_risk,
agent_ux_check/render_page, indexnow_submit, url_safety's own suite, and
the backlinks_auth hardening tests added earlier in this branch).

Verified in a throwaway venv with only pytest and beautifulsoup4
installed: the suite now completes (206 passed, 24 skipped) instead of
interrupting collection with 9 errors.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015qjDbqj88SjLGY4MdQKGB8
2026-09-10 21:06:22 +03:00

46 lines
1.5 KiB
Python

"""Deterministic response decoding for fetch_page.py."""
from __future__ import annotations
import sys
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parents[1]
SCRIPTS_DIR = REPO_ROOT / "scripts"
if str(SCRIPTS_DIR) not in sys.path:
sys.path.insert(0, str(SCRIPTS_DIR))
pytest.importorskip("requests")
import fetch_page # noqa: E402
class FakeResponse:
def __init__(self, content: bytes, content_type: str = "") -> None:
self.content = content
self.headers = {"Content-Type": content_type} if content_type else {}
def test_no_charset_defaults_to_utf8_with_replacement() -> None:
response = FakeResponse("Café".encode("utf-8"))
assert fetch_page._decode_response_content(response) == "Café"
def test_explicit_charset_from_content_type_wins() -> None:
response = FakeResponse("Café".encode("iso-8859-1"), "text/html; charset=iso-8859-1")
assert fetch_page._decode_response_content(response) == "Café"
def test_meta_charset_is_used_when_header_has_no_charset() -> None:
html = '<meta charset="windows-1252"><p>Smart quote: “</p>'.encode("windows-1252")
response = FakeResponse(html, "text/html")
assert "Smart quote: “" in fetch_page._decode_response_content(response)
def test_invalid_bytes_are_replaced_not_dropped() -> None:
response = FakeResponse(b"valid utf8 then invalid: \x8f")
decoded = fetch_page._decode_response_content(response)
assert "valid utf8" in decoded
assert "\ufffd" in decoded