-
Notifications
You must be signed in to change notification settings - Fork 313
/
Copy pathtest_url.py
393 lines (332 loc) · 12.1 KB
/
test_url.py
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import gzip
import http.server
import threading
import unittest
import wfdb.io._url
class TestNetFiles(unittest.TestCase):
"""
Test accessing remote files.
"""
def test_requests(self):
"""
Test reading a remote file using various APIs.
This tests that we can create a file object using
wfdb.io._url.openurl(), and tests that the object implements
the standard Python API functions for a file of the
appropriate type.
Parameters
----------
N/A
Returns
-------
N/A
"""
text_data = """
BERNARDO: Who's there?
FRANCISCO: Nay, answer me: stand, and unfold yourself.
BERNARDO: Long live the king!
FRANCISCO: Bernardo?
BERNARDO: He.
FRANCISCO: You come most carefully upon your hour.
BERNARDO: 'Tis now struck twelve; get thee to bed, Francisco.
"""
binary_data = text_data.encode()
file_content = {"/foo.txt": binary_data}
# Test all possible combinations of:
# - whether or not the server supports compression
# - whether or not the server supports random access
# - chosen buffering policy
for allow_gzip in (False, True):
for allow_range in (False, True):
with DummyHTTPServer(
file_content=file_content,
allow_gzip=allow_gzip,
allow_range=allow_range,
) as server:
url = server.url("/foo.txt")
for buffering in (-2, -1, 0, 20):
self._test_text(url, text_data, buffering)
self._test_binary(url, binary_data, buffering)
def _test_text(self, url, content, buffering):
"""
Test reading a URL using text-mode file APIs.
Parameters
----------
url : str
URL of the remote resource.
content : str
Expected content of the resource.
buffering : int
Buffering policy for openurl().
Returns
-------
N/A
"""
# read(-1), readable(), seekable()
with wfdb.io._url.openurl(url, "r", buffering=buffering) as tf:
self.assertTrue(tf.readable())
self.assertTrue(tf.seekable())
self.assertEqual(tf.read(), content)
self.assertEqual(tf.read(), "")
# read(10)
with wfdb.io._url.openurl(url, "r", buffering=buffering) as tf:
result = ""
while True:
chunk = tf.read(10)
result += chunk
if len(chunk) < 10:
break
self.assertEqual(result, content)
# readline(), seek(), tell()
with wfdb.io._url.openurl(url, "r", buffering=buffering) as tf:
result = ""
while True:
rpos = tf.tell()
tf.seek(0)
tf.seek(rpos)
chunk = tf.readline()
result += chunk
if len(chunk) == 0:
break
self.assertEqual(result, content)
def _test_binary(self, url, content, buffering):
"""
Test reading a URL using binary-mode file APIs.
Parameters
----------
url : str
URL of the remote resource.
content : bytes
Expected content of the resource.
buffering : int
Buffering policy for openurl().
Returns
-------
N/A
"""
# read(-1), readable(), seekable()
with wfdb.io._url.openurl(url, "rb", buffering=buffering) as bf:
self.assertTrue(bf.readable())
self.assertTrue(bf.seekable())
self.assertEqual(bf.read(), content)
self.assertEqual(bf.read(), b"")
self.assertEqual(bf.tell(), len(content))
# read(10)
with wfdb.io._url.openurl(url, "rb", buffering=buffering) as bf:
result = b""
while True:
chunk = bf.read(10)
result += chunk
if len(chunk) < 10:
break
self.assertEqual(result, content)
self.assertEqual(bf.tell(), len(content))
# readline()
with wfdb.io._url.openurl(url, "rb", buffering=buffering) as bf:
result = b""
while True:
chunk = bf.readline()
result += chunk
if len(chunk) == 0:
break
self.assertEqual(result, content)
self.assertEqual(bf.tell(), len(content))
# read1(10), seek(), tell()
with wfdb.io._url.openurl(url, "rb", buffering=buffering) as bf:
bf.seek(0, 2)
self.assertEqual(bf.tell(), len(content))
bf.seek(0)
result = b""
while True:
rpos = bf.tell()
bf.seek(0)
bf.seek(rpos)
chunk = bf.read1(10)
result += chunk
if len(chunk) == 0:
break
self.assertEqual(result, content)
self.assertEqual(bf.tell(), len(content))
# readinto(bytearray(10))
with wfdb.io._url.openurl(url, "rb", buffering=buffering) as bf:
result = b""
chunk = bytearray(10)
while True:
count = bf.readinto(chunk)
result += chunk[:count]
if count < 10:
break
self.assertEqual(result, content)
self.assertEqual(bf.tell(), len(content))
# readinto1(bytearray(10))
with wfdb.io._url.openurl(url, "rb", buffering=buffering) as bf:
result = b""
chunk = bytearray(10)
while True:
count = bf.readinto1(chunk)
result += chunk[:count]
if count == 0:
break
self.assertEqual(result, content)
self.assertEqual(bf.tell(), len(content))
class TestRemoteFLACFiles(unittest.TestCase):
"""
Test reading FLAC files over HTTP.
"""
def test_whole_file(self):
"""
Test reading a complete FLAC file using local and HTTP APIs.
This tests that we can read the file 'sample-data/flacformats.d2'
(a 24-bit FLAC stream) using the soundfile library, first by
reading the file from the local filesystem, and then using
wfdb.io._url.openurl() to access it through a simulated web server.
This is meant to verify that the soundfile library works using only
the standard Python file object API (as implemented by
wfdb.io._url.NetFile), and doesn't require the input file to be an
actual io.FileIO object.
Parameters
----------
N/A
Returns
-------
N/A
"""
import soundfile
import numpy as np
data_file_path = "sample-data/flacformats.d2"
expected_format = "FLAC"
expected_subtype = "PCM_24"
# Read the file using standard file I/O
sf1 = soundfile.SoundFile(data_file_path)
self.assertEqual(sf1.format, expected_format)
self.assertEqual(sf1.subtype, expected_subtype)
data1 = sf1.read()
# Read the file using HTTP
with open(data_file_path, "rb") as f:
file_content = {"/foo.dat": f.read()}
with DummyHTTPServer(file_content) as server:
url = server.url("/foo.dat")
file2 = wfdb.io._url.openurl(url, "rb")
sf2 = soundfile.SoundFile(file2)
self.assertEqual(sf2.format, expected_format)
self.assertEqual(sf2.subtype, expected_subtype)
data2 = sf2.read()
# Check that results are equal
np.testing.assert_array_equal(data1, data2)
class DummyHTTPServer(http.server.HTTPServer):
"""
HTTPServer used to simulate a web server for testing.
The server may be used as a context manager (using "with"); during
execution of the "with" block, a background thread runs that
listens for and handles client requests.
Attributes
----------
file_content : dict
Dictionary containing the content of each file on the server.
The keys are absolute paths (such as "/foo.txt"); the values
are the corresponding content (bytes).
allow_gzip : bool, optional
True if the server should return compressed responses (using
"Content-Encoding: gzip") when the client requests them (using
"Accept-Encoding: gzip").
allow_range : bool, optional
True if the server should return partial responses (using 206
Partial Content and "Content-Range") when the client requests
them (using "Range").
server_address : tuple (str, int), optional
A tuple specifying the address and port number where the
server should listen for connections. If the port is 0, an
arbitrary unused port is selected. The default address is
"127.0.0.1" and the default port is 0.
"""
def __init__(
self,
file_content,
allow_gzip=True,
allow_range=True,
server_address=("127.0.0.1", 0),
):
super().__init__(server_address, DummyHTTPRequestHandler)
self.file_content = file_content
self.allow_gzip = allow_gzip
self.allow_range = allow_range
def url(self, path="/"):
"""
Generate a URL that points to a file on this server.
Parameters
----------
path : str, optional
Path of the file on the server.
Returns
-------
url : str
Absolute URL for the specified file.
"""
return "http://127.0.0.1:%d/%s" % (
self.server_address[1],
path.lstrip("/"),
)
def __enter__(self):
super().__enter__()
self.thread = threading.Thread(target=self.serve_forever)
self.thread.start()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.shutdown()
self.thread.join()
self.thread = None
return super().__exit__(exc_type, exc_val, exc_tb)
class DummyHTTPRequestHandler(http.server.BaseHTTPRequestHandler):
"""
HTTPRequestHandler used to simulate a web server for testing.
"""
def do_HEAD(self):
self.send_head()
def do_GET(self):
body = self.send_head()
self.wfile.write(body)
def log_message(self, message, *args):
pass
def send_head(self):
content = self.server.file_content.get(self.path)
if content is None:
self.send_error(404)
return b""
headers = {"Content-Type": "text/plain"}
status = 200
if self.server.allow_gzip:
headers["Vary"] = "Accept-Encoding"
if "gzip" in self.headers.get("Accept-Encoding", ""):
content = gzip.compress(content)
headers["Content-Encoding"] = "gzip"
if self.server.allow_range:
headers["Accept-Ranges"] = "bytes"
req_range = self.headers.get("Range", "")
if req_range.startswith("bytes="):
start, end = req_range.split("=")[1].split("-")
start = int(start)
if end == "":
end = len(content)
else:
end = min(len(content), int(end) + 1)
if start < end:
status = 206
resp_range = "bytes %d-%d/%d" % (
start,
end - 1,
len(content),
)
content = content[start:end]
else:
status = 416
resp_range = "bytes */%d" % len(content)
content = b""
headers["Content-Range"] = resp_range
headers["Content-Length"] = len(content)
self.send_response(status)
for h, v in sorted(headers.items()):
self.send_header(h, v)
self.end_headers()
return content
if __name__ == "__main__":
unittest.main()