|
|
@@ -1,4 +1,5 @@
|
|
|
import json
|
|
|
+import lzma
|
|
|
from pathlib import Path
|
|
|
from unittest.mock import Mock, patch
|
|
|
|
|
|
@@ -8,6 +9,8 @@ from metadata_manager.firmware_server.client import _CacheMeta
|
|
|
|
|
|
FIXTURES_DIR = Path(__file__).parent / "fixtures"
|
|
|
SAMPLE_MANIFEST = json.loads((FIXTURES_DIR / "manifest_sample.json").read_text())
|
|
|
+SAMPLE_JSON_BYTES = json.dumps(SAMPLE_MANIFEST).encode("utf-8")
|
|
|
+MANIFEST_XZ_URL = "https://firmware.ardupilot.org/manifest.json.xz"
|
|
|
|
|
|
|
|
|
class TestManifestIndex:
|
|
|
@@ -39,7 +42,7 @@ class TestManifestIndex:
|
|
|
class TestManifestClientCache:
|
|
|
def test_uses_cache_on_304(self, tmp_path):
|
|
|
client = ManifestClient(
|
|
|
- url="https://example.com/manifest.json",
|
|
|
+ url=MANIFEST_XZ_URL,
|
|
|
cache_dir=str(tmp_path),
|
|
|
)
|
|
|
client._write_cache(
|
|
|
@@ -48,7 +51,49 @@ class TestManifestClientCache:
|
|
|
)
|
|
|
|
|
|
response = Mock(status_code=304, headers={}, content=b"")
|
|
|
- with patch("metadata_manager.firmware_server.client.requests.get", return_value=response):
|
|
|
+ with patch(
|
|
|
+ "metadata_manager.firmware_server.client.requests.get",
|
|
|
+ return_value=response,
|
|
|
+ ) as mock_get:
|
|
|
raw = client.fetch_raw()
|
|
|
|
|
|
assert raw == b'{"format-version":"1.0.0","firmware":[]}'
|
|
|
+ mock_get.assert_called_once_with(
|
|
|
+ MANIFEST_XZ_URL,
|
|
|
+ headers={
|
|
|
+ "User-Agent": "CustomBuild/1.0",
|
|
|
+ "If-None-Match": '"abc"',
|
|
|
+ "If-Modified-Since": "Mon, 01 Jan 2024 00:00:00 GMT",
|
|
|
+ },
|
|
|
+ timeout=120,
|
|
|
+ )
|
|
|
+
|
|
|
+ def test_download_decompresses_xz_manifest(self, tmp_path):
|
|
|
+ client = ManifestClient(
|
|
|
+ url=MANIFEST_XZ_URL,
|
|
|
+ cache_dir=str(tmp_path),
|
|
|
+ )
|
|
|
+ compressed = lzma.compress(SAMPLE_JSON_BYTES)
|
|
|
+ response = Mock(
|
|
|
+ status_code=200,
|
|
|
+ content=compressed,
|
|
|
+ headers={
|
|
|
+ "ETag": '"etag123"',
|
|
|
+ "Last-Modified": "Mon, 01 Jan 2024 00:00:00 GMT",
|
|
|
+ },
|
|
|
+ )
|
|
|
+ response.raise_for_status = Mock()
|
|
|
+
|
|
|
+ with patch(
|
|
|
+ "metadata_manager.firmware_server.client.requests.get",
|
|
|
+ return_value=response,
|
|
|
+ ):
|
|
|
+ result = client.fetch()
|
|
|
+
|
|
|
+ assert result == SAMPLE_MANIFEST
|
|
|
+ assert client.cache_path.read_bytes() == SAMPLE_JSON_BYTES
|
|
|
+ meta = _CacheMeta.from_dict(
|
|
|
+ json.loads(client.meta_path.read_text(encoding="utf-8"))
|
|
|
+ )
|
|
|
+ assert meta.etag == '"etag123"'
|
|
|
+ assert meta.last_modified == "Mon, 01 Jan 2024 00:00:00 GMT"
|