-
-
Notifications
You must be signed in to change notification settings - Fork 58
/
test_capture_http.py
303 lines (223 loc) · 10.1 KB
/
test_capture_http.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
import threading
from wsgiref.simple_server import make_server
from io import BytesIO
import time
# must be imported before 'requests'
from warcio.capture_http import capture_http
from pytest import raises
import requests
import json
import os
import tempfile
from warcio.archiveiterator import ArchiveIterator
from warcio.utils import BUFF_SIZE
from warcio.warcwriter import BufferWARCWriter, WARCWriter
# ==================================================================
# ==================================================================
class TestCaptureHttpBin(object):
@classmethod
def setup_class(cls):
from httpbin import app as httpbin_app
cls.temp_dir = tempfile.mkdtemp('warctest')
server = make_server('localhost', 0, httpbin_app)
addr, cls.port = server.socket.getsockname()
def run():
try:
server.serve_forever()
except Exception as e:
print(e)
thread = threading.Thread(target=run)
thread.daemon = True
thread.start()
time.sleep(0.1)
@classmethod
def teardown_class(cls):
os.rmdir(cls.temp_dir)
def test_get_no_capture(self):
url = 'http://localhost:{0}/get?foo=bar'.format(self.port)
res = requests.get(url, headers={'Host': 'httpbin.org'})
assert res.json()['args'] == {'foo': 'bar'}
def test_get(self):
url = 'http://localhost:{0}/get?foo=bar'.format(self.port)
with capture_http() as warc_writer:
res = requests.get(url, headers={'Host': 'httpbin.org'})
assert res.json()['args'] == {'foo': 'bar'}
ai = ArchiveIterator(warc_writer.get_stream())
response = next(ai)
assert response.rec_type == 'response'
assert response.rec_headers['WARC-Target-URI'] == url
assert response.rec_headers['WARC-IP-Address'] == '127.0.0.1'
assert res.json() == json.loads(response.content_stream().read().decode('utf-8'))
request = next(ai)
assert request.rec_type == 'request'
assert request.rec_headers['WARC-Target-URI'] == url
assert request.rec_headers['WARC-IP-Address'] == '127.0.0.1'
def test_post_cache_to_file(self):
warc_writer = BufferWARCWriter(gzip=False)
random_bytes = os.urandom(BUFF_SIZE * 2)
request_data = {"data": str(random_bytes)}
url = 'http://localhost:{0}/anything'.format(self.port)
with capture_http(warc_writer):
res = requests.post(
url,
headers={'Host': 'httpbin.org'},
json=request_data
)
assert res.json()["json"] == request_data
ai = ArchiveIterator(warc_writer.get_stream())
response = next(ai)
assert response.rec_type == 'response'
assert response.rec_headers['WARC-Target-URI'] == url
assert response.rec_headers['WARC-IP-Address'] == '127.0.0.1'
assert request_data == json.loads(response.content_stream().read().decode('utf-8'))["json"]
request = next(ai)
assert request.rec_type == 'request'
assert request.rec_headers['WARC-Target-URI'] == url
assert request.rec_headers['WARC-IP-Address'] == '127.0.0.1'
def test_post_json(self):
warc_writer = BufferWARCWriter(gzip=False)
with capture_http(warc_writer):
res = requests.post('http://localhost:{0}/post'.format(self.port),
headers={'Host': 'httpbin.org'},
json={'some': {'data': 'posted'}})
assert res.json()['json'] == {'some': {'data': 'posted'}}
# response
ai = ArchiveIterator(warc_writer.get_stream())
response = next(ai)
assert response.rec_type == 'response'
assert res.json() == json.loads(response.content_stream().read().decode('utf-8'))
# request
request = next(ai)
assert request.rec_type == 'request'
assert request.http_headers['Content-Type'] == 'application/json'
data = request.content_stream().read().decode('utf-8')
assert data == '{"some": {"data": "posted"}}'
def test_post_stream(self):
warc_writer = BufferWARCWriter(gzip=False)
def nop_filter(request, response, recorder):
assert request
assert response
return request, response
postbuff = BytesIO(b'somedatatopost')
url = 'http://localhost:{0}/post'.format(self.port)
with capture_http(warc_writer, nop_filter):
res = requests.post(url, data=postbuff)
# response
ai = ArchiveIterator(warc_writer.get_stream())
response = next(ai)
assert response.rec_type == 'response'
assert response.rec_headers['WARC-Target-URI'] == url
assert response.rec_headers['WARC-IP-Address'] == '127.0.0.1'
assert res.json() == json.loads(response.content_stream().read().decode('utf-8'))
# request
request = next(ai)
assert request.rec_type == 'request'
assert request.rec_headers['WARC-Target-URI'] == url
assert request.rec_headers['WARC-IP-Address'] == '127.0.0.1'
data = request.content_stream().read().decode('utf-8')
assert data == 'somedatatopost'
def test_post_chunked(self):
warc_writer = BufferWARCWriter(gzip=False)
def nop_filter(request, response, recorder):
assert request
assert response
return request, response
def gen():
return iter([b'some', b'data', b'to', b'post'])
#url = 'http://localhost:{0}/post'.format(self.port)
url = 'https://httpbin.org/post'
with capture_http(warc_writer, nop_filter, record_ip=False):
res = requests.post(url, data=gen(), headers={'Content-Type': 'application/json'})
# response
ai = ArchiveIterator(warc_writer.get_stream())
response = next(ai)
assert response.rec_type == 'response'
assert response.rec_headers['WARC-Target-URI'] == url
assert 'WARC-IP-Address' not in response.rec_headers
assert res.json() == json.loads(response.content_stream().read().decode('utf-8'))
# request
request = next(ai)
assert request.rec_type == 'request'
assert request.rec_headers['WARC-Target-URI'] == url
assert 'WARC-IP-Address' not in response.rec_headers
data = request.content_stream().read().decode('utf-8')
assert data == 'somedatatopost'
def test_skip_filter(self):
warc_writer = BufferWARCWriter(gzip=False)
def skip_filter(request, response, recorder):
assert request
assert response
return None, None
with capture_http(warc_writer, skip_filter):
res = requests.get('http://localhost:{0}/get?foo=bar'.format(self.port),
headers={'Host': 'httpbin.org'})
assert res.json()['args'] == {'foo': 'bar'}
# skipped, nothing written
assert warc_writer.get_contents() == b''
def test_capture_to_temp_file_append(self):
full_path = os.path.join(self.temp_dir, 'example.warc.gz')
url = 'http://localhost:{0}/get?foo=bar'.format(self.port)
with capture_http(full_path):
res = requests.get(url)
with capture_http(full_path):
res = requests.get(url)
with open(full_path, 'rb') as stream:
# response
ai = ArchiveIterator(stream)
response = next(ai)
assert response.rec_type == 'response'
assert response.rec_headers['WARC-Target-URI'] == url
# request
request = next(ai)
assert request.rec_type == 'request'
assert request.rec_headers['WARC-Target-URI'] == url
response = next(ai)
assert response.rec_type == 'response'
assert response.rec_headers['WARC-Target-URI'] == url
# request
request = next(ai)
assert request.rec_type == 'request'
assert request.rec_headers['WARC-Target-URI'] == url
os.remove(full_path)
def test_error_capture_to_temp_file_no_append_no_overwrite(self):
full_path = os.path.join(self.temp_dir, 'example2.warc.gz')
url = 'http://localhost:{0}/get?foo=bar'.format(self.port)
with capture_http(full_path, append=False):
res = requests.get(url)
with raises(OSError):
with capture_http(full_path, append=False):
res = requests.get(url)
os.remove(full_path)
def test_warc_1_1(self):
full_path = os.path.join(self.temp_dir, 'example3.warc')
url = 'http://localhost:{0}/get?foo=bar'.format(self.port)
with capture_http(full_path, append=False, warc_version='1.1', gzip=False):
res = requests.get(url)
with open(full_path, 'rb') as stream:
# response
ai = ArchiveIterator(stream)
response = next(ai)
assert response.rec_headers.protocol == 'WARC/1.1'
warc_date = response.rec_headers['WARC-Date']
# ISO 8601 date with fractional seconds (microseconds)
assert '.' in warc_date
assert len(warc_date) == 27
os.remove(full_path)
def test_remote(self):
with capture_http(warc_version='1.1', gzip=True) as writer:
requests.get('http://example.com/')
requests.get('https://google.com/')
expected = [('http://example.com/', 'response', True),
('http://example.com/', 'request', True),
('https://google.com/', 'response', True),
('https://google.com/', 'request', True),
('https://www.google.com/', 'response', True),
('https://www.google.com/', 'request', True)
]
actual = [
(record.rec_headers['WARC-Target-URI'],
record.rec_type,
'WARC-IP-Address' in record.rec_headers)
for record in ArchiveIterator(writer.get_stream())
]
assert actual == expected