server: add support for HEAD route on audio mp3

This commit is contained in:
2023-11-10 10:11:02 +01:00
committed by Mathieu Virbel
parent 898d1ffcab
commit 14946921f3
3 changed files with 33 additions and 1 deletions

View File

@@ -1,7 +1,7 @@
import os import os
from typing import BinaryIO from typing import BinaryIO
from fastapi import HTTPException, Request, status from fastapi import HTTPException, Request, Response, status
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
@@ -57,6 +57,9 @@ def range_requests_response(
), ),
} }
if request.method == "HEAD":
return Response(headers=headers)
if content_disposition: if content_disposition:
headers["Content-Disposition"] = content_disposition headers["Content-Disposition"] = content_disposition

View File

@@ -210,6 +210,7 @@ async def transcript_delete(
@router.get("/transcripts/{transcript_id}/audio/mp3") @router.get("/transcripts/{transcript_id}/audio/mp3")
@router.head("/transcripts/{transcript_id}/audio/mp3")
async def transcript_get_audio_mp3( async def transcript_get_audio_mp3(
request: Request, request: Request,
transcript_id: str, transcript_id: str,

View File

@@ -46,6 +46,34 @@ async def test_transcript_audio_download(fake_transcript, url_suffix, content_ty
assert response.status_code == 200 assert response.status_code == 200
assert response.headers["content-type"] == content_type assert response.headers["content-type"] == content_type
# test get 404
ac = AsyncClient(app=app, base_url="http://test/v1")
response = await ac.get(f"/transcripts/{fake_transcript.id}XXX/audio{url_suffix}")
assert response.status_code == 404
@pytest.mark.asyncio
@pytest.mark.parametrize(
"url_suffix,content_type",
[
["/mp3", "audio/mpeg"],
],
)
async def test_transcript_audio_download_head(
fake_transcript, url_suffix, content_type
):
from reflector.app import app
ac = AsyncClient(app=app, base_url="http://test/v1")
response = await ac.head(f"/transcripts/{fake_transcript.id}/audio{url_suffix}")
assert response.status_code == 200
assert response.headers["content-type"] == content_type
# test head 404
ac = AsyncClient(app=app, base_url="http://test/v1")
response = await ac.head(f"/transcripts/{fake_transcript.id}XXX/audio{url_suffix}")
assert response.status_code == 404
@pytest.mark.asyncio @pytest.mark.asyncio
@pytest.mark.parametrize( @pytest.mark.parametrize(