from pathlib import Path from django.test import RequestFactory from archivebox.misc.serve_static import serve_static_with_byterange_support def test_archive_file_response_uses_async_iterator_under_asgi(tmp_path: Path): output = tmp_path / "screenshot" / "output.png" output.parent.mkdir() output.write_bytes(b"0123456789") request = RequestFactory().get("/screenshot/output.png", HTTP_RANGE="bytes=2-5") request.scope = {"type": "http"} response = serve_static_with_byterange_support( request, "screenshot/output.png", document_root=tmp_path, ) assert response.is_async is True assert response.status_code == 206 assert response["Content-Range"] == "bytes 2-5/10" assert response["Content-Length"] == "4" def test_path_routed_directory_index_keeps_file_browsing_context(tmp_path: Path): plugin_dir = tmp_path / "archivewebpage" plugin_dir.mkdir() (plugin_dir / "archivewebpage.wacz").write_bytes(b"wacz") request = RequestFactory().get("/snapshot/snapshot-id/?files=1") response = serve_static_with_byterange_support( request, "", document_root=tmp_path, show_indexes=True, is_archive_replay=True, ) html = response.content.decode() assert 'href="archivewebpage/?files=1"' in html assert 'href="/snapshot/snapshot-id/"' in html nested_request = RequestFactory().get("/snapshot/snapshot-id/archivewebpage/?files=1") nested_response = serve_static_with_byterange_support( nested_request, "archivewebpage", document_root=tmp_path, show_indexes=True, is_archive_replay=True, ) nested_html = nested_response.content.decode() assert 'href="../?files=1"' in nested_html assert 'href="/snapshot/snapshot-id/"' in nested_html assert 'href="archivewebpage.wacz"' in nested_html