|
6 | 6 | from __future__ import annotations |
7 | 7 |
|
8 | 8 | from pathlib import Path |
| 9 | +from unittest.mock import MagicMock, patch |
| 10 | + |
| 11 | +import pytest |
9 | 12 |
|
10 | 13 | from metaobjects.loader.sources import ( |
11 | 14 | DirectorySource, |
@@ -108,3 +111,28 @@ def test_uri_source_format_inferred_from_path(tmp_path: Path) -> None: |
108 | 111 | p.write_text("k: v", encoding="utf-8") |
109 | 112 | src = UriSource(p.as_uri()) |
110 | 113 | assert src.format == MetaDataFormat.YAML |
| 114 | + |
| 115 | + |
| 116 | +def test_uri_source_http_scheme_decodes_utf8() -> None: |
| 117 | + fake_response = MagicMock() |
| 118 | + fake_response.read.return_value = b'{"metadata.root":{"package":"x","children":[]}}' |
| 119 | + fake_response.__enter__ = lambda self: self |
| 120 | + fake_response.__exit__ = lambda self, *a: None |
| 121 | + |
| 122 | + with patch( |
| 123 | + "metaobjects.loader.sources.uri_source.urlopen", |
| 124 | + return_value=fake_response, |
| 125 | + ) as mock_open: |
| 126 | + src = UriSource("https://example.com/meta.json") |
| 127 | + content = src.read() |
| 128 | + |
| 129 | + assert content == '{"metadata.root":{"package":"x","children":[]}}' |
| 130 | + # Verify timeout was passed |
| 131 | + args, kwargs = mock_open.call_args |
| 132 | + assert kwargs.get("timeout") == 30.0 |
| 133 | + |
| 134 | + |
| 135 | +def test_uri_source_rejects_unsupported_scheme() -> None: |
| 136 | + src = UriSource("ftp://example.com/foo.json", format=MetaDataFormat.JSON) |
| 137 | + with pytest.raises(ValueError, match="unsupported scheme"): |
| 138 | + src.read() |
0 commit comments