GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — ZeroConfigDLNA/test_dlna.py at main · richstokes/ZeroConfigDLNA

Expand file tree

/

test_dlna.py

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
243 lines (198 loc) · 9.13 KB
/

test_dlna.py

Copy path

File metadata and controls

243 lines (198 loc) · 9.13 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""Regression tests for DLNA media HTTP responses."""
import io
import os
import tempfile
import unittest
from unittest.mock import Mock
from dlna import DLNAHandler
from helpers import send_device_description
class MediaResponseTest(unittest.TestCase):
"""Verify standard HTTP behavior while retaining Xbox compatibility."""
def setUp(self):
"""Create a small media fixture."""
with tempfile.NamedTemporaryFile(delete=False) as temp_file:
temp_file.write(b"0123456789")
self.temp_path = temp_file.name
def tearDown(self):
"""Remove the media fixture."""
os.unlink(self.temp_path)
def make_handler(self, headers=None):
"""Build a handler without opening a network socket."""
handler = DLNAHandler.__new__(DLNAHandler)
handler.headers = headers or {}
handler.verbose = False
handler.wfile = io.BytesIO()
handler.response_status = None
handler.response_headers = {}
handler.send_response = lambda status: setattr(
handler, "response_status", status
)
handler.send_header = handler.response_headers.__setitem__
handler.end_headers = Mock()
return handler
def serve(self, headers=None, head_only=False):
"""Serve the fixture with the supplied request headers."""
handler = self.make_handler(headers)
handler.handle_range_request(
self.temp_path,
10,
"audio/x-flac",
handler.headers.get("Range"),
head_only,
)
return handler
def test_full_get_without_range_returns_200(self):
"""A normal full GET uses 200 and omits Content-Range."""
handler = self.serve({"User-Agent": "Lavf/ffprobe"})
self.assertEqual(handler.response_status, 200)
self.assertEqual(handler.response_headers["Content-Length"], "10")
self.assertNotIn("Content-Range", handler.response_headers)
self.assertEqual(handler.wfile.getvalue(), b"0123456789")
def test_head_without_range_returns_200_without_body(self):
"""A normal HEAD uses 200 and never writes the media body."""
handler = self.serve(head_only=True)
self.assertEqual(handler.response_status, 200)
self.assertNotIn("Content-Range", handler.response_headers)
self.assertEqual(handler.wfile.getvalue(), b"")
def test_range_get_returns_206_and_requested_bytes(self):
"""A valid byte range uses 206 and writes only that range."""
handler = self.serve({"Range": "bytes=3-6"})
self.assertEqual(handler.response_status, 206)
self.assertEqual(handler.response_headers["Content-Range"], "bytes 3-6/10")
self.assertEqual(handler.response_headers["Content-Length"], "4")
self.assertEqual(handler.wfile.getvalue(), b"3456")
def test_xbox_without_range_retains_206_compatibility(self):
"""An Xbox still gets the legacy full-file 206 response."""
handler = self.serve({"User-Agent": "Xbox/10.0 UPnP/1.0"})
self.assertEqual(handler.response_status, 206)
self.assertEqual(handler.response_headers["Content-Range"], "bytes 0-9/10")
self.assertEqual(handler.wfile.getvalue(), b"0123456789")
def test_subtitle_url_is_added_to_media_response(self):
"""A sidecar URL is exposed through the Samsung response header."""
handler = self.make_handler()
subtitle_url = "http://127.0.0.1:8000/subtitles/Movie.srt"
handler.handle_range_request(
self.temp_path,
10,
"video/x-matroska",
None,
True,
subtitle_url,
)
self.assertEqual(handler.response_headers["CaptionInfo.sec"], subtitle_url)
class SubtitleSupportTest(unittest.TestCase):
"""Verify SRT sidecar discovery, metadata, and delivery."""
def setUp(self):
"""Create a video and language-tagged subtitle fixture."""
self.temp_directory = tempfile.TemporaryDirectory()
self.video_path = os.path.join(self.temp_directory.name, "Movie.mkv")
self.language_subtitle_path = os.path.join(
self.temp_directory.name, "Movie.en.srt"
)
with open(self.video_path, "wb") as video_file:
video_file.write(b"video")
with open(self.language_subtitle_path, "wb") as subtitle_file:
subtitle_file.write(b"language subtitle")
self.handler = DLNAHandler.__new__(DLNAHandler)
self.handler.fast = True
self.handler.verbose = False
self.handler.server_instance = Mock(
media_directory=self.temp_directory.name,
server_ip="127.0.0.1",
port=8000,
)
def tearDown(self):
"""Remove the subtitle fixtures."""
self.temp_directory.cleanup()
def test_exact_sidecar_is_preferred_over_language_variant(self):
"""Movie.srt wins over language-tagged sidecars when both exist."""
self.assertEqual(
self.handler._find_sidecar_subtitle(self.video_path),
self.language_subtitle_path,
)
exact_subtitle_path = os.path.join(self.temp_directory.name, "Movie.srt")
with open(exact_subtitle_path, "wb") as subtitle_file:
subtitle_file.write(b"exact subtitle")
self.assertEqual(
self.handler._find_sidecar_subtitle(self.video_path), exact_subtitle_path
)
def test_didl_advertises_common_subtitle_extensions(self):
"""DIDL includes generic, Samsung, and Panasonic-style hints."""
file_info = {
"id": "2",
"name": "Movie.mkv",
"path": "Movie.mkv",
"full_path": self.video_path,
"mime_type": "video/x-matroska",
"size": os.path.getsize(self.video_path),
}
didl = self.handler._create_media_item_didl(file_info, "1")
subtitle_url = "http://127.0.0.1:8000/subtitles/Movie.en.srt"
self.assertIn('pv:subtitleFileType="SRT"', didl)
self.assertIn(f'pv:subtitleFileUri="{subtitle_url}"', didl)
self.assertIn(
f'<res protocolInfo="http-get:*:text/srt:*">{subtitle_url}</res>', didl
)
self.assertIn(
f'<sec:CaptionInfoEx sec:type="srt">{subtitle_url}</sec:CaptionInfoEx>',
didl,
)
def test_subtitle_response_is_utf8_text(self):
"""The dedicated endpoint returns a normal full subtitle response."""
self.handler.wfile = io.BytesIO()
self.handler.response_status = None
self.handler.response_headers = {}
self.handler.send_response = lambda status: setattr(
self.handler, "response_status", status
)
self.handler.send_header = self.handler.response_headers.__setitem__
self.handler.end_headers = Mock()
self.handler.serve_subtitle_file("Movie.en.srt")
self.assertEqual(self.handler.response_status, 200)
self.assertEqual(
self.handler.response_headers["Content-Type"], "text/srt; charset=utf-8"
)
self.assertEqual(self.handler.wfile.getvalue(), b"language subtitle")
class ContentLengthTest(unittest.TestCase):
"""Content-Length must count encoded bytes, not characters."""
def make_handler(self, media_directory="", path="/browse"):
"""Build a handler that records headers without a network socket."""
handler = DLNAHandler.__new__(DLNAHandler)
handler.path = path
handler.verbose = False
handler.server_name = "Médiatár"
handler.server_instance = Mock(
media_directory=media_directory,
server_ip="127.0.0.1",
port=8200,
device_uuid="test-uuid",
)
handler.wfile = io.BytesIO()
handler.response_headers = {}
handler.send_response = Mock()
handler.send_header = handler.response_headers.__setitem__
handler.end_headers = Mock()
return handler
def assert_length_matches_body(self, handler):
"""The declared length equals the number of bytes written."""
body = handler.wfile.getvalue()
self.assertEqual(handler.response_headers["Content-Length"], str(len(body)))
def test_device_description_with_non_ascii_server_name(self):
"""A non-ASCII server name doesn't truncate the device XML."""
handler = self.make_handler()
send_device_description(handler)
self.assertIn("Médiatár".encode("utf-8"), handler.wfile.getvalue())
self.assert_length_matches_body(handler)
def test_web_ui_with_non_ascii_names(self):
"""Accented names and the folder icon don't truncate the page."""
with tempfile.TemporaryDirectory() as media_directory:
os.mkdir(os.path.join(media_directory, "Filmek"))
media_path = os.path.join(media_directory, "árvíztűrő.mp4")
with open(media_path, "wb") as media_file:
media_file.write(b"0")
handler = self.make_handler(media_directory)
handler.send_browse_response()
self.assertIn("árvíztűrő".encode("utf-8"), handler.wfile.getvalue())
self.assert_length_matches_body(handler)
if __name__ == "__main__":
unittest.main()
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.