This repository was archived by the owner on May 2, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathtest_networking.py
More file actions
56 lines (33 loc) · 1.62 KB
/
test_networking.py
File metadata and controls
56 lines (33 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from unittest.mock import patch
import httpx
from viu_media.core.utils.networking import get_remote_filename, random_user_agent
def _make_response(url: str, headers: dict[str, str] | None = None) -> httpx.Response:
request = httpx.Request("GET", url)
return httpx.Response(200, headers=headers, request=request)
def test_random_user_agent_uses_selected_chrome_version():
with patch("viu_media.core.utils.networking.random.choice", return_value="97.0.4692.20"):
user_agent = random_user_agent()
assert "Chrome/97.0.4692.20" in user_agent
assert user_agent.startswith("Mozilla/5.0")
def test_get_remote_filename_prefers_filename_star_header():
response = _make_response(
"https://example.com/download",
{"Content-Disposition": "attachment; filename*=UTF-8''my%20anime%20file.mkv"},
)
filename = get_remote_filename(response)
assert filename == "my anime file.mkv"
def test_get_remote_filename_uses_regular_filename_header():
response = _make_response(
"https://example.com/download",
{"Content-Disposition": 'attachment; filename="episode-01.mp4"'},
)
filename = get_remote_filename(response)
assert filename == "episode-01.mp4"
def test_get_remote_filename_falls_back_to_url_path():
response = _make_response("https://example.com/files/ep%2001.mp4?token=abc")
filename = get_remote_filename(response)
assert filename == "ep 01.mp4"
def test_get_remote_filename_returns_none_when_no_candidate_found():
response = _make_response("https://example.com/")
filename = get_remote_filename(response)
assert filename is None