From f258eee451f14108f410aae333e0368237e0fdc4 Mon Sep 17 00:00:00 2001 From: Rohan McGovern Date: Wed, 3 Aug 2022 10:53:14 +1000 Subject: [PATCH] Raise coverage to 100% --- tests/test_cmd.py | 2 +- tests/test_http_fetcher.py | 55 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 tests/test_http_fetcher.py diff --git a/tests/test_cmd.py b/tests/test_cmd.py index 657a9bf..67f90e7 100644 --- a/tests/test_cmd.py +++ b/tests/test_cmd.py @@ -28,7 +28,7 @@ async def test_command(monkeypatch: pytest.MonkeyPatch, tmp_path: pathlib.Path): app.add_routes([web.static("/", THIS_DIR)]) async with test_utils.TestServer(app) as server: - repo_url = server.make_url("/sample_repo") + repo_url = server.make_url("/sample_repo///") monkeypatch.setattr("sys.argv", ["repo-autoindex", str(repo_url)]) entrypoint() diff --git a/tests/test_http_fetcher.py b/tests/test_http_fetcher.py new file mode 100644 index 0000000..70172a5 --- /dev/null +++ b/tests/test_http_fetcher.py @@ -0,0 +1,55 @@ +import gzip +import pytest +from aiohttp import web +from repo_autoindex._impl.api import http_fetcher + + +class FakeReader: + def __init__(self, body: bytes): + self.body = body + + async def read(self): + return self.body + + +class FakeResponse: + def __init__(self, body: bytes, content_type: str): + self.body = body + self.content_type = content_type + + async def __aenter__(self): + return self + + async def __aexit__(self, *_): + pass + + def raise_for_status(self): + pass + + @property + def content(self): + return FakeReader(self.body) + + +class FakeSession: + def __init__(self, body: bytes, content_type: str): + self.body = body + self.content_type = content_type + + def get(self, url: str) -> FakeResponse: + return FakeResponse(self.body, self.content_type) + + +@pytest.mark.parametrize( + "content_type", ["application/x-gzip", "application/octet-stream"] +) +async def test_http_fetcher_decompresses(content_type: str): + """http_fetcher will decompress certain responses.""" + text = "some text" + compressed = gzip.compress(text.encode("utf-8")) + + session = FakeSession(body=compressed, content_type=content_type) + fetcher = http_fetcher(session) + + response = await fetcher("/some/path.gz") + assert response == text