2020-08-11 03:41:33 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim: ai ts=4 sts=4 et sw=4 nu
|
|
|
|
|
2020-07-22 13:22:36 -07:00
|
|
|
import os
|
2020-08-18 22:59:22 -07:00
|
|
|
import time
|
2020-12-09 10:55:59 +00:00
|
|
|
import json
|
2020-07-22 21:11:18 -07:00
|
|
|
from io import BytesIO
|
|
|
|
|
2020-07-27 01:51:55 +00:00
|
|
|
import pytest
|
2021-01-12 16:17:46 +00:00
|
|
|
import requests
|
2020-07-27 01:51:55 +00:00
|
|
|
|
2020-07-22 13:22:36 -07:00
|
|
|
from warcio import ArchiveIterator
|
2020-08-10 18:48:01 -07:00
|
|
|
from jinja2 import Environment, PackageLoader
|
2022-01-07 13:52:57 +00:00
|
|
|
from zimscraperlib.zim import Archive
|
2020-07-22 13:22:36 -07:00
|
|
|
|
2023-11-14 14:56:23 +01:00
|
|
|
from warc2zim.url_rewriting import canonicalize
|
|
|
|
from warc2zim.converter import iter_warc_records
|
|
|
|
from warc2zim.items import get_record_url
|
|
|
|
|
|
|
|
# Import last to not mask warc2zim module
|
|
|
|
from warc2zim.main import warc2zim
|
|
|
|
|
2020-07-27 01:51:55 +00:00
|
|
|
|
2020-07-22 13:22:36 -07:00
|
|
|
|
2021-10-29 16:52:36 +00:00
|
|
|
TEST_DATA_DIR = os.path.join(os.path.dirname(os.path.realpath(__file__)), "data")
|
|
|
|
|
|
|
|
|
|
|
|
# ============================================================================
|
2020-08-02 22:59:41 +00:00
|
|
|
CMDLINES = [
|
|
|
|
["example-response.warc"],
|
2020-12-09 10:55:59 +00:00
|
|
|
["example-response.warc", "--progress-file", "progress.json"],
|
2020-08-03 17:22:29 -07:00
|
|
|
["example-resource.warc.gz", "--favicon", "https://example.com/some/favicon.ico"],
|
2023-05-22 09:27:50 +00:00
|
|
|
["example-resource.warc.gz", "--favicon", "https://www.google.com/favicon.ico"],
|
2020-10-06 04:40:03 +00:00
|
|
|
["example-revisit.warc.gz"],
|
2020-08-03 17:22:29 -07:00
|
|
|
[
|
|
|
|
"example-revisit.warc.gz",
|
|
|
|
"-u",
|
|
|
|
"http://example.iana.org/",
|
|
|
|
"--lang",
|
|
|
|
"eng",
|
|
|
|
],
|
2020-08-02 23:04:32 +00:00
|
|
|
[
|
|
|
|
"example-utf8.warc",
|
|
|
|
"-u",
|
|
|
|
"https://httpbin.org/anything/utf8=%E2%9C%93?query=test&a=b&1=%E2%9C%93",
|
|
|
|
],
|
2020-10-06 04:40:03 +00:00
|
|
|
["single-page-test.warc"],
|
2020-07-27 08:26:57 -07:00
|
|
|
]
|
|
|
|
|
2020-07-22 13:22:36 -07:00
|
|
|
|
2020-08-03 17:22:29 -07:00
|
|
|
@pytest.fixture(params=CMDLINES, ids=[" ".join(cmds) for cmds in CMDLINES])
|
2020-08-02 22:59:41 +00:00
|
|
|
def cmdline(request):
|
2020-07-22 13:22:36 -07:00
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
2021-10-29 16:52:36 +00:00
|
|
|
# ============================================================================
|
|
|
|
FUZZYCHECKS = [
|
|
|
|
{
|
|
|
|
"filename": "video-yt.warc.gz",
|
|
|
|
"entries": [
|
|
|
|
"H/youtube.fuzzy.replayweb.page/get_video_info?video_id=aT-Up5Y4uRI",
|
|
|
|
"H/youtube.fuzzy.replayweb.page/videoplayback?id=o-AE3bg3qVNY-gAWwYgL52vgpHKJe9ijdbu2eciNi5Uo_w",
|
2021-10-29 16:55:45 +00:00
|
|
|
],
|
2021-10-29 16:52:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"filename": "video-yt-2.warc.gz",
|
|
|
|
"entries": [
|
|
|
|
"H/youtube.fuzzy.replayweb.page/youtubei/v1/player?videoId=aT-Up5Y4uRI",
|
|
|
|
"H/youtube.fuzzy.replayweb.page/videoplayback?id=o-AGDtIqpFRmvgVVZk96wgGyFxL_SFSdpBxs0iBHatQpRD",
|
2021-10-29 16:55:45 +00:00
|
|
|
],
|
2021-10-29 16:52:36 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"filename": "video-vimeo.warc.gz",
|
|
|
|
"entries": [
|
|
|
|
"H/vimeo.fuzzy.replayweb.page/video/347119375",
|
2021-10-29 16:55:45 +00:00
|
|
|
"H/vimeo-cdn.fuzzy.replayweb.page/01/4423/13/347119375/1398505169.mp4",
|
|
|
|
],
|
|
|
|
},
|
2021-10-29 16:52:36 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(params=FUZZYCHECKS, ids=[fuzzy["filename"] for fuzzy in FUZZYCHECKS])
|
|
|
|
def fuzzycheck(request):
|
|
|
|
return request.param
|
|
|
|
|
|
|
|
|
2020-07-22 13:22:36 -07:00
|
|
|
# ============================================================================
|
|
|
|
class TestWarc2Zim(object):
|
2020-08-02 00:12:50 -07:00
|
|
|
def list_articles(self, zimfile):
|
2022-01-07 13:52:57 +00:00
|
|
|
zim_fh = Archive(zimfile)
|
|
|
|
for x in range(zim_fh.entry_count):
|
|
|
|
yield zim_fh.get_entry_by_id(x)
|
|
|
|
|
|
|
|
def get_metadata(self, zimfile, name):
|
|
|
|
zim_fh = Archive(zimfile)
|
|
|
|
return zim_fh.get_metadata(name)
|
2020-08-02 00:12:50 -07:00
|
|
|
|
|
|
|
def get_article(self, zimfile, path):
|
2022-01-07 13:52:57 +00:00
|
|
|
zim_fh = Archive(zimfile)
|
|
|
|
return zim_fh.get_content(path)
|
2020-08-02 00:12:50 -07:00
|
|
|
|
2020-08-03 09:26:48 -07:00
|
|
|
def get_article_raw(self, zimfile, path):
|
2022-01-07 13:52:57 +00:00
|
|
|
zim_fh = Archive(zimfile)
|
|
|
|
return zim_fh.get_item(path)
|
2020-08-03 09:26:48 -07:00
|
|
|
|
2020-07-22 13:22:36 -07:00
|
|
|
def verify_warc_and_zim(self, warcfile, zimfile):
|
|
|
|
assert os.path.isfile(warcfile)
|
|
|
|
assert os.path.isfile(zimfile)
|
|
|
|
|
2020-08-11 03:43:54 +00:00
|
|
|
# autoescape=False to allow injecting html entities from translated text
|
2020-08-10 18:48:01 -07:00
|
|
|
env = Environment(
|
|
|
|
loader=PackageLoader("warc2zim", "templates"),
|
|
|
|
extensions=["jinja2.ext.i18n"],
|
2020-08-11 03:43:54 +00:00
|
|
|
autoescape=False,
|
2020-08-10 18:48:01 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
head_insert = env.get_template("sw_check.html").render().encode("utf-8")
|
|
|
|
|
2020-07-22 13:22:36 -07:00
|
|
|
# track to avoid checking duplicates, which are not written to ZIM
|
|
|
|
warc_urls = set()
|
|
|
|
|
2022-01-07 13:52:57 +00:00
|
|
|
zim_fh = Archive(zimfile)
|
2021-10-29 16:52:36 +00:00
|
|
|
for record in iter_warc_records([warcfile]):
|
|
|
|
url = get_record_url(record)
|
|
|
|
if not url:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if url in warc_urls:
|
|
|
|
continue
|
|
|
|
|
|
|
|
if record.rec_type not in (("response", "resource", "revisit")):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# ignore revisit records that are to the same url
|
|
|
|
if (
|
|
|
|
record.rec_type == "revisit"
|
|
|
|
and record.rec_headers["WARC-Refers-To-Target-URI"] == url
|
|
|
|
):
|
|
|
|
continue
|
|
|
|
|
|
|
|
# parse headers as record, ensure headers match
|
|
|
|
url_no_scheme = url.split("//", 2)[1]
|
|
|
|
print(url_no_scheme)
|
2022-01-07 13:52:57 +00:00
|
|
|
parsed_record = next(
|
|
|
|
ArchiveIterator(BytesIO(zim_fh.get_content("H/" + url_no_scheme)))
|
|
|
|
)
|
2021-10-29 16:52:36 +00:00
|
|
|
|
|
|
|
assert record.rec_headers == parsed_record.rec_headers
|
|
|
|
assert record.http_headers == parsed_record.http_headers
|
|
|
|
|
|
|
|
# ensure payloads match
|
|
|
|
try:
|
2022-01-07 13:52:57 +00:00
|
|
|
payload = zim_fh.get_item("A/" + url_no_scheme)
|
2021-10-29 16:52:36 +00:00
|
|
|
except KeyError:
|
|
|
|
payload = None
|
|
|
|
|
|
|
|
if record.rec_type == "revisit" or (
|
|
|
|
record.http_headers and record.http_headers.get("Content-Length") == "0"
|
|
|
|
):
|
|
|
|
assert not payload
|
|
|
|
else:
|
|
|
|
payload_content = payload.content.tobytes()
|
|
|
|
|
2023-02-02 15:32:10 +00:00
|
|
|
# if HTML, still need to account for the head insert, otherwise should have exact match
|
|
|
|
if payload.mimetype.startswith("text/html"):
|
2021-10-29 16:52:36 +00:00
|
|
|
assert head_insert in payload_content
|
|
|
|
assert (
|
|
|
|
payload_content.replace(head_insert, b"")
|
|
|
|
== record.buffered_stream.read()
|
|
|
|
)
|
2020-07-22 21:31:18 -07:00
|
|
|
else:
|
2021-10-29 16:52:36 +00:00
|
|
|
assert payload_content == record.buffered_stream.read()
|
2020-07-22 21:31:18 -07:00
|
|
|
|
2021-10-29 16:52:36 +00:00
|
|
|
warc_urls.add(url)
|
2020-07-22 21:31:18 -07:00
|
|
|
|
2020-10-21 20:52:05 +00:00
|
|
|
def test_canonicalize(self):
|
|
|
|
assert canonicalize("http://example.com/?foo=bar") == "example.com/?foo=bar"
|
|
|
|
|
|
|
|
assert canonicalize("https://example.com/?foo=bar") == "example.com/?foo=bar"
|
|
|
|
|
|
|
|
assert (
|
|
|
|
canonicalize("https://example.com/some/path/http://example.com/?foo=bar")
|
|
|
|
== "example.com/some/path/http://example.com/?foo=bar"
|
|
|
|
)
|
|
|
|
|
|
|
|
assert (
|
|
|
|
canonicalize("example.com/some/path/http://example.com/?foo=bar")
|
|
|
|
== "example.com/some/path/http://example.com/?foo=bar"
|
|
|
|
)
|
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
def test_warc_to_zim_specify_params_and_metadata(self, tmp_path):
|
2020-08-03 17:22:29 -07:00
|
|
|
zim_output = "zim-out-filename.zim"
|
2020-07-27 08:26:57 -07:00
|
|
|
warc2zim(
|
|
|
|
[
|
|
|
|
"-v",
|
2020-08-18 22:59:22 -07:00
|
|
|
os.path.join(TEST_DATA_DIR, "example-response.warc"),
|
|
|
|
"--name",
|
|
|
|
"example-response",
|
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--zim-file",
|
2020-07-27 08:26:57 -07:00
|
|
|
zim_output,
|
Multi-Page Mode via SW (#28)
* new replay system using wabac.js directly, without replaywebpage UI or single page mode.
* ignore wabac sw.js from github (automatically added on packaging)
bump version of wabac.js
* use more generic 'notFoundPage.html' to handle all navigate errors
notFoundPage includes live redirect handling (#23), including checking for kiwix-serve redirect warning page
don't use format() for templates, confuses codefactor, juse juse replace()
don't write empty A/ records, if content-length is 0, don't write payload, addresses #21
* formatting, bump to latest wabac.js as default
* lint tweaks
* tests: add more revisit tests, don't clear raw_stream (needed by warcio), ensure indexed urls are tracked
2020-08-03 09:47:32 -07:00
|
|
|
"-r",
|
2023-08-30 11:31:25 +00:00
|
|
|
"https://cdn.jsdelivr.net/npm/@webrecorder/wabac@2.16.11/dist/",
|
2020-08-02 00:12:50 -07:00
|
|
|
"--tags",
|
|
|
|
"some",
|
|
|
|
"--tags",
|
|
|
|
"foo",
|
|
|
|
"--desc",
|
|
|
|
"test zim",
|
|
|
|
"--tags",
|
2020-08-02 18:58:08 +00:00
|
|
|
"bar",
|
2020-08-03 17:22:29 -07:00
|
|
|
"--title",
|
|
|
|
"Some Title",
|
2020-07-27 08:26:57 -07:00
|
|
|
]
|
|
|
|
)
|
2020-07-22 21:31:18 -07:00
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
zim_output = tmp_path / zim_output
|
|
|
|
|
2020-07-22 21:31:18 -07:00
|
|
|
assert os.path.isfile(zim_output)
|
|
|
|
|
2020-08-02 00:12:50 -07:00
|
|
|
all_articles = {
|
2022-01-07 13:52:57 +00:00
|
|
|
article.path: article.title for article in self.list_articles(zim_output)
|
2020-08-02 00:12:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
assert all_articles == {
|
|
|
|
# entries from WARC
|
|
|
|
"A/example.com/": "Example Domain",
|
2022-01-07 13:52:57 +00:00
|
|
|
"H/example.com/": "H/example.com/",
|
2020-08-02 00:12:50 -07:00
|
|
|
# replay system files
|
2022-01-07 13:52:57 +00:00
|
|
|
"A/index.html": "A/index.html",
|
|
|
|
"A/load.js": "A/load.js",
|
|
|
|
"A/404.html": "A/404.html",
|
|
|
|
"A/sw.js": "A/sw.js",
|
|
|
|
"A/topFrame.html": "A/topFrame.html",
|
2020-08-02 00:12:50 -07:00
|
|
|
}
|
|
|
|
|
2022-01-07 13:52:57 +00:00
|
|
|
zim_fh = Archive(zim_output)
|
|
|
|
|
|
|
|
# ZIM metadata
|
|
|
|
assert list(zim_fh.metadata.keys()) == [
|
|
|
|
"Counter",
|
|
|
|
"Creator",
|
|
|
|
"Date",
|
|
|
|
"Description",
|
|
|
|
"Language",
|
|
|
|
"Name",
|
|
|
|
"Publisher",
|
|
|
|
"Scraper",
|
|
|
|
"Tags",
|
|
|
|
"Title",
|
|
|
|
]
|
|
|
|
|
|
|
|
assert zim_fh.has_fulltext_index
|
|
|
|
assert zim_fh.has_title_index
|
|
|
|
|
|
|
|
assert self.get_metadata(zim_output, "Description") == b"test zim"
|
2020-08-19 18:36:04 +00:00
|
|
|
assert (
|
2022-01-07 13:52:57 +00:00
|
|
|
self.get_metadata(zim_output, "Tags")
|
2020-08-19 18:36:04 +00:00
|
|
|
== b"_ftindex:yes;_category:other;_sw:yes;some;foo;bar"
|
|
|
|
)
|
2022-01-07 13:52:57 +00:00
|
|
|
assert self.get_metadata(zim_output, "Title") == b"Some Title"
|
2020-08-02 00:12:50 -07:00
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
def test_warc_to_zim(self, cmdline, tmp_path):
|
2020-08-18 23:15:07 -07:00
|
|
|
# intput filename
|
2020-08-02 22:59:41 +00:00
|
|
|
filename = cmdline[0]
|
2020-08-03 17:22:29 -07:00
|
|
|
|
2020-08-18 23:15:07 -07:00
|
|
|
# set intput filename (first arg) to absolute path from test dir
|
2020-08-18 22:59:22 -07:00
|
|
|
warcfile = os.path.join(TEST_DATA_DIR, filename)
|
2020-08-18 23:15:07 -07:00
|
|
|
cmdline[0] = warcfile
|
2020-07-22 21:31:18 -07:00
|
|
|
|
2020-08-18 23:15:07 -07:00
|
|
|
cmdline.extend(["--output", str(tmp_path), "--name", filename])
|
2020-08-18 22:59:22 -07:00
|
|
|
|
2020-08-03 17:22:29 -07:00
|
|
|
warc2zim(cmdline)
|
2020-07-22 21:31:18 -07:00
|
|
|
|
2020-08-18 23:15:07 -07:00
|
|
|
zimfile = filename + "_" + time.strftime("%Y-%m") + ".zim"
|
2020-07-22 21:31:18 -07:00
|
|
|
|
2020-12-09 10:55:59 +00:00
|
|
|
if "--progress-file" in cmdline:
|
|
|
|
with open(tmp_path / "progress.json", "r") as fh:
|
|
|
|
progress = json.load(fh)
|
|
|
|
assert (
|
|
|
|
progress["written"] > 0
|
|
|
|
and progress["total"] > 0
|
|
|
|
and progress["written"] <= progress["total"]
|
|
|
|
)
|
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
self.verify_warc_and_zim(warcfile, tmp_path / zimfile)
|
2020-07-22 21:31:18 -07:00
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
def test_same_domain_only(self, tmp_path):
|
2020-08-03 17:22:29 -07:00
|
|
|
zim_output = "same-domain.zim"
|
|
|
|
warc2zim(
|
|
|
|
[
|
2020-08-18 22:59:22 -07:00
|
|
|
os.path.join(TEST_DATA_DIR, "example-revisit.warc.gz"),
|
2020-08-03 17:22:29 -07:00
|
|
|
"--favicon",
|
|
|
|
"http://example.com/favicon.ico",
|
2020-10-06 04:40:03 +00:00
|
|
|
"--include-domains",
|
|
|
|
"example.com/",
|
2020-08-03 17:22:29 -07:00
|
|
|
"--lang",
|
|
|
|
"eng",
|
2020-08-18 22:59:22 -07:00
|
|
|
"--zim-file",
|
2020-08-03 17:22:29 -07:00
|
|
|
zim_output,
|
2020-08-18 22:59:22 -07:00
|
|
|
"--name",
|
|
|
|
"same-domain",
|
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
2020-08-03 17:22:29 -07:00
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
zim_output = tmp_path / zim_output
|
|
|
|
|
2020-08-03 17:22:29 -07:00
|
|
|
for article in self.list_articles(zim_output):
|
2022-01-07 13:52:57 +00:00
|
|
|
url = article.path
|
2020-08-03 17:22:29 -07:00
|
|
|
# ignore the replay files, which have only one path segment
|
|
|
|
if url.startswith("A/") and len(url.split("/")) > 2:
|
|
|
|
assert url.startswith("A/example.com/")
|
|
|
|
|
2020-09-25 02:47:16 +00:00
|
|
|
def test_skip_self_redirect(self, tmp_path):
|
|
|
|
zim_output = "self-redir.zim"
|
|
|
|
warc2zim(
|
|
|
|
[
|
|
|
|
os.path.join(TEST_DATA_DIR, "self-redirect.warc"),
|
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--zim-file",
|
|
|
|
zim_output,
|
|
|
|
"--name",
|
|
|
|
"self-redir",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
zim_output = tmp_path / zim_output
|
|
|
|
|
|
|
|
for article in self.list_articles(zim_output):
|
2022-01-07 13:52:57 +00:00
|
|
|
url = article.path
|
2020-09-25 02:47:16 +00:00
|
|
|
if url.startswith("H/"):
|
|
|
|
# ensure there is only one H/ record, and its a 200 (not 301)
|
|
|
|
assert url == "H/kiwix.org/"
|
|
|
|
assert b"HTTP/1.1 200 OK" in self.get_article(
|
|
|
|
zim_output, "H/kiwix.org/"
|
|
|
|
)
|
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
def test_include_domains_favicon_and_language(self, tmp_path):
|
2020-08-03 17:22:29 -07:00
|
|
|
zim_output = "spt.zim"
|
2020-08-03 10:26:24 -07:00
|
|
|
warc2zim(
|
|
|
|
[
|
2020-08-18 22:59:22 -07:00
|
|
|
os.path.join(TEST_DATA_DIR, "single-page-test.warc"),
|
2020-08-03 10:26:24 -07:00
|
|
|
"-i",
|
|
|
|
"reseau-canope.fr",
|
2020-08-18 22:59:22 -07:00
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--zim-file",
|
2020-08-03 10:26:24 -07:00
|
|
|
zim_output,
|
2020-08-18 22:59:22 -07:00
|
|
|
"--name",
|
|
|
|
"spt",
|
2020-08-03 10:26:24 -07:00
|
|
|
]
|
|
|
|
)
|
2020-08-03 09:26:48 -07:00
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
zim_output = tmp_path / zim_output
|
|
|
|
|
2020-08-03 09:26:48 -07:00
|
|
|
for article in self.list_articles(zim_output):
|
2022-01-07 13:52:57 +00:00
|
|
|
url = article.path
|
2020-08-03 17:22:29 -07:00
|
|
|
# ignore the replay files, which have only one path segment
|
2020-08-03 09:26:48 -07:00
|
|
|
if url.startswith("A/") and len(url.split("/")) > 2:
|
2020-08-03 17:22:29 -07:00
|
|
|
assert "reseau-canope.fr/" in url
|
2020-08-03 09:26:48 -07:00
|
|
|
|
|
|
|
# test detected language
|
2022-01-07 13:52:57 +00:00
|
|
|
assert self.get_metadata(zim_output, "Language") == b"fra"
|
2020-08-03 09:26:48 -07:00
|
|
|
|
|
|
|
# test detected favicon
|
2022-01-07 13:52:57 +00:00
|
|
|
assert self.get_article(
|
|
|
|
zim_output,
|
|
|
|
"A/lesfondamentaux.reseau-canope.fr/fileadmin/template/img/favicon.ico",
|
2020-08-03 10:26:24 -07:00
|
|
|
)
|
2022-01-10 12:15:58 +00:00
|
|
|
assert self.get_metadata(zim_output, "Illustration_48x48@1")
|
2020-08-03 09:26:48 -07:00
|
|
|
|
2020-08-19 18:36:04 +00:00
|
|
|
# test default tags added
|
|
|
|
assert (
|
2022-01-07 13:52:57 +00:00
|
|
|
self.get_metadata(zim_output, "Tags")
|
2020-08-19 18:36:04 +00:00
|
|
|
== b"_ftindex:yes;_category:other;_sw:yes"
|
|
|
|
)
|
|
|
|
|
2020-10-19 18:00:10 +00:00
|
|
|
def test_all_warcs_root_dir(self, tmp_path):
|
|
|
|
zim_output = "test-all.zim"
|
|
|
|
warc2zim(
|
|
|
|
[
|
|
|
|
os.path.join(TEST_DATA_DIR),
|
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--zim-file",
|
|
|
|
zim_output,
|
|
|
|
"--name",
|
|
|
|
"test-all",
|
|
|
|
"--url",
|
|
|
|
"http://example.com",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
zim_output = tmp_path / zim_output
|
2020-10-22 01:05:57 +00:00
|
|
|
|
|
|
|
# check articles from different warc records in tests/data dir
|
|
|
|
|
2020-10-19 18:00:10 +00:00
|
|
|
# ensure trailing slash added
|
|
|
|
assert b'window.mainUrl = "http://example.com/"' in self.get_article(
|
|
|
|
zim_output, "A/index.html"
|
|
|
|
)
|
|
|
|
|
2020-10-22 01:05:57 +00:00
|
|
|
# from example.warc.gz
|
2020-10-19 18:00:10 +00:00
|
|
|
assert self.get_article(zim_output, "A/example.com/") != b""
|
2020-10-22 01:05:57 +00:00
|
|
|
|
|
|
|
# from single-page-test.warc
|
2020-10-19 18:00:10 +00:00
|
|
|
assert (
|
|
|
|
self.get_article(
|
|
|
|
zim_output, "A/lesfondamentaux.reseau-canope.fr/accueil.html"
|
|
|
|
)
|
|
|
|
!= b""
|
|
|
|
)
|
|
|
|
|
2020-10-22 01:05:57 +00:00
|
|
|
# timestamp fuzzy match from example-with-timestamp.warc
|
|
|
|
assert self.get_article(zim_output, "H/example.com/path.txt?") != b""
|
|
|
|
|
2021-10-29 16:52:36 +00:00
|
|
|
def test_fuzzy_urls(self, tmp_path, fuzzycheck):
|
|
|
|
zim_output = fuzzycheck["filename"] + ".zim"
|
2020-10-21 04:23:36 +00:00
|
|
|
warc2zim(
|
|
|
|
[
|
2021-10-29 16:52:36 +00:00
|
|
|
os.path.join(TEST_DATA_DIR, fuzzycheck["filename"]),
|
2020-10-21 04:23:36 +00:00
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--zim-file",
|
|
|
|
zim_output,
|
|
|
|
"--name",
|
|
|
|
"test-fuzzy",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
zim_output = tmp_path / zim_output
|
2021-10-29 02:20:01 +00:00
|
|
|
|
2021-10-29 16:52:36 +00:00
|
|
|
for entry in fuzzycheck["entries"]:
|
|
|
|
res = self.get_article(zim_output, entry)
|
|
|
|
assert b"Location: " in res
|
2021-10-29 02:20:01 +00:00
|
|
|
|
2021-10-29 20:17:02 +00:00
|
|
|
def test_local_replay_viewer_url(self, tmp_path):
|
|
|
|
zim_local_sw = "zim-local-sw.zim"
|
|
|
|
|
2021-11-02 00:42:45 +00:00
|
|
|
res = requests.get(
|
2023-08-30 11:31:25 +00:00
|
|
|
"https://cdn.jsdelivr.net/npm/@webrecorder/wabac@2.16.11/dist/sw.js"
|
2021-11-02 00:42:45 +00:00
|
|
|
)
|
2021-10-29 20:17:02 +00:00
|
|
|
|
|
|
|
with open(tmp_path / "sw.js", "wt") as fh:
|
|
|
|
fh.write(res.text)
|
|
|
|
|
|
|
|
warc2zim(
|
|
|
|
[
|
|
|
|
"-v",
|
|
|
|
os.path.join(TEST_DATA_DIR, "example-response.warc"),
|
|
|
|
"-r",
|
|
|
|
str(tmp_path) + "/",
|
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--name",
|
|
|
|
"local-sw",
|
|
|
|
"--zim-file",
|
|
|
|
zim_local_sw,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
|
|
|
assert os.path.isfile(tmp_path / zim_local_sw)
|
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
def test_error_bad_replay_viewer_url(self, tmp_path):
|
2020-08-03 17:22:29 -07:00
|
|
|
zim_output_not_created = "zim-out-not-created.zim"
|
2020-07-22 21:31:18 -07:00
|
|
|
with pytest.raises(Exception) as e:
|
2020-07-27 08:26:57 -07:00
|
|
|
warc2zim(
|
|
|
|
[
|
|
|
|
"-v",
|
2020-08-18 22:59:22 -07:00
|
|
|
os.path.join(TEST_DATA_DIR, "example-response.warc"),
|
2020-07-27 08:26:57 -07:00
|
|
|
"-r",
|
|
|
|
"x-invalid-x",
|
2020-08-18 22:59:22 -07:00
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--name",
|
|
|
|
"bad",
|
|
|
|
"--zim-file",
|
2020-07-27 08:26:57 -07:00
|
|
|
zim_output_not_created,
|
|
|
|
]
|
|
|
|
)
|
2020-07-27 01:56:13 +00:00
|
|
|
|
|
|
|
# zim file should not have been created since replay viewer could not be loaded
|
2021-10-29 20:17:02 +00:00
|
|
|
assert not os.path.isfile(tmp_path / zim_output_not_created)
|
2020-08-03 09:26:48 -07:00
|
|
|
|
2020-08-18 22:59:22 -07:00
|
|
|
def test_error_bad_main_page(self, tmp_path):
|
2020-08-03 17:22:29 -07:00
|
|
|
zim_output_not_created = "zim-out-not-created.zim"
|
2020-08-03 09:26:48 -07:00
|
|
|
with pytest.raises(Exception) as e:
|
|
|
|
warc2zim(
|
|
|
|
[
|
|
|
|
"-v",
|
2020-08-18 22:59:22 -07:00
|
|
|
os.path.join(TEST_DATA_DIR, "example-response.warc"),
|
2020-08-03 09:26:48 -07:00
|
|
|
"-u",
|
|
|
|
"https://no-such-url.example.com",
|
2020-08-18 22:59:22 -07:00
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--name",
|
|
|
|
"bad",
|
|
|
|
"--zim-file",
|
2020-08-03 09:26:48 -07:00
|
|
|
zim_output_not_created,
|
|
|
|
]
|
|
|
|
)
|
2020-10-06 04:26:34 +00:00
|
|
|
|
|
|
|
def test_args_only(self):
|
|
|
|
# error, name required
|
|
|
|
with pytest.raises(SystemExit) as e:
|
|
|
|
warc2zim([])
|
|
|
|
assert e.code == 2
|
|
|
|
|
|
|
|
# error, no such output directory
|
|
|
|
with pytest.raises(Exception) as e:
|
|
|
|
warc2zim(["--name", "test", "--output", "/no-such-dir"])
|
|
|
|
|
|
|
|
# success, special error code for no output files
|
|
|
|
assert warc2zim(["--name", "test", "--output", "./"]) == 100
|
2021-01-12 16:17:46 +00:00
|
|
|
|
|
|
|
def test_custom_css(self, tmp_path):
|
|
|
|
custom_css = b"* { background-color: red; }"
|
|
|
|
custom_css_path = tmp_path / "custom.css"
|
|
|
|
with open(custom_css_path, "wb") as fh:
|
|
|
|
fh.write(custom_css)
|
|
|
|
|
|
|
|
zim_output = "test-css.zim"
|
|
|
|
|
|
|
|
warc2zim(
|
|
|
|
[
|
|
|
|
os.path.join(TEST_DATA_DIR, "example-response.warc"),
|
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--zim-file",
|
|
|
|
zim_output,
|
|
|
|
"--name",
|
|
|
|
"test-css",
|
|
|
|
"--custom-css",
|
|
|
|
str(custom_css_path),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
zim_output = tmp_path / zim_output
|
|
|
|
|
|
|
|
res = self.get_article(zim_output, "A/example.com/")
|
|
|
|
assert "https://warc2zim.kiwix.app/custom.css".encode("utf-8") in res
|
|
|
|
|
|
|
|
res = self.get_article(zim_output, "A/warc2zim.kiwix.app/custom.css")
|
|
|
|
assert custom_css == res
|
|
|
|
|
|
|
|
def test_custom_css_remote(self, tmp_path):
|
|
|
|
zim_output = "test-css.zim"
|
|
|
|
url = (
|
|
|
|
"https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap-reboot.css"
|
|
|
|
)
|
|
|
|
|
|
|
|
warc2zim(
|
|
|
|
[
|
|
|
|
os.path.join(TEST_DATA_DIR, "example-response.warc"),
|
|
|
|
"--output",
|
|
|
|
str(tmp_path),
|
|
|
|
"--zim-file",
|
|
|
|
zim_output,
|
|
|
|
"--name",
|
|
|
|
"test-css",
|
|
|
|
"--custom-css",
|
|
|
|
url,
|
|
|
|
]
|
|
|
|
)
|
|
|
|
zim_output = tmp_path / zim_output
|
|
|
|
|
|
|
|
res = self.get_article(zim_output, "A/example.com/")
|
|
|
|
assert "https://warc2zim.kiwix.app/custom.css".encode("utf-8") in res
|
|
|
|
|
|
|
|
res = self.get_article(zim_output, "A/warc2zim.kiwix.app/custom.css")
|
|
|
|
assert res == requests.get(url).content
|