Skip to content

~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.

15.4 KB
Raw Blame
"""Regression tests for the ``GenericDownloader`` ``.pdf`` fallback URL builder.

Background
----------
``GenericDownloader._download_pdf`` and ``download_with_result`` both
have a "fall back to a URL with ``.pdf`` appended" branch for sources
that serve the PDF under a slightly different path than the HTML
landing page (the canonical case is a publisher that exposes
``https://example.com/paper`` for the HTML and
``https://example.com/paper.pdf`` for the PDF).

The original implementation was::

    pdf_url = url.rstrip("/") + ".pdf"

That works for the common case (``https://example.com/paper`` becomes
``https://example.com/paper.pdf``) but breaks in three silent ways:

1. **Bare host** -- ``https://pmc.ncbi.nlm.nih.gov/`` becomes
   ``https://pmc.ncbi.nlm.nih.gov.pdf``. ``.pdf`` ends up appended to
   the hostname, the SSRF validator (``safe_requests.ssrf_validator``)
   rejects the unresolvable hostname with
   ``URL failed security validation (possible SSRF)``, and the failure
   is logged at ``ERROR`` even though the fallback was a normal part of
   the download path. This was the failure mode that surfaced in
   production Docker logs on 2026-09-21.
2. **Trailing slash on a path** -- ``https://example.com/paper/``
   happens to work because ``rstrip("/")`` deletes exactly the slash
   before appending. Fragile.
3. **Query string** -- ``https://example.com/paper?q=1`` becomes
   ``https://example.com/paper?q=1.pdf``. ``.pdf`` ends up after the
   query separator and the URL is malformed.

The fix routes the URL through ``urlunparse(parsed._replace(path=...))``
so the suffix lands in the path component and scheme / netloc / query /
fragment / userinfo are preserved verbatim. An empty path becomes
``/index.pdf`` -- a deterministic placeholder that still describes a
resource at the host root.

These tests pin every URL shape the production scheduler feeds into the
download service, plus the historical shape that the OLD string
concatenation would have corrupted silently.
"""

import inspect
from contextlib import contextmanager
from unittest.mock import patch

import pytest
import requests
from urllib3.util import parse_url

from local_deep_research.research_library.downloaders.generic import (
    GenericDownloader,
    _with_pdf_suffix,
)


# ---------------------------------------------------------------------------
# Pure unit tests on the URL builder
# ---------------------------------------------------------------------------


class TestWithPdfSuffix:
    """The helper is the single source of truth for how a URL becomes
    its ``.pdf`` suffix variant. Pin its behaviour against every URL
    shape the production download scheduler produces."""

    @pytest.mark.parametrize(
        "input_url,expected",
        [
            # The canonical case the original code happened to get right.
            (
                "https://example.com/paper",
                "https://example.com/paper.pdf",
            ),
            # URL already ends in .pdf -> no change needed, helper returns
            # None to signal "skip the fallback".
            ("https://example.com/paper.pdf", None),
            # The production bug: bare host with trailing slash. The old
            # implementation turned this into
            # ``https://pmc.ncbi.nlm.nih.gov.pdf`` -- a URL whose
            # ``.pdf`` was bolted onto the hostname. The helper now
            # synthesises an ``/index.pdf`` resource at the host root.
            (
                "https://pmc.ncbi.nlm.nih.gov/",
                "https://pmc.ncbi.nlm.nih.gov/index.pdf",
            ),
            # Bare host without trailing slash. ``urlparse`` normalises
            # the empty path the same way.
            (
                "https://example.com",
                "https://example.com/index.pdf",
            ),
            # Trailing slash on a real path. Used to work by accident
            # (``rstrip("/")`` + ``".pdf"``); the helper now does it
            # structurally.
            (
                "https://example.com/paper/",
                "https://example.com/paper.pdf",
            ),
            # Latent bug: a query string used to land before ``.pdf``
            # in the rebuilt URL. The helper keeps the query intact.
            (
                "https://example.com/paper?q=1",
                "https://example.com/paper.pdf?q=1",
            ),
            # Query string with multiple parameters and a fragment.
            (
                "https://example.com/paper?q=1&v=2#section",
                "https://example.com/paper.pdf?q=1&v=2#section",
            ),
            # userinfo is preserved.
            (
                "https://user:[email protected]/paper",
                "https://user:[email protected]/paper.pdf",
            ),
            # Path with trailing slash AND query string.
            (
                "https://example.com/paper/?lang=en",
                "https://example.com/paper.pdf?lang=en",
            ),
            # A path that already happens to end with a non-``.pdf``
            # extension. The helper appends ``.pdf`` after the existing
            # extension, which is the documented "best effort fallback"
            # semantic.
            (
                "https://example.com/paper.html",
                "https://example.com/paper.html.pdf",
            ),
        ],
    )
    def test_url_shapes(self, input_url, expected):
        assert _with_pdf_suffix(input_url) == expected

    @pytest.mark.parametrize(
        "input_url",
        [
            # Trailing slash on an already-.pdf path -- the OLD guard
            # only checked ``endswith(".pdf")`` on the unstripped path,
            # so the helper used to fall through and produce
            # ``paper.pdf.pdf``.
            "https://example.com/paper.pdf/",
            "https://example.com/paper.pdf//",
            # Uppercase / mixed-case ``.pdf`` extension. The OLD guard
            # was case-sensitive (``paper.PDF`` slipped through) and
            # produced ``paper.PDF.pdf``. Both shapes are recognised
            # as already-suffixed and short-circuit to ``None``.
            "https://example.com/paper.PDF",
            "https://example.com/paper.Pdf",
            "https://example.com/paper.pDf",
            # Uppercase + trailing slash.
            "https://example.com/paper.PDF/",
            "https://example.com/paper.Pdf/",
        ],
    )
    def test_already_suffixed_url_short_circuits(self, input_url):
        """A path that already ends in ``.pdf`` -- case-insensitive,
        with or without trailing slashes -- must NOT trigger a second
        ``.pdf`` fallback. Pinned against the
        ``paper.pdf.pdf`` / ``paper.PDF.pdf`` doubling the OLD guard
        produced."""
        assert _with_pdf_suffix(input_url) is None

    def test_bare_host_does_not_corrupt_hostname(self):
        """Production reproduction. The OLD implementation turned
        ``https://pmc.ncbi.nlm.nih.gov/`` into
        ``https://pmc.ncbi.nlm.nih.gov.pdf`` -- ``.pdf`` appended to the
        hostname. The host portion of the rebuilt URL must still be
        ``pmc.ncbi.nlm.nih.gov``.
        """
        from urllib.parse import urlparse

        rebuilt = _with_pdf_suffix("https://pmc.ncbi.nlm.nih.gov/")
        assert rebuilt is not None
        parsed = urlparse(rebuilt)
        assert parsed.hostname == "pmc.ncbi.nlm.nih.gov", (
            f"hostname must not absorb the .pdf suffix; got {parsed.hostname!r}"
        )
        assert parsed.path == "/index.pdf"
        assert ".pdf" not in parsed.hostname

    @pytest.mark.parametrize(
        "url,expected",
        [
            (
                "https://u%40ser:p%[email protected]:8443/paper;session=abc?x=1&x=2#part",
                "https://u%40ser:p%[email protected]:8443/paper.pdf;session=abc?x=1&x=2#part",
            ),
            (
                "https://[2606:4700:4700::1111]:8443/paper?q=1",
                "https://[2606:4700:4700::1111]:8443/paper.pdf?q=1",
            ),
            (
                "http://[fe80::1%25eth0]/paper",
                "http://[fe80::1%25eth0]/paper.pdf",
            ),
            (
                "https://bücher.example/paper",
                "https://bücher.example/paper.pdf",
            ),
            (
                "https://example.com/a%2Fb%3Fc%23d?token=a%2Fb%3Fc%23d",
                "https://example.com/a%2Fb%3Fc%23d.pdf?token=a%2Fb%3Fc%23d",
            ),
            (
                "https://example.com//other.example/paper///?next=https://other.example/",
                "https://example.com//other.example/paper.pdf?next=https://other.example/",
            ),
            (
                "https://example.com.////?q=1#/section/",
                "https://example.com./index.pdf?q=1#/section/",
            ),
        ],
    )
    def test_preserves_components_and_http_client_origin(self, url, expected):
        rebuilt = _with_pdf_suffix(url)
        assert rebuilt == expected
        assert _with_pdf_suffix(rebuilt) is None

        # Compare the destinations interpreted by the HTTP client, which uses
        # urllib3 rather than the helper's urllib.parse parser.
        original = parse_url(requests.Request("GET", url).prepare().url)
        fallback = parse_url(requests.Request("GET", rebuilt).prepare().url)
        assert (fallback.scheme, fallback.host, fallback.port) == (
            original.scheme,
            original.host,
            original.port,
        )

    @pytest.mark.parametrize(
        "url",
        [
            "https://[broken/paper",
            "https://[not-ip]/paper",
            "https://example.com\uff0fevil/paper",
        ],
    )
    def test_unparseable_url_returns_none(self, url):
        """Malformed authorities must fail softly without mocking urlparse."""
        assert _with_pdf_suffix(url) is None


# ---------------------------------------------------------------------------
# Integration with _download_pdf and download_with_result
# ---------------------------------------------------------------------------


class TestDownloaderUsesCorrectFallbackUrl:
    """The two callers of ``_with_pdf_suffix`` (in ``_download_pdf`` and
    ``download_with_result``) must pass the rebuilt URL to the parent
    class's ``_download_pdf`` -- and must NOT call it with the
    hostname-corrupted URL the OLD implementation produced."""

    @pytest.fixture
    def downloader(self):
        return GenericDownloader(timeout=30)

    @contextmanager
    def _spy_downloads(self, downloader):
        """Record every URL the parent ``_download_pdf`` is called with."""
        urls_called = []

        def fake_download(url, headers=None):
            urls_called.append(url)
            return  # every attempt "fails" so the fallback runs

        # Patch the inherited ``BaseDownloader._download_pdf`` so we can
        # observe the URL the helper built and the parent fetched.
        with patch.object(
            GenericDownloader.__bases__[0],
            "_download_pdf",
            side_effect=fake_download,
        ):
            yield urls_called

    def test_download_pdf_uses_index_pdf_for_bare_host(self, downloader):
        """Pin the production bug fix at the public API boundary: a
        bare-host URL must produce a ``/index.pdf`` request, not a
        hostname that ends in ``.pdf``."""
        with self._spy_downloads(downloader) as urls_called:
            downloader._download_pdf("https://pmc.ncbi.nlm.nih.gov/")

        assert urls_called == [
            "https://pmc.ncbi.nlm.nih.gov/",  # direct attempt
            "https://pmc.ncbi.nlm.nih.gov/index.pdf",  # rebuilt fallback
        ]

    def test_download_pdf_preserves_query_string(self, downloader):
        with self._spy_downloads(downloader) as urls_called:
            downloader._download_pdf("https://example.com/paper?id=42")

        # ``.pdf`` must land BEFORE the query, not after it.
        assert urls_called == [
            "https://example.com/paper?id=42",
            "https://example.com/paper.pdf?id=42",
        ]

    def test_download_pdf_does_not_double_extension(self, downloader):
        """A URL already ending in ``.pdf`` must NOT trigger a second
        fallback attempt (``paper.pdf.pdf``). Pinned against the
        existing happy-path contract."""
        with self._spy_downloads(downloader) as urls_called:
            downloader._download_pdf("https://example.com/paper.pdf")

        # The direct attempt fails (we stubbed every call to None), so
        # the fallback branch is reached, but ``_with_pdf_suffix``
        # returns ``None`` for an already-``.pdf`` URL -- so the helper
        # makes only ONE call.
        assert urls_called == ["https://example.com/paper.pdf"]
        assert not any(".pdf.pdf" in u for u in urls_called)

    def test_download_with_result_uses_index_pdf_for_bare_host(
        self, downloader
    ):
        """Same fix, but on the ``download_with_result`` code path.
        That branch has its own try/except block that used to build
        the URL with the same broken string concatenation."""

        # The diagnostic GET inside ``download_with_result`` also fires
        # when the fallback fails. A plain object works because the
        # route only reads ``status_code`` and ``headers`` and uses it
        # inside ``with ... as response:`` -- a regular class with
        # ``__enter__``/``__exit__`` is the most truthful stand-in.
        class _DiagnosticResp:
            def __init__(self):
                self.status_code = 200
                self.headers = {"content-type": "text/html"}

            def __enter__(self):
                return self

            def __exit__(self, exc_type, exc, tb):
                return False

        with self._spy_downloads(downloader) as urls_called:
            with patch.object(
                downloader.session, "get", return_value=_DiagnosticResp()
            ):
                result = downloader.download_with_result(
                    "https://pmc.ncbi.nlm.nih.gov/"
                )

        # The route must have tried the rebuilt URL, not a
        # hostname-with-``.pdf`` URL.
        assert urls_called == [
            "https://pmc.ncbi.nlm.nih.gov/",
            "https://pmc.ncbi.nlm.nih.gov/index.pdf",
        ]
        # And the result is the HTML-detected response, not a crash.
        assert result.is_success is False


# ---------------------------------------------------------------------------
# Dogfood: the helper should not be exported as part of the module API
# by accident -- it's an internal detail. Document that here so a
# future refactor that promotes it has to update the test.
# ---------------------------------------------------------------------------


def test_helper_is_module_level_and_private():
    """``_with_pdf_suffix`` is the single chokepoint for the URL
    transform. If a future refactor duplicates the logic in the two
    callers instead of routing through this helper, the regression
    coverage in ``TestDownloaderUsesCorrectFallbackUrl`` would still
    pass -- so pin the helper's existence here."""
    import local_deep_research.research_library.downloaders.generic as mod

    assert hasattr(mod, "_with_pdf_suffix"), (
        "_with_pdf_suffix must remain a module-level helper so the "
        "two callers (download_with_result and _download_pdf) cannot "
        "drift in their URL-rebuild semantics"
    )
    assert callable(mod._with_pdf_suffix)
    # And it's a pure function -- no reliance on session, rate_tracker,
    # or anything else that would force tests to set up a downloader.
    sig = inspect.signature(mod._with_pdf_suffix)
    assert list(sig.parameters) == ["url"]