Files
MoneyPrinterTurbo/test/services/test_http_byte_range_grammar.py
2026-10-14 11:43:53 +08:00

33 lines
1.5 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from unittest.mock import patch
import pytest
from fastapi.testclient import TestClient
from app import asgi
from app.config import config
from app.controllers.v1 import video as controller
from app.models.exception import HttpException
@pytest.mark.parametrize('header', ['Bytes=1-3', 'BYTES=-3'])
def test_stream_accepts_case_insensitive_byte_unit(tmp_path, header):
(tmp_path / 'clip.mp4').write_bytes(b'0123456789')
with patch.dict(config.app, {'api_key': ''}), patch.object(controller.utils, 'task_dir', return_value=str(tmp_path)):
response = TestClient(asgi.get_application()).get('/api/v1/stream/clip.mp4', headers={'Range': header})
assert response.status_code == 206
assert response.content == (b'123' if header == 'Bytes=1-3' else b'789')
@pytest.mark.parametrize('header', ['bytes=+1-3', 'bytes=1_0-', 'bytes=1-+3', 'bytes=-+3', 'bytes= 1-3'])
def test_stream_rejects_non_http_numeric_syntax(tmp_path, header):
(tmp_path / 'clip.mp4').write_bytes(b'01234567890123456789')
with patch.dict(config.app, {'api_key': ''}), patch.object(controller.utils, 'task_dir', return_value=str(tmp_path)):
response = TestClient(asgi.get_application()).get('/api/v1/stream/clip.mp4', headers={'Range': header})
assert response.status_code == 416
def test_byte_positions_require_ascii_digits():
with pytest.raises(HttpException) as raised:
controller._parse_byte_range('bytes=١-٣', 10, 'fixture')
assert raised.value.status_code == 416