9from pytest_httpserver import HTTPServer
11from utils import save_safari_webarchive
14def test_creates_a_single_archive(out_path: pathlib.Path) -> None:
15 result = save_safari_webarchive(["https://example.com", out_path])
17 assert result["returncode"] == 0
18 assert result["stdout"] is not None
19 assert result["stderr"] is None
20 assert out_path.exists()
22 with open(out_path, "rb") as in_file:
23 webarchive = plistlib.load(in_file)
25 main_resource = webarchive["WebMainResource"]
27 assert main_resource["WebResourceURL"] == "https://example.com/"
29 main_resource["WebResourceData"]
30 == open("tests/fixtures/example.com.html", "rb").read()
34def test_does_not_overwrite_existing_archive(out_path: pathlib.Path) -> None:
35 out_path.write_text("This should still be here later")
37 result = save_safari_webarchive(["https://example.com", out_path])
43 "Unable to save webarchive file: "
44 "The file “example.webarchive” couldn’t be saved in the folder "
45 "“test_does_not_overwrite_existi0” because a file with "
46 "the same name already exists.\n"
50 assert out_path.read_text() == "This should still be here later"
53@pytest.mark.parametrize(
56 pytest.param([], id="no_arguments"),
57 pytest.param(["https://example.com"], id="not_enough_arguments"),
59 ["https://example.com", "example.webarchive", "--debug"],
60 id="too_many_arguments",
64def test_it_fails_if_you_supply_the_wrong_arguments(argv: list[str]) -> None:
65 result = save_safari_webarchive(argv)
70 "stderr": "Usage: save_safari_webarchive.swift <URL> <OUTPUT_PATH>\n",
74@pytest.mark.parametrize("status_code", ["403", "404", "410", "500"])
75def test_it_fails_if_non_200_status_code(
76 httpserver: HTTPServer, status_code: str, out_path: pathlib.Path
78 httpserver.expect_request("/error").respond_with_data(
79 "Boom!", status=int(status_code), content_type="text/plain"
81 url = f"http://localhost:{httpserver.port}/error"
83 result = save_safari_webarchive([url, out_path])
88 "stderr": f"Failed to load {url}: got status code {status_code}\n",
91 assert not out_path.exists()
94def test_it_fails_if_cannot_load_domain(out_path: pathlib.Path) -> None:
95 result = save_safari_webarchive(["https://doesnotexist.tk/", out_path])
100 "stderr": "Failed to load https://doesnotexist.tk/: A server with the specified hostname could not be found.\n",
103 assert not out_path.exists()
106# If I run this test in GitHub Actions, I get a warning to stderr but
107# the archive is saved correctly:
109# CFURLCopyResourcePropertyForKey failed because it was passed a URL which
112# This test passes locally; leave it for now -- I can come back to this.
114 os.getenv("CI") == "true",
115 reason="This test doesn’t work correctly in GitHub Actions",
117def test_it_fails_if_url_is_invalid(out_path: pathlib.Path) -> None:
118 result = save_safari_webarchive([">", out_path])
123 "stderr": "Unable to use > as a URL\n",
126 assert not out_path.exists()
129def test_prints_the_version() -> None:
130 result = save_safari_webarchive(["--version"])
132 assert result["returncode"] == 0
133 assert result["stderr"] is None
135 r"^save_safari_webarchive.swift [0-9]+\.[0-9]+\.[0-9]+\n$", result["stdout"]