Files

154 lines
5.7 KiB
Python

import tempfile
from typing import cast
import pytest
from a2wsgi import WSGIMiddleware
from fastapi.applications import FastAPI
from fastapi.testclient import TestClient
from galaxy.util.bunch import Bunch
from galaxy.web.framework.base import (
Request,
Response,
send_file,
)
from galaxy.webapps.base.webapp import GalaxyWebTransaction
CONTENT = "content"
def setup_fastAPI(fh, nginx_x_accel_redirect_base=None, apache_xsendfile=None, set_content_length=True):
def wsgi_application(env, start_response):
trans = Bunch(
response=Response(),
request=Request(env),
app=Bunch(
config=Bunch(nginx_x_accel_redirect_base=nginx_x_accel_redirect_base, apache_xsendfile=apache_xsendfile)
),
)
if set_content_length:
trans.response.headers["content-length"] = len(CONTENT)
trans.response.set_content_type("application/octet-stream")
return send_file(start_response, cast(GalaxyWebTransaction, trans), fh)
app = FastAPI()
# https://github.com/abersheeran/a2wsgi/issues/44
app.mount("/test/send_file", WSGIMiddleware(wsgi_application)) # type: ignore[arg-type]
return app
@pytest.fixture
def test_file_handle():
with tempfile.NamedTemporaryFile() as fh:
fh.write(b"content")
fh.flush()
fh.seek(0)
yield fh
def test_sendfile_nginx(test_file_handle):
app = setup_fastAPI(test_file_handle, nginx_x_accel_redirect_base="/base")
client = TestClient(app)
response = client.get("/test/send_file")
assert response.status_code == 200
assert response.headers["x-accel-redirect"] == f"/base{test_file_handle.name}"
assert "content-length" not in response.headers
assert not response.content
def test_sendfile_apache(test_file_handle):
app = setup_fastAPI(test_file_handle, apache_xsendfile="/base")
client = TestClient(app)
response = client.get("/test/send_file")
assert response.status_code == 200
assert response.headers["X-Sendfile"] == test_file_handle.name
assert "content-length" not in response.headers
assert not response.content
def test_sendfile_in_process(test_file_handle):
app = setup_fastAPI(test_file_handle)
client = TestClient(app)
response = client.get("/test/send_file")
assert response.status_code == 200
assert "X-Sendfile" not in response.headers
assert "x-accel-redirect" not in response.headers
assert response.headers["content-length"] == str(len(CONTENT))
assert response.content.decode() == "content"
def test_sendfile_range(test_file_handle):
app = setup_fastAPI(test_file_handle)
client = TestClient(app)
response = client.get("/test/send_file", headers={"Range": "bytes=0-3"})
assert response.status_code == 206
assert response.headers["content-range"] == f"bytes 0-3/{len(CONTENT)}"
assert response.headers["content-length"] == "4"
assert response.content.decode() == "cont"
def test_sendfile_range_open_ended(test_file_handle):
app = setup_fastAPI(test_file_handle)
client = TestClient(app)
response = client.get("/test/send_file", headers={"Range": "bytes=2-"})
assert response.status_code == 206
assert response.headers["content-range"] == f"bytes 2-{len(CONTENT) - 1}/{len(CONTENT)}"
assert response.headers["content-length"] == str(len(CONTENT) - 2)
assert response.content.decode() == "ntent"
def test_sendfile_range_without_content_length(test_file_handle):
# Files served out of a dataset's extra_files_path (e.g. JBrowse track data) don't set
# a content-length header; a Range request on a non-nginx deployment must still succeed.
app = setup_fastAPI(test_file_handle, set_content_length=False)
client = TestClient(app)
response = client.get("/test/send_file", headers={"Range": "bytes=0-3"})
assert response.status_code == 206
assert response.headers["content-range"] == f"bytes 0-3/{len(CONTENT)}"
assert response.content.decode() == "cont"
def test_sendfile_range_suffix(test_file_handle):
app = setup_fastAPI(test_file_handle)
client = TestClient(app)
response = client.get("/test/send_file", headers={"Range": "bytes=-3"})
assert response.status_code == 206
assert response.headers["content-range"] == f"bytes 4-6/{len(CONTENT)}"
assert response.headers["content-length"] == "3"
assert response.content.decode() == "ent"
def test_sendfile_range_suffix_longer_than_file(test_file_handle):
app = setup_fastAPI(test_file_handle)
client = TestClient(app)
response = client.get("/test/send_file", headers={"Range": "bytes=-99"})
assert response.status_code == 206
assert response.headers["content-range"] == f"bytes 0-6/{len(CONTENT)}"
assert response.content.decode() == CONTENT
def test_sendfile_range_unsatisfiable(test_file_handle):
app = setup_fastAPI(test_file_handle)
client = TestClient(app)
response = client.get("/test/send_file", headers={"Range": "bytes=7-"})
assert response.status_code == 416
assert response.headers["content-range"] == f"bytes */{len(CONTENT)}"
assert not response.content
def test_sendfile_multiple_ranges_serves_whole_file(test_file_handle):
app = setup_fastAPI(test_file_handle)
client = TestClient(app)
response = client.get("/test/send_file", headers={"Range": "bytes=0-1,3-4"})
assert response.status_code == 200
assert "content-range" not in response.headers
assert response.content.decode() == CONTENT
def test_sendfile_malformed_range(test_file_handle):
app = setup_fastAPI(test_file_handle)
client = TestClient(app)
response = client.get("/test/send_file", headers={"Range": "bytes=-1-1"})
assert response.status_code == 416
assert response.headers["content-range"] == f"bytes */{len(CONTENT)}"