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.

4.9 KB
Raw Blame
"""Partial in-memory downloads preserve HTTP range and response contracts."""

import pytest
import sys
from starlette.requests import Request

from local_deep_research.web.utils.range_response import (
    in_memory_range_response,
)

CONTENT = b"0123456789"


def _response(headers=(), *, method="GET", content=CONTENT):
    request = Request(
        {
            "type": "http",
            "method": method,
            "headers": [
                (key.lower().encode("ascii"), value.encode("ascii"))
                for key, value in headers
            ],
        }
    )
    return in_memory_range_response(
        content,
        request=request,
        media_type="application/pdf",
        headers={"Content-Disposition": 'inline; filename="report.pdf"'},
    )


@pytest.mark.parametrize(
    ("value", "body", "content_range"),
    [
        ("bytes=0-3", b"0123", "bytes 0-3/10"),
        ("bytes=2-5", b"2345", "bytes 2-5/10"),
        ("bytes=9-9", b"9", "bytes 9-9/10"),
        ("bytes=8-", b"89", "bytes 8-9/10"),
        ("bytes=0-", CONTENT, "bytes 0-9/10"),
        ("bytes=-3", b"789", "bytes 7-9/10"),
        ("bytes=-10", CONTENT, "bytes 0-9/10"),
        ("bytes=-99", CONTENT, "bytes 0-9/10"),
        ("bytes=8-99", b"89", "bytes 8-9/10"),
        ("bytes=00-03", b"0123", "bytes 0-3/10"),
        ("BYTES=0-3", b"0123", "bytes 0-3/10"),
        ("bytes= 0-3 ", b"0123", "bytes 0-3/10"),
        ("bytes=-" + "9" * 100, CONTENT, "bytes 0-9/10"),
        ("bytes=8-" + "9" * 100, b"89", "bytes 8-9/10"),
    ],
)
def test_single_range(value, body, content_range):
    response = _response([("Range", value)])

    assert response.status_code == 206
    assert response.body == body
    assert response.headers["content-range"] == content_range
    assert response.headers["content-length"] == str(len(body))
    assert response.headers["accept-ranges"] == "bytes"
    assert response.headers["content-type"] == "application/pdf"
    assert (
        response.headers["content-disposition"]
        == 'inline; filename="report.pdf"'
    )


@pytest.mark.parametrize(
    "value", ["bytes=10-", "bytes=99-100", "bytes=" + "9" * 100 + "-"]
)
def test_unsatisfiable_range(value):
    response = _response([("Range", value)])

    assert response.status_code == 416
    assert response.body == b""
    assert response.headers["content-range"] == "bytes */10"
    assert response.headers["content-length"] == "0"
    assert response.headers["accept-ranges"] == "bytes"


@pytest.mark.parametrize(
    "headers",
    [
        [],
        [("Range", "")],
        [("Range", "items=0-3")],
        [("Range", "bytes=wrong")],
        [("Range", "bytes=-")],
        [("Range", "bytes=0--1")],
        [("Range", "bytes=1_0-99")],
        [("Range", "bytes=0-3;extra")],
        [("Range", "bytes=0-3\n")],
        [("Range", "bytes=0-3\x00")],
        [("Range", "bytes=5-2")],
        [("Range", "bytes=-0")],
        [("Range", "bytes=-000")],
        [("Range", "bytes=+1-3")],
        [("Range", "bytes=1.0-3")],
        [("Range", "bytes=0-1,4-5")],
        [("Range", "bytes=0-3,2-5")],
        [("Range", "bytes=0-3,")],
        [("Range", "bytes=0-3"), ("Range", "bytes=5-6")],
        [("Range", "bytes=0-3"), ("If-Range", '"older-document"')],
        [("Range", "bytes=0-3"), ("If-Range", "Wed, 21 Oct 2015 07:28:00 GMT")],
        [("Range", "bytes=0-3"), ("If-Range", "")],
        pytest.param(
            [("Range", "bytes=" + "9" * 5000 + "-")],
            id="oversized-integer",
        ),
    ],
)
def test_ignored_ranges_return_the_complete_response(headers):
    response = _response(headers)

    assert response.status_code == 200
    assert response.body == CONTENT
    assert response.headers["content-length"] == "10"
    assert response.headers["accept-ranges"] == "bytes"
    assert "content-range" not in response.headers


@pytest.mark.parametrize("method", ["HEAD", "POST", "OPTIONS"])
def test_range_only_applies_to_get(method):
    response = _response([("Range", "bytes=0-3")], method=method)

    assert response.status_code == 200
    assert response.headers["content-length"] == "10"
    assert "content-range" not in response.headers


def test_empty_representation_has_no_satisfiable_range():
    response = _response([("Range", "bytes=0-")], content=b"")

    assert response.status_code == 416
    assert response.body == b""
    assert response.headers["content-range"] == "bytes */0"
    assert response.headers["content-length"] == "0"


def test_configured_integer_limit_keeps_the_complete_response():
    original_limit = sys.get_int_max_str_digits()
    try:
        sys.set_int_max_str_digits(640)
        response = _response([("Range", "bytes=" + "9" * 700 + "-")])
    finally:
        sys.set_int_max_str_digits(original_limit)

    assert response.status_code == 200
    assert response.body == CONTENT
    assert response.headers["content-length"] == "10"
    assert "content-range" not in response.headers