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.

3.2 KB
Raw Blame
"""``_is_api_request`` path-shape boundaries: the ``api`` segment is
slash-bounded.

Ported from ``TestIsApiPath`` in ``tests/web/auth/test_decorators.py`` on
main, which tested ``web/auth/decorators.py::_is_api_path`` — deleted by the
FastAPI migration. The successor is
``web/fastapi_app.py::_is_api_request``, which keeps the identical path rule
(``"/api/" in path or path.endswith("/api")``) and adds Accept-header
negotiation on top of it.

Why this file rather than more cases in
``tests/web/test_exception_handler_contract.py::TestIsApiRequest``: that
class pins the positive shapes (``/api/v1/foo``, ``/settings/api/bar``,
``/settings/api``) and the Accept-header branches, but nothing on the branch
pins the *negative* half — that a path merely CONTAINING the letters "api"
is not an API path. This is the assertion that goes red if the rule is ever
loosened to ``"api" in path``, which would flip ``/openapi.json`` and
``/apidocs`` onto the JSON branch: those two are FastAPI's own built-in
docs endpoints, so a browser hitting them while signed out would receive a
JSON 401 instead of the login redirect.

Requests are built as raw ASGI scopes with NO Accept header, so only the
path rule is under test — ``httpx``/``TestClient`` would send ``Accept:
*/*``, which is not the browser shape and not the JSON shape either.
"""

import pytest
from starlette.requests import Request as StarletteRequest

from local_deep_research.web.fastapi_app import _is_api_request


def _request(path: str) -> StarletteRequest:
    return StarletteRequest(
        {
            "type": "http",
            "method": "GET",
            "path": path,
            "raw_path": path.encode(),
            "query_string": b"",
            "headers": [],
            "server": ("testserver", 80),
            "scheme": "http",
        }
    )


@pytest.mark.parametrize(
    "path",
    [
        "/api/v1/foo",
        # Nested API blueprints: the regression that motivated matching
        # "/api/" anywhere in the path rather than only as a prefix.
        "/news/api/categories",
        "/library/api/documents",
        "/settings/api/foo",
    ],
)
def test_api_segment_anywhere_in_the_path_is_an_api_path(path):
    assert _is_api_request(_request(path)) is True


@pytest.mark.parametrize(
    "path",
    [
        # Paths ending in "/api" with no further segments are JSON
        # endpoints in their own right.
        "/settings/api",
        "/history/api",
        "/foo/api",
        "/api",
    ],
)
def test_path_ending_in_slash_api_is_an_api_path(path):
    assert _is_api_request(_request(path)) is True


@pytest.mark.parametrize(
    "path",
    ["/news/", "/dashboard", "/news/subscriptions"],
)
def test_page_paths_are_not_api_paths(path):
    assert _is_api_request(_request(path)) is False


@pytest.mark.parametrize("path", ["/apidocs", "/openapi.json", "/notapi/x"])
def test_partial_api_word_does_not_match(path):
    """The ``api`` segment must be slash-bounded.

    ``/openapi.json`` and ``/apidocs`` are FastAPI's own docs endpoints and
    are reached by browsers; classifying them as API paths would replace
    their 401 login redirect with a raw JSON body.
    """
    assert _is_api_request(_request(path)) is False