~95% on SimpleQA (e.g. Qwen3.6-27B on a 3090). Supports all local and cloud LLMs (llama.cpp, Ollama, Google, ...). 10+ search engines - arXiv, PubMed, your private documents. Everything Local & Encrypted.
Python academiaanthropicarxivbravedeep-researchencryptionhome-automationhomeserverlocallocal-deep-researchlocal-llmmistralollamaopenaipubmedresearchresearch-toolretrieval-augmented-generationsearxngself-hosted
"""
Tests for HTML downloader.
"""
from unittest.mock import MagicMock, PropertyMock, patch
import pytest
from requests.exceptions import ChunkedEncodingError
from local_deep_research.research_library.downloaders.html import (
HTMLDownloader,
_fetch_failure_reason,
)
from local_deep_research.research_library.downloaders.base import ContentType
class TestHTMLDownloaderCanHandle:
"""Test URL handling capability."""
def test_can_handle_http(self):
"""Test HTTP URLs are handled."""
downloader = HTMLDownloader()
assert downloader.can_handle("http://example.com/article")
def test_can_handle_https(self):
"""Test HTTPS URLs are handled."""
downloader = HTMLDownloader()
assert downloader.can_handle("https://example.com/article")
def test_cannot_handle_ftp(self):
"""Test FTP URLs are not handled."""
downloader = HTMLDownloader()
assert not downloader.can_handle("ftp://example.com/file")
def test_cannot_handle_invalid(self):
"""Test invalid URLs are not handled."""
downloader = HTMLDownloader()
assert not downloader.can_handle("not-a-url")
class TestHTMLDownloaderExtraction:
"""Test content extraction from HTML."""
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_extract_title(self, mock_fetch, mock_html_response):
"""Test title extraction."""
mock_fetch.return_value = mock_html_response
downloader = HTMLDownloader()
result = downloader.download_text("https://example.com/article")
assert result is not None
assert "Test Article Title" in result
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_extract_content(self, mock_fetch, mock_html_response):
"""Test main content extraction."""
mock_fetch.return_value = mock_html_response
downloader = HTMLDownloader()
result = downloader.download_text("https://example.com/article")
assert result is not None
assert "first paragraph" in result
assert "second paragraph" in result
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_removes_navigation(self, mock_fetch, mock_html_response):
"""Test navigation elements are removed."""
mock_fetch.return_value = mock_html_response
downloader = HTMLDownloader()
result = downloader.download_text("https://example.com/article")
assert result is not None
assert "Navigation menu" not in result
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_removes_footer(self, mock_fetch, mock_html_response):
"""Test footer elements are removed."""
mock_fetch.return_value = mock_html_response
downloader = HTMLDownloader()
result = downloader.download_text("https://example.com/article")
assert result is not None
assert "Footer content" not in result
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_extract_description(self, mock_fetch, mock_html_response):
"""Test meta description extraction."""
mock_fetch.return_value = mock_html_response
downloader = HTMLDownloader()
result = downloader.download_text("https://example.com/article")
assert result is not None
assert "test article description" in result
class TestHTMLDownloaderDownload:
"""Test download functionality."""
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_download_returns_bytes(self, mock_fetch, mock_html_response):
"""Test download returns bytes."""
mock_fetch.return_value = mock_html_response
downloader = HTMLDownloader()
result = downloader.download(
"https://example.com/article", ContentType.TEXT
)
assert result is not None
assert isinstance(result, bytes)
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_download_pdf_not_supported(self, mock_fetch):
"""Test PDF download returns None."""
downloader = HTMLDownloader()
result = downloader.download(
"https://example.com/article", ContentType.PDF
)
assert result is None
mock_fetch.assert_not_called()
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_download_with_result_success(self, mock_fetch, mock_html_response):
"""Test download_with_result on success."""
mock_fetch.return_value = mock_html_response
downloader = HTMLDownloader()
result = downloader.download_with_result(
"https://example.com/article", ContentType.TEXT
)
assert result.is_success
assert result.content is not None
assert result.skip_reason is None
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_download_with_result_failure(self, mock_fetch):
"""Test download_with_result on failure."""
mock_fetch.return_value = None
downloader = HTMLDownloader()
result = downloader.download_with_result(
"https://example.com/article", ContentType.TEXT
)
assert not result.is_success
assert result.content is None
assert result.skip_reason is not None
class TestHTMLDownloaderFetchHTML:
"""Test HTML fetching."""
@patch("local_deep_research.security.SafeSession")
def test_fetch_html_success(
self, mock_session_class, mock_successful_response
):
"""Test successful HTML fetch."""
mock_session = MagicMock()
mock_session.headers = {}
mock_session.get.return_value = mock_successful_response
mock_session_class.return_value = mock_session
downloader = HTMLDownloader()
downloader.session = mock_session
result = downloader._fetch_html("https://example.com/article")
assert result is not None
mock_session.get.assert_called_once()
@patch("local_deep_research.security.SafeSession")
def test_fetch_html_404(self, mock_session_class):
"""Test 404 response."""
mock_session = MagicMock()
mock_session.headers = {}
mock_response = MagicMock()
mock_response.status_code = 404
mock_session.get.return_value = mock_response
mock_session_class.return_value = mock_session
downloader = HTMLDownloader()
downloader.session = mock_session
result = downloader._fetch_html("https://example.com/notfound")
assert result is None
@patch("local_deep_research.security.SafeSession")
def test_fetch_html_wrong_content_type(self, mock_session_class):
"""Test non-HTML content type."""
mock_session = MagicMock()
mock_session.headers = {}
mock_response = MagicMock()
mock_response.status_code = 200
mock_response.headers = {"content-type": "application/json"}
mock_session.get.return_value = mock_response
mock_session_class.return_value = mock_session
downloader = HTMLDownloader()
downloader.session = mock_session
result = downloader._fetch_html("https://api.example.com/data")
assert result is None
class TestHTMLDownloaderMetadata:
"""Test metadata extraction."""
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_get_metadata(self, mock_fetch, mock_html_response):
"""Test metadata extraction."""
mock_fetch.return_value = mock_html_response
downloader = HTMLDownloader()
metadata = downloader.get_metadata("https://example.com/article")
assert metadata.get("url") == "https://example.com/article"
assert metadata.get("title") == "Test Article Title"
assert (
metadata.get("description") == "This is a test article description."
)
assert metadata.get("author") == "Test Author"
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_get_metadata_fetch_failure(self, mock_fetch):
"""Test metadata extraction when fetch fails."""
mock_fetch.return_value = None
downloader = HTMLDownloader()
metadata = downloader.get_metadata("https://example.com/article")
assert metadata == {}
class TestHTMLDownloaderEdgeCases:
"""Test edge cases."""
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_empty_html(self, mock_fetch):
"""Test handling of empty HTML."""
mock_fetch.return_value = "<html><body></body></html>"
downloader = HTMLDownloader()
result = downloader.download_text("https://example.com/empty")
# Should return None for empty content
assert result is None
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_malformed_html(self, mock_fetch):
"""Test handling of malformed HTML."""
mock_fetch.return_value = "<html><body><p>Unclosed paragraph"
downloader = HTMLDownloader()
# Should not raise exception
result = downloader.download_text("https://example.com/malformed")
# BeautifulSoup handles malformed HTML gracefully
assert result is None or isinstance(result, str)
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_unicode_content(self, mock_fetch):
"""Test handling of Unicode content."""
mock_fetch.return_value = """
<html>
<head><title>Unicode Test</title></head>
<body>
<article>
<p>This has émojis 🎉 and spëcial çharacters.</p>
<p>Chinese: 中文内容</p>
<p>Japanese: 日本語のテキスト</p>
</article>
</body>
</html>
"""
downloader = HTMLDownloader()
result = downloader.download_text("https://example.com/unicode")
assert result is not None
assert "émojis" in result or "mojis" in result # Unicode handling
assert "中文" in result or "Chinese" in result
@patch(
"local_deep_research.research_library.downloaders.html.HTMLDownloader._fetch_html"
)
def test_very_long_content(self, mock_fetch):
"""Test handling of very long content."""
# Create HTML with many paragraphs
paragraphs = "\n".join(
f"<p>Paragraph {i} with some content that is long enough to matter.</p>"
for i in range(100)
)
mock_fetch.return_value = f"""
<html>
<head><title>Long Article</title></head>
<body><article>{paragraphs}</article></body>
</html>
"""
downloader = HTMLDownloader()
result = downloader.download_text("https://example.com/long")
assert result is not None
assert len(result) > 1000
def _make_text_pdf(num_pages: int) -> bytes:
"""Build a real PDF with one searchable marker per page.
Each page shows ``pagetext-NNN`` (zero-padded) using a Helvetica
Type1 font, so pypdf's text extraction recovers the markers without
any mocks. Kept local (not shared) so this file stays runnable on
its own.
"""
import io
from pypdf import PdfWriter
from pypdf.generic import (
DecodedStreamObject,
DictionaryObject,
NameObject,
)
writer = PdfWriter()
for index in range(num_pages):
writer.add_blank_page(612, 792)
page = writer.pages[index]
stream = DecodedStreamObject()
stream.set_data(
f"BT /F1 12 Tf 72 720 Td (pagetext-{index:03d}) Tj ET".encode(
"latin-1"
)
)
page[NameObject("/Contents")] = writer._add_object(stream)
font = DictionaryObject()
font[NameObject("/Type")] = NameObject("/Font")
font[NameObject("/Subtype")] = NameObject("/Type1")
font[NameObject("/BaseFont")] = NameObject("/Helvetica")
fonts = DictionaryObject()
fonts[NameObject("/F1")] = writer._add_object(font)
resources = DictionaryObject()
resources[NameObject("/Font")] = fonts
page[NameObject("/Resources")] = resources
buffer = io.BytesIO()
writer.write(buffer)
return buffer.getvalue()
class TestHTMLDownloaderPdfRecovery:
"""An HTML-classified URL serving application/pdf must yield text.
The URL classifier only sees the URL string, so publisher/DOI links
without a .pdf path land here serving PDFs. The bytes were already
downloaded -- discarding them with a warning loses the source.
"""
PDF_URL = "https://example.com/papers/cool-paper"
def _pdf_downloader(self, monkeypatch, response):
"""HTMLDownloader whose session serves one canned response."""
downloader = HTMLDownloader()
mock_session = MagicMock()
mock_session.headers = {}
mock_session.get.return_value = response
monkeypatch.setattr(downloader, "session", mock_session)
return downloader, mock_session
def _pdf_response(self, content_type="application/pdf"):
response = MagicMock()
response.status_code = 200
response.headers = {"content-type": content_type}
response.content = b"%PDF-1.4 fake pdf bytes"
response.text = "%PDF-1.4 fake pdf bytes"
response.url = self.PDF_URL
return response
def test_download_recovers_pdf_text_single_request(self, monkeypatch):
"""download() extracts PDF text with no second request."""
downloader, mock_session = self._pdf_downloader(
monkeypatch, self._pdf_response()
)
monkeypatch.setattr(
HTMLDownloader,
"extract_text_from_pdf",
staticmethod(lambda _pdf, *args, **kwargs: "Recovered PDF text"),
)
result = downloader.download(self.PDF_URL, ContentType.TEXT)
assert result is not None
assert b"Recovered PDF text" in result
assert self.PDF_URL.encode() in result
mock_session.get.assert_called_once()
def test_download_with_result_recovers_pdf_text(self, monkeypatch):
"""download_with_result() reports success for served PDFs."""
downloader, _ = self._pdf_downloader(monkeypatch, self._pdf_response())
monkeypatch.setattr(
HTMLDownloader,
"extract_text_from_pdf",
staticmethod(lambda _pdf, *args, **kwargs: "Recovered PDF text"),
)
result = downloader.download_with_result(self.PDF_URL, ContentType.TEXT)
assert result.is_success
assert result.content is not None
assert b"Recovered PDF text" in result.content
assert result.skip_reason is None
def test_pdf_detected_by_magic_bytes_not_just_header(self, monkeypatch):
"""Generic octet-stream with %PDF magic still recovers."""
downloader, _ = self._pdf_downloader(
monkeypatch, self._pdf_response("application/octet-stream")
)
monkeypatch.setattr(
HTMLDownloader,
"extract_text_from_pdf",
staticmethod(lambda _pdf, *args, **kwargs: "Recovered PDF text"),
)
result = downloader.download(self.PDF_URL, ContentType.TEXT)
assert result is not None
assert b"Recovered PDF text" in result
def test_pdf_without_extractable_text_still_fails(self, monkeypatch):
"""Scanned/empty PDFs fail cleanly (no crash, no HTML warning)."""
downloader, _ = self._pdf_downloader(monkeypatch, self._pdf_response())
monkeypatch.setattr(
HTMLDownloader,
"extract_text_from_pdf",
staticmethod(lambda _p, *args, **kwargs: None),
)
assert downloader.download(self.PDF_URL, ContentType.TEXT) is None
result = downloader.download_with_result(self.PDF_URL, ContentType.TEXT)
assert not result.is_success
assert result.content is None
assert "PDF" in (result.skip_reason or "")
def test_non_pdf_unexpected_type_still_fails(self, monkeypatch):
"""Genuinely unexpected types (JSON, images) keep old behavior."""
response = MagicMock()
response.status_code = 200
response.headers = {"content-type": "application/json"}
response.content = b'{"key": "value"}'
response.text = '{"key": "value"}'
response.url = "https://api.example.com/data"
downloader, _ = self._pdf_downloader(monkeypatch, response)
assert (
downloader.download(
"https://api.example.com/data", ContentType.TEXT
)
is None
)
def test_stash_never_leaks_across_urls(self, monkeypatch):
"""Stashed PDF bytes are only consumed by their own request URL."""
downloader, mock_session = self._pdf_downloader(
monkeypatch, self._pdf_response()
)
# Prime the stash with a PDF fetch, then ask for another URL that
# 404s: the other URL must not receive the first URL's PDF text.
assert downloader._fetch_html(self.PDF_URL) is None
not_found = MagicMock()
not_found.status_code = 404
mock_session.get.return_value = not_found
assert (
downloader.download("https://example.com/missing", ContentType.TEXT)
is None
)
def test_oversize_pdf_skips_extraction_with_reason(self, monkeypatch):
"""PDFs over the recovery byte cap skip parsing, with one GET."""
downloader, mock_session = self._pdf_downloader(
monkeypatch, self._pdf_response()
)
oversize = b"%PDF-1.4 " + b"x" * (
HTMLDownloader.MAX_RECOVERED_PDF_BYTES + 1
)
mock_session.get.return_value.content = oversize
called = []
monkeypatch.setattr(
HTMLDownloader,
"extract_text_from_pdf",
staticmethod(
lambda _pdf, *args, **kwargs: (
called.append(1) or "SHOULD NOT PARSE"
)
),
)
assert downloader.download(self.PDF_URL, ContentType.TEXT) is None
assert called == []
result = downloader.download_with_result(self.PDF_URL, ContentType.TEXT)
assert not result.is_success
assert result.content is None
assert "exceeds" in (result.skip_reason or "").lower()
# Each download() path performs exactly one GET (no second request).
assert mock_session.get.call_count == 2
def test_recovery_extraction_is_page_bounded(self, monkeypatch):
"""Recovery passes a page budget to the PDF extractor."""
downloader, _ = self._pdf_downloader(monkeypatch, self._pdf_response())
seen = {}
def _fake_extract(pdf_bytes, max_pages=None, **kwargs):
seen["max_pages"] = max_pages
return "bounded text"
monkeypatch.setattr(
HTMLDownloader,
"extract_text_from_pdf",
staticmethod(_fake_extract),
)
result = downloader.download(self.PDF_URL, ContentType.TEXT)
assert result is not None
assert b"bounded text" in result
assert seen["max_pages"] == HTMLDownloader.MAX_RECOVERED_PDF_PAGES
def test_recovery_byte_cap_boundary(self, monkeypatch):
"""The byte cap is ``>``, not ``>=``: == parses, +1 skips."""
for size, should_parse in (
(HTMLDownloader.MAX_RECOVERED_PDF_BYTES, True),
(HTMLDownloader.MAX_RECOVERED_PDF_BYTES + 1, False),
):
downloader, mock_session = self._pdf_downloader(
monkeypatch, self._pdf_response()
)
payload = b"%PDF" + b"x" * (size - len(b"%PDF"))
assert len(payload) == size
mock_session.get.return_value.content = payload
called = []
monkeypatch.setattr(
HTMLDownloader,
"extract_text_from_pdf",
staticmethod(
lambda _pdf, *args, **kwargs: (
called.append(1) or "boundary text"
)
),
)
result = downloader.download(self.PDF_URL, ContentType.TEXT)
if should_parse:
assert result is not None, size
assert b"boundary text" in result, size
assert called == [1], size
with_result = downloader.download_with_result(
self.PDF_URL, ContentType.TEXT
)
assert with_result.is_success, size
assert with_result.content is not None, size
assert b"boundary text" in with_result.content, size
assert mock_session.get.call_count == 2
else:
assert result is None, size
assert called == [], size
mock_session.get.assert_called_once()
def test_recovery_extracts_sixty_page_pdf_fully(self, monkeypatch):
"""End-to-end: a 60-page PDF served at an HTML URL is fully read.
The recovery page budget aliases the central
``MAX_PDF_EXTRACTION_PAGES`` ceiling (500), so ordinary
multi-dozen-page documents are no longer cut at 50. Uses the
real extractor (no mocks).
"""
pdf_bytes = _make_text_pdf(60)
assert len(pdf_bytes) <= HTMLDownloader.MAX_RECOVERED_PDF_BYTES
response = self._pdf_response()
response.content = pdf_bytes
downloader, mock_session = self._pdf_downloader(monkeypatch, response)
result = downloader.download(self.PDF_URL, ContentType.TEXT)
assert result is not None
text = result.decode("utf-8")
assert "pagetext-000" in text
assert "pagetext-049" in text
assert "pagetext-050" in text
assert "pagetext-059" in text
mock_session.get.assert_called_once()
def test_recovery_truncation_enforced_at_configured_cap(self, monkeypatch):
"""A small configured cap still truncates (wiring check)."""
monkeypatch.setattr(HTMLDownloader, "MAX_RECOVERED_PDF_PAGES", 7)
pdf_bytes = _make_text_pdf(10)
response = self._pdf_response()
response.content = pdf_bytes
downloader, _ = self._pdf_downloader(monkeypatch, response)
result = downloader.download(self.PDF_URL, ContentType.TEXT)
assert result is not None
text = result.decode("utf-8")
assert "pagetext-006" in text
assert "pagetext-007" not in text
def test_page_budget_aliases_central_ceiling(self):
"""The recovery budget cannot silently diverge again."""
from local_deep_research.utilities.pdf_extraction_limits import (
MAX_PDF_EXTRACTION_PAGES,
)
assert (
HTMLDownloader.MAX_RECOVERED_PDF_PAGES == MAX_PDF_EXTRACTION_PAGES
)
class TestHTMLDownloaderTextRecovery:
"""Plain-text / feed responses at HTML URLs are usable as-is.
Same mismatch class as served PDFs (classifier sees only the URL
string), but with no extraction step: text/* and feed XML are already
source text.
"""
TEXT_URL = "https://example.com/notes/paper-notes"
def _text_downloader(self, monkeypatch, content_type, body):
downloader = HTMLDownloader()
mock_session = MagicMock()
mock_session.headers = {}
response = MagicMock()
response.status_code = 200
response.headers = {"content-type": content_type}
response.content = body.encode("utf-8")
response.text = body
response.url = self.TEXT_URL
mock_session.get.return_value = response
monkeypatch.setattr(downloader, "session", mock_session)
return downloader, mock_session
def test_plain_text_recovers(self, monkeypatch):
downloader, mock_session = self._text_downloader(
monkeypatch, "text/plain; charset=utf-8", "Hello plain world"
)
result = downloader.download(self.TEXT_URL, ContentType.TEXT)
assert result is not None
assert b"Hello plain world" in result
assert self.TEXT_URL.encode() in result
mock_session.get.assert_called_once()
def test_markdown_and_csv_recover(self, monkeypatch):
for content_type, body in [
("text/markdown", "# Title\n\nSome *markdown* prose."),
("text/csv", "a,b,c\n1,2,3"),
]:
downloader, _ = self._text_downloader(
monkeypatch, content_type, body
)
result = downloader.download_with_result(
self.TEXT_URL, ContentType.TEXT
)
assert result.is_success, content_type
assert body.encode() in result.content, content_type
def test_rss_atom_and_xml_recover(self, monkeypatch):
feed = (
'<?xml version="1.0"?>'
"<rss><channel><title>Feed</title>"
"<item><title>Story</title></item>"
"</channel></rss>"
)
for content_type in [
"application/rss+xml",
"application/atom+xml",
"application/xml",
# Headers routinely carry parameters; the media type must match
# with "; charset=..." suffixed (bare forms covered above).
"application/rss+xml; charset=utf-8",
"application/atom+xml; charset=utf-8",
"application/xml; charset=utf-8",
"application/tei+xml; charset=utf-8",
"application/mathml+xml; charset=utf-8",
"Application/RSS+XML; Charset=UTF-8",
]:
downloader, _ = self._text_downloader(
monkeypatch, content_type, feed
)
result = downloader.download(self.TEXT_URL, ContentType.TEXT)
assert result is not None, content_type
assert b"Story" in result, content_type
def test_svg_markup_is_not_recovered(self, monkeypatch):
"""image/svg+xml matches +xml but is markup, not prose."""
for content_type in [
"image/svg+xml",
"image/svg+xml; charset=utf-8",
"application/svg+xml",
"text/svg+xml",
"text/svg+xml; charset=utf-8",
"text/svg",
]:
downloader, _ = self._text_downloader(
monkeypatch,
content_type,
'<svg xmlns="http://www.w3.org/2000/svg"></svg>',
)
assert (
downloader.download(self.TEXT_URL, ContentType.TEXT) is None
), content_type
def test_json_still_fails_loudly(self, monkeypatch):
"""application/json is deliberately excluded (API dumps)."""
for content_type in [
"application/json",
"application/json; charset=utf-8",
]:
downloader, _ = self._text_downloader(
monkeypatch, content_type, '{"key": "value"}'
)
result = downloader.download_with_result(
self.TEXT_URL, ContentType.TEXT
)
assert not result.is_success, content_type
assert result.content is None, content_type
class TestHTMLDownloaderBodyReadFailure:
"""A non-HTML 200 whose body aborts mid-read is a transport failure.
``BaseDownloader._is_pdf_content`` swallows every exception raised
while reading ``response.content`` and answers "not a PDF", so a body
that aborts mid-read used to fall through to the ordinary
unsupported-content return: the host never answered, yet nothing
recorded a failure. The arXiv caller then classified the attempt as
NOT_FETCHED (a refundable fetch) instead of FETCH_FAILED and kept
sending requests to a failing host.
"""
URL = "https://example.com/papers/cool-paper"
def _downloader(self, monkeypatch, response):
"""HTMLDownloader with a stubbed session and an observable tracker."""
downloader = HTMLDownloader()
mock_session = MagicMock()
mock_session.headers = {}
mock_session.get.return_value = response
monkeypatch.setattr(downloader, "session", mock_session)
tracker = MagicMock()
monkeypatch.setattr(downloader, "rate_tracker", tracker)
return downloader, mock_session, tracker
def _aborting_response(self, content_type="application/octet-stream"):
"""A 200 whose ``.content`` raises, as a dropped connection does."""
response = MagicMock()
response.status_code = 200
response.headers = {"content-type": content_type}
response.url = self.URL
type(response).content = PropertyMock(
side_effect=ChunkedEncodingError("connection broken")
)
return response
def test_body_read_failure_reports_transport_failure(self, monkeypatch):
"""One callback, one failed outcome, closed response, no payload."""
response = self._aborting_response()
downloader, mock_session, tracker = self._downloader(
monkeypatch, response
)
on_transport_failure = MagicMock()
html, final_url = downloader._fetch_html_with_final_url(
self.URL, on_transport_failure=on_transport_failure
)
assert (html, final_url) == (None, None)
# The body was never read in full, so the host never answered.
on_transport_failure.assert_called_once_with()
assert tracker.record_outcome.call_count == 1
assert tracker.record_outcome.call_args.kwargs["success"] is False
# stream=True leaves the connection open unless the response closes.
response.close.assert_called_once()
# A failed fetch leaves nothing a later call could mistake for
# recovered content.
assert downloader._consume_recovery(self.URL) == (None, None, None)
mock_session.get.assert_called_once()
def test_body_read_failure_yields_no_content(self, monkeypatch):
"""download()/download_with_result() report failure, not content."""
response = self._aborting_response()
downloader, _, _ = self._downloader(monkeypatch, response)
assert downloader.download(self.URL, ContentType.TEXT) is None
result = downloader.download_with_result(self.URL, ContentType.TEXT)
assert not result.is_success
assert result.content is None
def test_body_read_failure_on_text_type_also_fails(self, monkeypatch):
"""A text/plain answer that aborts mid-read is not recovered."""
response = self._aborting_response(content_type="text/plain")
downloader, _, _ = self._downloader(monkeypatch, response)
assert downloader.download(self.URL, ContentType.TEXT) is None
def test_successful_recovery_leaves_callback_untouched(self, monkeypatch):
"""A fully read non-HTML 200 is an answer, not a failure."""
cases = [
("application/pdf", b"%PDF-1.4 real enough", "PDF"),
("application/rss+xml; charset=utf-8", b"a", None),
]
for content_type, body, _label in cases:
response = MagicMock()
response.status_code = 200
response.headers = {"content-type": content_type}
response.content = body
response.text = "rss body"
response.url = self.URL
downloader, _, tracker = self._downloader(monkeypatch, response)
on_transport_failure = MagicMock()
downloader._fetch_html_with_final_url(
self.URL, on_transport_failure=on_transport_failure
)
on_transport_failure.assert_not_called()
response.close.assert_called_once()
assert tracker.record_outcome.call_count == 0, content_type
def test_pdf_body_read_failure_records_no_outcome_twice(self, monkeypatch):
"""A %PDF header with a failing read is still one failure."""
response = self._aborting_response(content_type="application/pdf")
downloader, _, tracker = self._downloader(monkeypatch, response)
on_transport_failure = MagicMock()
downloader._fetch_html_with_final_url(
self.URL, on_transport_failure=on_transport_failure
)
on_transport_failure.assert_called_once_with()
assert tracker.record_outcome.call_count == 1
assert downloader._consume_recovery(self.URL) == (None, None, None)
class TestHTMLFetchRetry:
"""Transient failures (429/503, timeouts) are retried, rest fail fast."""
URL = "https://example.com/unstable-page"
def _downloader(self, monkeypatch):
downloader = HTMLDownloader()
mock_session = MagicMock()
mock_session.headers = {}
monkeypatch.setattr(downloader, "session", mock_session)
tracker = MagicMock()
monkeypatch.setattr(downloader, "rate_tracker", tracker)
return downloader, mock_session, tracker
def _status_response(self, status):
response = MagicMock()
response.status_code = status
response.headers = {}
response.url = self.URL
return response
def _html_response(self):
response = self._status_response(200)
response.headers = {"content-type": "text/html; charset=utf-8"}
response.text = "<html><body>recovered</body></html>"
return response
def test_429_then_success(self, monkeypatch):
"""A 429 is retried; the eventual 200 succeeds."""
downloader, mock_session, tracker = self._downloader(monkeypatch)
mock_session.get.side_effect = [
self._status_response(429),
self._html_response(),
]
on_transport_failure = MagicMock()
html, final_url = downloader._fetch_html_with_final_url(
self.URL, on_transport_failure=on_transport_failure
)
assert html == "<html><body>recovered</body></html>"
assert final_url == self.URL
assert mock_session.get.call_count == 2
on_transport_failure.assert_not_called()
assert tracker.record_outcome.call_count == 2
failed, succeeded = tracker.record_outcome.call_args_list
assert failed.kwargs["success"] is False
assert failed.kwargs["error_type"] == "HTTP_429"
assert failed.kwargs["retry_count"] == 1
assert succeeded.kwargs["success"] is True
assert succeeded.kwargs["retry_count"] == 2
def test_503_exhausted_gives_up_once(self, monkeypatch):
"""Three 503s: three GETs, one callback, terminal reason logged."""
downloader, mock_session, tracker = self._downloader(monkeypatch)
mock_session.get.side_effect = [self._status_response(503)] * 3
on_transport_failure = MagicMock()
with patch(
"local_deep_research.research_library.downloaders.html.logger"
) as mock_logger:
html, final_url = downloader._fetch_html_with_final_url(
self.URL, on_transport_failure=on_transport_failure
)
assert (html, final_url) == (None, None)
assert mock_session.get.call_count == 3
on_transport_failure.assert_called_once_with()
assert tracker.record_outcome.call_count == 3
failed = [
c
for c in mock_logger.warning.call_args_list
if "fetch_failed" in c.args[0]
]
assert len(failed) == 1
assert failed[0].kwargs["status"] == 503
assert failed[0].kwargs["reason"] == "rate_limited"
assert failed[0].kwargs["attempts"] == 3
def test_timeout_then_success(self, monkeypatch):
"""Timeouts are retried like 429/503."""
from requests.exceptions import Timeout
downloader, mock_session, tracker = self._downloader(monkeypatch)
mock_session.get.side_effect = [
Timeout("timed out"),
Timeout("timed out"),
self._html_response(),
]
html, _ = downloader._fetch_html_with_final_url(self.URL)
assert html == "<html><body>recovered</body></html>"
assert mock_session.get.call_count == 3
assert tracker.record_outcome.call_count == 3
def test_403_fails_fast_with_reason(self, monkeypatch):
"""A 403 is not retryable; the reason names the bucket."""
downloader, mock_session, tracker = self._downloader(monkeypatch)
mock_session.get.return_value = self._status_response(403)
on_transport_failure = MagicMock()
with patch(
"local_deep_research.research_library.downloaders.html.logger"
) as mock_logger:
html, final_url = downloader._fetch_html_with_final_url(
self.URL, on_transport_failure=on_transport_failure
)
assert (html, final_url) == (None, None)
mock_session.get.assert_called_once()
on_transport_failure.assert_called_once_with()
failed = [
c
for c in mock_logger.warning.call_args_list
if "fetch_failed" in c.args[0]
]
assert len(failed) == 1
assert failed[0].kwargs["reason"] == "forbidden"
def test_404_fails_fast_without_callback(self, monkeypatch):
"""Absent documents are answers, not transport failures."""
downloader, mock_session, _ = self._downloader(monkeypatch)
mock_session.get.return_value = self._status_response(404)
on_transport_failure = MagicMock()
with patch(
"local_deep_research.research_library.downloaders.html.logger"
) as mock_logger:
html, final_url = downloader._fetch_html_with_final_url(
self.URL, on_transport_failure=on_transport_failure
)
assert (html, final_url) == (None, None)
mock_session.get.assert_called_once()
on_transport_failure.assert_not_called()
failed = [
c
for c in mock_logger.warning.call_args_list
if "fetch_failed" in c.args[0]
]
assert len(failed) == 1
assert failed[0].kwargs["reason"] == "absent"
def test_max_attempts_one_disables_retry(self, monkeypatch):
"""max_attempts=1 preserves the old single-shot behavior."""
downloader, mock_session, _ = self._downloader(monkeypatch)
mock_session.get.side_effect = [
self._status_response(429),
self._html_response(),
]
html, final_url = downloader._fetch_html_with_final_url(
self.URL, max_attempts=1
)
assert (html, final_url) == (None, None)
mock_session.get.assert_called_once()
def test_retried_response_connection_is_closed(self, monkeypatch):
"""A retried 429 response is closed before the next attempt.
The old single-shot path also closed its (single) response in a
``finally``, so asserting the close alone cannot tell retry from
no-retry. Pin the ordering: the first response must be closed
before the second GET is issued.
"""
downloader, mock_session, _ = self._downloader(monkeypatch)
first = self._status_response(429)
second = self._html_response()
order: list[str] = []
def _record_close(*args, **kwargs):
order.append("close")
first.close.side_effect = _record_close
def _ordered_get(*args, **kwargs):
order.append("get")
if len([c for c in order if c == "get"]) == 1:
return first
return second
mock_session.get.side_effect = _ordered_get
downloader._fetch_html_with_final_url(self.URL)
first.close.assert_called_once()
assert mock_session.get.call_count == 2
# Close must sit between the two GETs: the old single-shot
# ``finally`` only closes after the fetch returns, i.e. after the
# last GET (["get", "get", "close"]).
assert order == ["get", "close", "get"]
def test_connection_error_then_success(self, monkeypatch):
"""ConnectionErrors are retried like timeouts."""
from requests.exceptions import ConnectionError as RequestsConnError
downloader, mock_session, tracker = self._downloader(monkeypatch)
mock_session.get.side_effect = [
RequestsConnError("connection reset"),
self._html_response(),
]
on_transport_failure = MagicMock()
html, final_url = downloader._fetch_html_with_final_url(
self.URL, on_transport_failure=on_transport_failure
)
assert html == "<html><body>recovered</body></html>"
assert final_url == self.URL
assert mock_session.get.call_count == 2
on_transport_failure.assert_not_called()
assert tracker.record_outcome.call_count == 2
failed, succeeded = tracker.record_outcome.call_args_list
assert failed.kwargs["success"] is False
assert failed.kwargs["error_type"] == "ConnectionError"
assert failed.kwargs["retry_count"] == 1
assert succeeded.kwargs["success"] is True
assert succeeded.kwargs["retry_count"] == 2
def test_non_retryable_exception_fails_fast_once(self, monkeypatch):
"""A non-retryable GET error: 1 GET, 1 outcome, 1 callback."""
downloader, mock_session, tracker = self._downloader(monkeypatch)
# SSRF rejections surface as ValueError from the session; any
# non-timeout/connection exception must take the same path.
mock_session.get.side_effect = ValueError("SSRF blocked")
on_transport_failure = MagicMock()
html, final_url = downloader._fetch_html_with_final_url(
self.URL, on_transport_failure=on_transport_failure
)
assert (html, final_url) == (None, None)
mock_session.get.assert_called_once()
on_transport_failure.assert_called_once_with()
assert tracker.record_outcome.call_count == 1
outcome = tracker.record_outcome.call_args
assert outcome.kwargs["success"] is False
assert outcome.kwargs["retry_count"] == 1
assert outcome.kwargs["error_type"] == "ValueError"
@pytest.mark.parametrize(
("status", "expected"),
[
(404, "absent"),
(410, "absent"),
(429, "rate_limited"),
(503, "rate_limited"),
(401, "forbidden"),
(403, "forbidden"),
(400, "bad_request"),
(500, "server_error"),
(502, "server_error"),
(599, "server_error"),
(418, "client_error"),
(422, "client_error"),
(499, "client_error"),
(302, "unexpected_status"),
(199, "unexpected_status"),
(600, "unexpected_status"),
],
)
def test_fetch_failure_reason_token_table(status, expected):
"""Every status bucket maps to its caller-safe log token."""
assert _fetch_failure_reason(status) == expected