forked from danvk/RangeHTTPServer
/
server_test.py
138 lines (109 loc) · 3.71 KB
/
server_test.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
# Import RangeHTTPServer from this project, not the global install.
import sys
sys.path = ['.'] + sys.path
from RangeHTTPServer import RangeRequestHandler
from nose.tools import *
try:
# Python 3
from http.server import HTTPServer
except ImportError:
# Python2
from BaseHTTPServer import HTTPServer
import requests
import threading
import time
httpd = None
server_thread = None
def setup():
def start_server():
global httpd
RangeRequestHandler.protocol_version = 'HTTP/1.0'
# TODO(danvk): pick a random, available port
httpd = HTTPServer(('', 8712), RangeRequestHandler)
httpd.serve_forever()
global server_thread
server_thread = threading.Thread(target=start_server)
server_thread.start()
time.sleep(1.0)
def teardown():
httpd.shutdown()
server_thread.join()
def headers_of_note(response):
'''Returns a dict of just the interesting headers for RangeHTTPServer.'''
return {k: response.headers.get(k) for k in [
'Content-Type',
'Accept-Ranges',
'Content-Range',
'Content-Length']}
def test_simple_request():
r = requests.get('http://localhost:8712/tests/data.txt')
eq_(200, r.status_code)
eq_('0123456789abcdef\n', r.text)
eq_('text/plain', r.headers['content-type'])
def test_range_request():
r = requests.get('http://localhost:8712/tests/data.txt',
headers={'Range': 'bytes=0-9'})
eq_(206, r.status_code)
eq_('0123456789', r.text)
eq_({
'Content-Type': 'text/plain',
'Accept-Ranges': 'bytes',
'Content-Range': 'bytes 0-9/17',
'Content-Length': '10'
}, headers_of_note(r))
def test_open_range_request():
r = requests.get('http://localhost:8712/tests/data.txt',
headers={'Range': 'bytes=10-'})
eq_(206, r.status_code)
eq_('abcdef\n', r.text)
eq_({
'Content-Type': 'text/plain',
'Accept-Ranges': 'bytes',
'Content-Range': 'bytes 10-16/17',
'Content-Length': '7'
}, headers_of_note(r))
def test_mid_file_range_request():
r = requests.get('http://localhost:8712/tests/data.txt',
headers={'Range': 'bytes=6-10'})
eq_(206, r.status_code)
eq_('6789a', r.text)
eq_({
'Content-Type': 'text/plain',
'Accept-Ranges': 'bytes',
'Content-Range': 'bytes 6-10/17',
'Content-Length': '5'
}, headers_of_note(r))
def test_404():
r = requests.get('http://localhost:8712/tests/nonexistent.txt',
headers={'Range': 'bytes=6-10'})
eq_(404, r.status_code)
def test_bad_range():
r = requests.get('http://localhost:8712/tests/data.txt',
headers={'Range': 'bytes=abc'})
eq_(400, r.status_code)
def test_range_past_eof():
r = requests.get('http://localhost:8712/tests/data.txt',
headers={'Range': 'bytes=10-100'})
eq_(206, r.status_code)
eq_('abcdef\n', r.text)
eq_({
'Content-Type': 'text/plain',
'Accept-Ranges': 'bytes',
'Content-Range': 'bytes 10-16/17',
'Content-Length': '7'
}, headers_of_note(r))
def test_range_at_eof():
r = requests.get('http://localhost:8712/tests/data.txt',
headers={'Range': 'bytes=16-'})
eq_(206, r.status_code)
eq_('\n', r.text)
eq_({
'Content-Type': 'text/plain',
'Accept-Ranges': 'bytes',
'Content-Range': 'bytes 16-16/17',
'Content-Length': '1'
}, headers_of_note(r))
def test_range_starting_past_eof():
r = requests.get('http://localhost:8712/tests/data.txt',
headers={'Range': 'bytes=17-'})
eq_(416, r.status_code) # "Requested Range Not Satisfiable"