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
|
# Copyright (c) 2018 crocoite contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
from tempfile import NamedTemporaryFile
from operator import itemgetter
from io import BytesIO
import pytest
from warcio.archiveiterator import ArchiveIterator
from warcio.warcwriter import WARCWriter
from warcio.statusandheaders import StatusAndHeaders
from .tools import mergeWarc
@pytest.fixture
def writer():
return WARCWriter (NamedTemporaryFile(), gzip=True)
def recordsEqual(golden, underTest):
for a, b in zip (golden, underTest):
# record ids are not predictable, so we cannot compare them
a.rec_headers.remove_header('WARC-Record-Id')
a.rec_headers.remove_header('WARC-Block-Digest')
b.rec_headers.remove_header('WARC-Record-Id')
b.rec_headers.remove_header('WARC-Block-Digest')
aheader = sorted(a.rec_headers.headers, key=itemgetter(0))
bheader = sorted(b.rec_headers.headers, key=itemgetter(0))
assert aheader == bheader
def test_unmodified(writer):
"""
Single request/response pair, no revisits
"""
records = []
httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
warcHeaders = {}
record = writer.create_warc_record ('http://example.com/', 'request', payload=BytesIO(b'foobar'),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
record = writer.create_warc_record ('http://example.com/', 'response', payload=BytesIO(b'data'),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
for r in records:
writer.write_record (r)
output = NamedTemporaryFile()
mergeWarc ([writer.out.name], output)
output.seek(0)
recordsEqual (records, ArchiveIterator (output))
def test_different_payload(writer):
"""
Duplicate URL, but different payload
"""
records = []
for i in range (2):
httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
warcHeaders = {}
record = writer.create_warc_record ('http://example.com/', 'request', payload=BytesIO(b'foobar'),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
record = writer.create_warc_record ('http://example.com/', 'response',
payload=BytesIO('data{}'.format(i).encode ('utf8')),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
for r in records:
writer.write_record (r)
output = NamedTemporaryFile()
mergeWarc ([writer.out.name], output)
output.seek(0)
recordsEqual (records, ArchiveIterator (output))
def makeRevisit(writer, ref, dup):
""" Make revisit record for reference """
dupHeaders = dup.rec_headers
refHeaders = ref.rec_headers
httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
record = writer.create_revisit_record (dupHeaders.get_header('WARC-Target-URI'),
digest=refHeaders.get_header('WARC-Payload-Digest'),
refers_to_uri=refHeaders.get_header('WARC-Target-URI'),
refers_to_date=refHeaders.get_header('WARC-Date'))
record.rec_headers.add_header ('WARC-Refers-To', refHeaders.get_header('WARC-Record-ID'))
record.rec_headers.add_header ('WARC-Truncated', 'length')
record.rec_headers.add_header ('Content-Length', '0')
# XXX: added by warcio, but this seems wrong
record.rec_headers.add_header ('Content-Type', 'application/http; msgtype=response')
return record
def test_resp_revisit_same_url(writer):
"""
Duplicate record for the same URL, creates a revisit
"""
records = []
for i in range (2):
httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
warcHeaders = {}
record = writer.create_warc_record ('http://example.com/', 'request', payload=BytesIO(b'foobar'),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
record = writer.create_warc_record ('http://example.com/', 'response', payload=BytesIO(b'data'),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
for r in records:
writer.write_record (r)
dup = records.pop ()
ref = records[1]
records.append (makeRevisit (writer, ref, dup))
output = NamedTemporaryFile()
mergeWarc ([writer.out.name], output)
output.seek(0)
recordsEqual (records, ArchiveIterator (output))
def test_resp_revisit_other_url(writer):
"""
Duplicate record for different URL, creates a revisit
"""
records = []
httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
warcHeaders = {}
record = writer.create_warc_record ('http://example.com/', 'request', payload=BytesIO(b'foobar'),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
record = writer.create_warc_record ('http://example.com/', 'response', payload=BytesIO(b'data'),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
warcHeaders = {}
record = writer.create_warc_record ('http://example.com/one', 'request', payload=BytesIO(b'foobar'),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
record = writer.create_warc_record ('http://example.com/one', 'response', payload=BytesIO(b'data'),
warc_headers_dict=warcHeaders, http_headers=httpHeaders)
records.append (record)
for r in records:
writer.write_record (r)
dup = records.pop ()
ref = records[1]
records.append (makeRevisit (writer, ref, dup))
output = NamedTemporaryFile()
mergeWarc ([writer.out.name], output)
output.seek(0)
recordsEqual (records, ArchiveIterator (output))
|