-
-
Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathtest_record_replay.py
220 lines (163 loc) · 8.34 KB
/
test_record_replay.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
from .base_config_test import BaseConfigTest, fmod, CollsDirMixin
from pywb.manager.manager import main as manager
from pywb.manager.autoindex import AutoIndexer
from pywb.warcserver.test.testutils import to_path, HttpBinLiveTests, TEST_WARC_PATH, TEST_CDX_PATH
from warcio import ArchiveIterator
import os
import time
import json
from mock import patch
import pytest
# ============================================================================
class TestRecordReplay(HttpBinLiveTests, CollsDirMixin, BaseConfigTest):
@classmethod
def setup_class(cls):
super(TestRecordReplay, cls).setup_class('config_test_record.yaml')
cls.indexer = AutoIndexer(interval=0.1)
cls.indexer.start()
@classmethod
def teardown_class(cls):
cls.indexer.stop()
super(TestRecordReplay, cls).teardown_class()
def test_init_coll(self):
manager(['init', 'test'])
assert os.path.isdir(os.path.join(self.root_dir, '_test_colls', 'test', 'archive'))
manager(['init', 'test2'])
assert os.path.isdir(os.path.join(self.root_dir, '_test_colls', 'test2', 'archive'))
def test_record_1(self):
res = self.testapp.get('/test/record/mp_/http://httpbin.org/get?A=B')
assert '"A": "B"' in res.text
def test_record_head(self):
res = self.testapp.head('/test/record/mp_/http://httpbin.org/get?A=B')
assert res.status_code == 200
assert res.text == ''
def test_replay_1(self, fmod):
self.ensure_empty()
fmod_slash = fmod + '/' if fmod else ''
res = self.get('/test/{0}http://httpbin.org/get?A=B', fmod_slash)
assert '"A": "B"' in res.text
def test_replay_head(self, fmod):
fmod_slash = fmod + '/' if fmod else ''
res = self.testapp.head('/test/{0}http://httpbin.org/get?A=B'.format(fmod_slash))
assert res.status_code == 200
assert res.text == ''
def test_record_2(self):
res = self.testapp.get('/test2/record/mp_/http://httpbin.org/get?C=D')
assert '"C": "D"' in res.text
def test_replay_2(self, fmod):
self.ensure_empty()
fmod_slash = fmod + '/' if fmod else ''
res = self.get('/test2/{0}http://httpbin.org/get?C=D', fmod_slash)
assert '"C": "D"' in res.text
def test_record_again_1(self):
res = self.testapp.get('/test/record/mp_/http://httpbin.org/get?C=D2')
assert '"C": "D2"' in res.text
def test_replay_again_1(self, fmod):
self.ensure_empty()
fmod_slash = fmod + '/' if fmod else ''
res = self.get('/test/{0}http://httpbin.org/get?C=D2', fmod_slash)
assert '"C": "D2"' in res.text
assert len(os.listdir(os.path.join(self.root_dir, '_test_colls', 'test', 'archive'))) == 1
assert len(os.listdir(os.path.join(self.root_dir, '_test_colls', 'test', 'indexes'))) == 1
def ensure_empty(self):
while not self.app.recorder.write_queue.empty():
time.sleep(0.1)
time.sleep(0.4)
def test_replay_all_coll(self, fmod):
self.ensure_empty()
fmod_slash = fmod + '/' if fmod else ''
res = self.get('/all/{0}http://httpbin.org/get?C=D', fmod_slash)
assert '"C": "D"' in res.text
res = self.get('/all/mp_/http://httpbin.org/get?A=B', fmod_slash)
assert '"A": "B"' in res.text
def test_cdx_all_coll(self):
res = self.testapp.get('/all/cdx?url=http://httpbin.org/get*&output=json')
cdxj_lines = [json.loads(line) for line in res.text.rstrip().split('\n')]
assert len(cdxj_lines) == 4
assert cdxj_lines[0]['url'] == 'http://httpbin.org/get?A=B'
assert cdxj_lines[1]['url'] == 'http://httpbin.org/get?A=B'
assert cdxj_lines[2]['url'] == 'http://httpbin.org/get?C=D'
assert cdxj_lines[3]['url'] == 'http://httpbin.org/get?C=D2'
assert cdxj_lines[0]['urlkey'] == 'org,httpbin)/get?__pywb_method=head&a=b'
assert cdxj_lines[1]['urlkey'] == 'org,httpbin)/get?a=b'
assert cdxj_lines[2]['urlkey'] == 'org,httpbin)/get?c=d'
assert cdxj_lines[3]['urlkey'] == 'org,httpbin)/get?c=d2'
assert cdxj_lines[0]['source'] == to_path('test/indexes/autoindex.cdxj')
assert cdxj_lines[1]['source'] == to_path('test/indexes/autoindex.cdxj')
assert cdxj_lines[2]['source'] == to_path('test2/indexes/autoindex.cdxj')
assert cdxj_lines[3]['source'] == to_path('test/indexes/autoindex.cdxj')
assert cdxj_lines[0]['source-coll'] == 'test'
assert cdxj_lines[1]['source-coll'] == 'test'
assert cdxj_lines[2]['source-coll'] == 'test2'
assert cdxj_lines[3]['source-coll'] == 'test'
assert cdxj_lines[1]['filename'] == cdxj_lines[3]['filename']
def test_timemap_all_coll(self):
res = self.testapp.get('/all/timemap/link/http://httpbin.org/get?C=D')
link_lines = res.text.rstrip().split('\n')
assert len(link_lines) == 4
assert to_path('collection="test2"') in link_lines[3]
#assert to_path('collection="test"') in link_lines[4]
# ============================================================================
class TestRecordCustomConfig(HttpBinLiveTests, CollsDirMixin, BaseConfigTest):
@classmethod
def setup_class(cls):
rec_custom = {'recorder': {'source_coll': 'live',
'filename_template': 'pywb-rec-test-{timestamp}.warcgz'}}
super(TestRecordCustomConfig, cls).setup_class('config_test_record.yaml', custom_config=rec_custom)
def test_init_and_rec(self):
manager(['init', 'test-new'])
dir_name = os.path.join(self.root_dir, '_test_colls', 'test-new', 'archive')
assert os.path.isdir(dir_name)
res = self.testapp.get('/test-new/record/mp_/http://httpbin.org/get?A=B')
assert '"A": "B"' in res.text
names = os.listdir(dir_name)
assert len(names) == 1
assert names[0].startswith('pywb-rec-test-')
assert names[0].endswith('.warcgz')
TestRecordCustomConfig.warc_name = os.path.join(dir_name, names[0])
@patch('pywb.rewrite.rewriteinputreq.has_brotli', False)
def test_no_brotli(self):
res = self.testapp.get('/test-new/record/mp_/http://httpbin.org/get?C=D',
headers={'Accept-Encoding': 'gzip, deflate, br'})
assert '"C": "D"' in res.text
with open(self.warc_name, 'rb') as fh:
for record in ArchiveIterator(fh):
last_record = record
assert record.http_headers['Accept-Encoding'] == 'gzip, deflate'
# ============================================================================
class TestRecordFilter(HttpBinLiveTests, CollsDirMixin, BaseConfigTest):
@classmethod
def setup_class(cls):
rec_custom = {'collections': {'fallback': {'sequence': [
{
'index_paths': os.path.join(TEST_CDX_PATH, 'example.cdxj'),
'archive_paths': TEST_WARC_PATH,
'name': 'example'
},{
'index':'$live',
'name': 'live'
}]}},
'recorder': {'source_coll': 'fallback',
'source_filter': 'live',
}
}
super(TestRecordFilter, cls).setup_class('config_test_record.yaml', custom_config=rec_custom)
manager(['init', 'test-new'])
def test_skip_existing(self):
dir_name = os.path.join(self.root_dir, '_test_colls', 'test-new', 'archive')
assert os.path.isdir(dir_name)
res = self.testapp.get('/fallback/cdx?url=http://example.com/?example=1')
assert res.text != ''
res = self.testapp.get('/test-new/record/mp_/http://example.com/?example=1')
assert 'Example Domain' in res.text
assert os.listdir(dir_name) == []
def test_record_new(self):
dir_name = os.path.join(self.root_dir, '_test_colls', 'test-new', 'archive')
assert os.path.isdir(dir_name)
res = self.testapp.get('/fallback/cdx?url=http://httpbin.org/get?A=B')
assert res.text == ''
res = self.testapp.get('/test-new/record/mp_/http://httpbin.org/get?A=B')
assert res.json['args']['A'] == 'B'
names = os.listdir(dir_name)
assert len(names) == 1
assert names[0].endswith('.warc.gz')