Files
galaxy/test/unit/webapps/test_range_header.py
mvdbeek ec57de42b6 Keep only range and download tests that fail without the fixes
Removed test cases that already passed on release_26.1:
- byte ranges the old parser resolved correctly (bytes=0-499, bytes=500-,
  bytes=999-999) and the WSGI end-past-EOF clamp, which webob already did;
- 416 cases where release_26.1 already answered 416;
- the `directory` download case, which was already zipped;
- the plain-text element in the collection download test.

Every remaining new test fails on release_26.1. The RangeNotSatisfiable
docstring now also covers malformed byte ranges.
2026-10-19 08:49:21 +02:00

75 lines
2.2 KiB
Python

import tempfile
import pytest
from fastapi import HTTPException
from fastapi.applications import FastAPI
from fastapi.testclient import TestClient
from galaxy.webapps.base.api import (
_get_range_header,
GalaxyFileResponse,
)
@pytest.mark.parametrize(
"range_header, expected",
[
("bytes=-500", (500, 999)),
("bytes=-1", (999, 999)),
("bytes=-5000", (0, 999)),
("bytes=0-5000", (0, 999)),
("Bytes=0-1", (0, 1)),
("bytes = 0 - 1", (0, 1)),
],
)
def test_get_range_header(range_header, expected):
assert _get_range_header(range_header, 1000) == expected
@pytest.mark.parametrize("range_header", ["", "bytes=0-1,3-4", "items=0-1"])
def test_get_range_header_ignored(range_header):
assert _get_range_header(range_header, 1000) is None
@pytest.mark.parametrize("range_header", ["bytes=-0", "bytes=-"])
def test_get_range_header_unsatisfiable(range_header):
with pytest.raises(HTTPException) as exc_info:
_get_range_header(range_header, 1000)
assert exc_info.value.status_code == 416
assert exc_info.value.headers == {"content-range": "bytes */1000"}
@pytest.fixture
def file_client():
with tempfile.NamedTemporaryFile() as fh:
fh.write(b"content")
fh.flush()
app = FastAPI()
@app.get("/file")
def serve_file():
return GalaxyFileResponse(fh.name)
yield TestClient(app)
def test_file_response_suffix_range(file_client):
response = file_client.get("/file", headers={"Range": "bytes=-3"})
assert response.status_code == 206
assert response.headers["content-range"] == "bytes 4-6/7"
assert response.headers["content-length"] == "3"
assert response.content == b"ent"
def test_file_response_multiple_ranges_serves_whole_file(file_client):
response = file_client.get("/file", headers={"Range": "bytes=0-1,3-4"})
assert response.status_code == 200
assert "content-range" not in response.headers
assert response.content == b"content"
def test_file_response_unsatisfiable_range(file_client):
response = file_client.get("/file", headers={"Range": "bytes=7-"})
assert response.status_code == 416
assert response.headers["content-range"] == "bytes */7"