test_tools.py 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. # Copyright (c) 2018 crocoite contributors
  2. #
  3. # Permission is hereby granted, free of charge, to any person obtaining a copy
  4. # of this software and associated documentation files (the "Software"), to deal
  5. # in the Software without restriction, including without limitation the rights
  6. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. # copies of the Software, and to permit persons to whom the Software is
  8. # furnished to do so, subject to the following conditions:
  9. #
  10. # The above copyright notice and this permission notice shall be included in
  11. # all copies or substantial portions of the Software.
  12. #
  13. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. # THE SOFTWARE.
  20. from tempfile import NamedTemporaryFile
  21. from operator import itemgetter
  22. from io import BytesIO
  23. import pytest
  24. from warcio.archiveiterator import ArchiveIterator
  25. from warcio.warcwriter import WARCWriter
  26. from warcio.statusandheaders import StatusAndHeaders
  27. from pkg_resources import parse_version
  28. from .tools import mergeWarc, Errata, FixableErrata
  29. @pytest.fixture
  30. def writer():
  31. return WARCWriter (NamedTemporaryFile(), gzip=True)
  32. def recordsEqual(golden, underTest):
  33. for a, b in zip (golden, underTest):
  34. # record ids are not predictable, so we cannot compare them. Dito for
  35. # dates. Content-* seems to be added when writing to file.
  36. for x in {'WARC-Record-Id', 'WARC-Block-Digest', 'WARC-Date',
  37. 'Content-Length', 'Content-Type'}:
  38. a.rec_headers.remove_header(x)
  39. b.rec_headers.remove_header(x)
  40. aheader = sorted(a.rec_headers.headers, key=itemgetter(0))
  41. bheader = sorted(b.rec_headers.headers, key=itemgetter(0))
  42. assert aheader == bheader
  43. assert a.http_headers == b.http_headers
  44. def makeGolden(writer, records):
  45. # additional warcinfo is written. Content does not matter.
  46. record = writer.create_warc_record (
  47. '',
  48. 'warcinfo',
  49. payload=b'',
  50. warc_headers_dict={'Content-Type': 'application/json; charset=utf-8'})
  51. records.insert (0, record)
  52. return records
  53. def test_unmodified(writer):
  54. """
  55. Single request/response pair, no revisits
  56. """
  57. records = []
  58. httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
  59. warcHeaders = {}
  60. record = writer.create_warc_record ('http://example.com/', 'request', payload=BytesIO(b'foobar'),
  61. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  62. records.append (record)
  63. httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
  64. record = writer.create_warc_record ('http://example.com/', 'response', payload=BytesIO(b'data'),
  65. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  66. records.append (record)
  67. for r in records:
  68. writer.write_record (r)
  69. output = NamedTemporaryFile()
  70. mergeWarc ([writer.out.name], output)
  71. output.seek(0)
  72. recordsEqual (makeGolden (writer, records), ArchiveIterator (output))
  73. def test_different_payload(writer):
  74. """
  75. Duplicate URL, but different payload
  76. """
  77. records = []
  78. for i in range (2):
  79. httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
  80. warcHeaders = {}
  81. record = writer.create_warc_record ('http://example.com/', 'request', payload=BytesIO(b'foobar'),
  82. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  83. records.append (record)
  84. httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
  85. record = writer.create_warc_record ('http://example.com/', 'response',
  86. payload=BytesIO(f'data{i}'.encode ('utf8')),
  87. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  88. records.append (record)
  89. for r in records:
  90. writer.write_record (r)
  91. output = NamedTemporaryFile()
  92. mergeWarc ([writer.out.name], output)
  93. output.seek(0)
  94. recordsEqual (makeGolden (writer, records), ArchiveIterator (output))
  95. def makeRevisit(writer, ref, dup):
  96. """ Make revisit record for reference """
  97. dupHeaders = dup.rec_headers
  98. refHeaders = ref.rec_headers
  99. record = writer.create_revisit_record (dupHeaders.get_header('WARC-Target-URI'),
  100. digest=refHeaders.get_header('WARC-Payload-Digest'),
  101. refers_to_uri=refHeaders.get_header('WARC-Target-URI'),
  102. refers_to_date=refHeaders.get_header('WARC-Date'),
  103. http_headers=dup.http_headers)
  104. record.rec_headers.add_header ('WARC-Refers-To', refHeaders.get_header('WARC-Record-ID'))
  105. record.rec_headers.add_header ('WARC-Truncated', 'length')
  106. return record
  107. def test_resp_revisit_same_url(writer):
  108. """
  109. Duplicate record for the same URL, creates a revisit
  110. """
  111. records = []
  112. for i in range (2):
  113. httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
  114. warcHeaders = {}
  115. record = writer.create_warc_record ('http://example.com/', 'request', payload=BytesIO(b'foobar'),
  116. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  117. records.append (record)
  118. httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
  119. record = writer.create_warc_record ('http://example.com/', 'response', payload=BytesIO(b'data'),
  120. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  121. records.append (record)
  122. for r in records:
  123. writer.write_record (r)
  124. dup = records.pop ()
  125. ref = records[1]
  126. records.append (makeRevisit (writer, ref, dup))
  127. output = NamedTemporaryFile()
  128. mergeWarc ([writer.out.name], output)
  129. output.seek(0)
  130. recordsEqual (makeGolden (writer, records), ArchiveIterator (output))
  131. def test_resp_revisit_other_url(writer):
  132. """
  133. Duplicate record for different URL, creates a revisit
  134. """
  135. records = []
  136. httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
  137. warcHeaders = {}
  138. record = writer.create_warc_record ('http://example.com/', 'request', payload=BytesIO(b'foobar'),
  139. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  140. records.append (record)
  141. httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
  142. record = writer.create_warc_record ('http://example.com/', 'response', payload=BytesIO(b'data'),
  143. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  144. records.append (record)
  145. httpHeaders = StatusAndHeaders('GET / HTTP/1.1', {}, is_http_request=True)
  146. warcHeaders = {}
  147. record = writer.create_warc_record ('http://example.com/one', 'request', payload=BytesIO(b'foobar'),
  148. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  149. records.append (record)
  150. httpHeaders = StatusAndHeaders('200 OK', {}, protocol='HTTP/1.1')
  151. record = writer.create_warc_record ('http://example.com/one', 'response', payload=BytesIO(b'data'),
  152. warc_headers_dict=warcHeaders, http_headers=httpHeaders)
  153. records.append (record)
  154. for r in records:
  155. writer.write_record (r)
  156. dup = records.pop ()
  157. ref = records[1]
  158. records.append (makeRevisit (writer, ref, dup))
  159. output = NamedTemporaryFile()
  160. mergeWarc ([writer.out.name], output)
  161. output.seek(0)
  162. recordsEqual (makeGolden (writer, records), ArchiveIterator (output))
  163. def test_errata_contains():
  164. """ Test version matching """
  165. e = Errata('some-uuid', 'description', ['a<1.0'])
  166. assert {'a': parse_version('0.1')} in e
  167. assert {'a': parse_version('1.0')} not in e
  168. assert {'b': parse_version('1.0')} not in e
  169. e = Errata('some-uuid', 'description', ['a<1.0,>0.1'])
  170. assert {'a': parse_version('0.1')} not in e
  171. assert {'a': parse_version('0.2')} in e
  172. assert {'a': parse_version('1.0')} not in e
  173. # a AND b
  174. e = Errata('some-uuid', 'description', ['a<1.0', 'b>1.0'])
  175. assert {'a': parse_version('0.1')} not in e
  176. assert {'b': parse_version('1.1')} not in e
  177. assert {'a': parse_version('0.1'), 'b': parse_version('1.1')} in e
  178. def test_errata_fixable ():
  179. e = Errata('some-uuid', 'description', ['a<1.0', 'b>1.0'])
  180. assert not e.fixable
  181. e = FixableErrata('some-uuid', 'description', ['a<1.0', 'b>1.0'])
  182. assert e.fixable