|
| 1 | + |
| 2 | +from io import BytesIO |
| 3 | +import os |
| 4 | + |
| 5 | +from warcio import WARCWriter, StatusAndHeaders |
| 6 | +from pywb.manager.manager import main as wb_manager |
| 7 | + |
| 8 | +from .base_config_test import BaseConfigTest, CollsDirMixin, fmod |
| 9 | + |
| 10 | + |
| 11 | +# ============================================================================ |
| 12 | +class TestRevisits(CollsDirMixin, BaseConfigTest): |
| 13 | + @classmethod |
| 14 | + def setup_class(cls): |
| 15 | + super(TestRevisits, cls).setup_class('config_test.yaml') |
| 16 | + |
| 17 | + |
| 18 | + def create_revisit_record(self, url, date, headers, refers_to_uri, refers_to_date): |
| 19 | + http_headers = StatusAndHeaders( |
| 20 | + "301 Permanent Redirect", headers, protocol="HTTP/1.0" |
| 21 | + ) |
| 22 | + |
| 23 | + return self.writer.create_revisit_record( |
| 24 | + url, |
| 25 | + digest="sha1:B6QJ6BNJ3R4B23XXMRKZKHLPGJY2VE4O", |
| 26 | + refers_to_uri=refers_to_uri, |
| 27 | + refers_to_date=refers_to_date, |
| 28 | + warc_headers_dict={"WARC-Date": date}, |
| 29 | + http_headers=http_headers, |
| 30 | + ) |
| 31 | + |
| 32 | + |
| 33 | + def create_response_record(self, url, date, headers, payload): |
| 34 | + http_headers = StatusAndHeaders( |
| 35 | + "301 Permanent Redirect", headers, protocol="HTTP/1.0" |
| 36 | + ) |
| 37 | + |
| 38 | + return self.writer.create_warc_record( |
| 39 | + url, |
| 40 | + record_type="response", |
| 41 | + http_headers=http_headers, |
| 42 | + payload=BytesIO(payload), |
| 43 | + warc_headers_dict={"WARC-Date": date}, |
| 44 | + length=len(payload), |
| 45 | + ) |
| 46 | + |
| 47 | + def create(self): |
| 48 | + payload = b"some\ntext" |
| 49 | + |
| 50 | + # record 1 |
| 51 | + self.writer.write_record( |
| 52 | + self.create_response_record( |
| 53 | + "http://example.com/orig-1", |
| 54 | + "2020-01-01T00:00:00Z", |
| 55 | + [ |
| 56 | + ("Content-Type", 'text/plain; charset="UTF-8"'), |
| 57 | + ("Location", "https://example.com/redirect-1"), |
| 58 | + ("Content-Length", str(len(payload))), |
| 59 | + ("Custom", "1"), |
| 60 | + ], |
| 61 | + payload, |
| 62 | + ) |
| 63 | + ) |
| 64 | + |
| 65 | + # record 2 |
| 66 | + self.writer.write_record( |
| 67 | + self.create_response_record( |
| 68 | + "http://example.com/orig-2", |
| 69 | + "2020-01-01T00:00:00Z", |
| 70 | + [ |
| 71 | + ("Content-Type", 'text/plain; charset="UTF-8"'), |
| 72 | + ("Location", "https://example.com/redirect-2"), |
| 73 | + ("Content-Length", str(len(payload))), |
| 74 | + ("Custom", "2"), |
| 75 | + ], |
| 76 | + payload, |
| 77 | + ) |
| 78 | + ) |
| 79 | + |
| 80 | + # record 3 |
| 81 | + self.writer.write_record( |
| 82 | + self.create_revisit_record( |
| 83 | + "http://example.com/orig-2", |
| 84 | + "2022-01-01T00:00:00Z", |
| 85 | + [ |
| 86 | + ("Content-Type", 'text/plain; charset="UTF-8"'), |
| 87 | + ("Location", "https://example.com/redirect-3"), |
| 88 | + ("Content-Length", str(len(payload))), |
| 89 | + ("Custom", "3"), |
| 90 | + ], |
| 91 | + refers_to_uri="http://example.com/orig-1", |
| 92 | + refers_to_date="2020-01-01T00:00:00Z", |
| 93 | + ) |
| 94 | + ) |
| 95 | + |
| 96 | + # record 4 |
| 97 | + self.writer.write_record( |
| 98 | + self.create_revisit_record( |
| 99 | + "http://example.com/", |
| 100 | + "2022-01-01T00:00:00Z", |
| 101 | + [ |
| 102 | + ("Content-Type", 'text/plain; charset="UTF-8"'), |
| 103 | + ("Location", "https://example.com/redirect-4"), |
| 104 | + ("Content-Length", str(len(payload))), |
| 105 | + ("Custom", "4"), |
| 106 | + ], |
| 107 | + refers_to_uri="http://example.com/orig-2", |
| 108 | + refers_to_date="2020-01-01T00:00:00Z", |
| 109 | + ) |
| 110 | + ) |
| 111 | + |
| 112 | + def test_init(self): |
| 113 | + filename = os.path.join(self.root_dir, 'redir.warc.gz') |
| 114 | + with open(filename, 'wb') as fh: |
| 115 | + self.writer = WARCWriter(fh, gzip=True) |
| 116 | + self.create() |
| 117 | + |
| 118 | + wb_manager(['init', 'revisits']) |
| 119 | + |
| 120 | + wb_manager(['add', 'revisits', filename]) |
| 121 | + |
| 122 | + assert os.path.isfile(os.path.join(self.root_dir, self.COLLS_DIR, 'revisits', 'indexes', 'index.cdxj')) |
| 123 | + |
| 124 | + def test_different_url_revisit_orig_headers(self, fmod): |
| 125 | + res = self.get('/revisits/20220101{0}/http://example.com/', fmod, status=301) |
| 126 | + assert res.headers["Custom"] == "4" |
| 127 | + assert res.headers["Location"].endswith("/20220101{0}/https://example.com/redirect-4".format(fmod)) |
| 128 | + assert res.text == 'some\ntext' |
| 129 | + |
| 130 | + def test_different_url_revisit_and_response(self, fmod): |
| 131 | + res = self.get('/revisits/20200101{0}/http://example.com/orig-2', fmod, status=301) |
| 132 | + assert res.headers["Custom"] == "2" |
| 133 | + assert res.headers["Location"].endswith("/20200101{0}/https://example.com/redirect-2".format(fmod)) |
| 134 | + assert res.text == 'some\ntext' |
| 135 | + |
| 136 | + res = self.get('/revisits/20220101{0}/http://example.com/orig-2', fmod, status=301) |
| 137 | + assert res.headers["Custom"] == "3" |
| 138 | + assert res.headers["Location"].endswith("/20220101{0}/https://example.com/redirect-3".format(fmod)) |
| 139 | + assert res.text == 'some\ntext' |
| 140 | + |
| 141 | + def test_orig(self, fmod): |
| 142 | + res = self.get('/revisits/20200101{0}/http://example.com/orig-1', fmod, status=301) |
| 143 | + assert res.headers["Custom"] == "1" |
| 144 | + assert res.headers["Location"].endswith("/20200101{0}/https://example.com/redirect-1".format(fmod)) |
| 145 | + assert res.text == 'some\ntext' |
| 146 | + |
0 commit comments