|
3 | 3 | import warnings
|
4 | 4 | from datetime import datetime
|
5 | 5 | from tempfile import TemporaryDirectory
|
6 |
| -from typing import Any |
| 6 | +from typing import Any, Dict |
7 | 7 | from urllib.parse import parse_qs, urlsplit
|
8 | 8 |
|
9 | 9 | import pystac
|
@@ -146,6 +146,51 @@ def test_get_collections_single_slash(self, requests_mock: Mocker) -> None:
|
146 | 146 | assert len(history) == 2
|
147 | 147 | assert history[1].url == f"{root_url}collections"
|
148 | 148 |
|
| 149 | + def test_keep_trailing_slash_on_root(self, requests_mock: Mocker) -> None: |
| 150 | + pc_root_text = read_data_file("planetary-computer-root.json") |
| 151 | + root_url = "http://pystac-client.test/" |
| 152 | + requests_mock.get(root_url, status_code=200, text=pc_root_text) |
| 153 | + client = Client.open(root_url) |
| 154 | + self_href = client.get_self_href() |
| 155 | + assert self_href |
| 156 | + assert self_href.endswith("/") |
| 157 | + |
| 158 | + def test_fall_back_to_data_link_for_collections( |
| 159 | + self, requests_mock: Mocker |
| 160 | + ) -> None: |
| 161 | + pc_root_text = read_data_file("planetary-computer-root.json") |
| 162 | + root_url = "http://pystac-client.test/" |
| 163 | + requests_mock.get(root_url, status_code=200, text=pc_root_text) |
| 164 | + api = Client.open(root_url) |
| 165 | + api.set_self_href(None) |
| 166 | + pc_collection_dict = read_data_file( |
| 167 | + "planetary-computer-aster-l1t-collection.json", parse_json=True |
| 168 | + ) |
| 169 | + requests_mock.get( |
| 170 | + # the href of the data link |
| 171 | + "https://planetarycomputer.microsoft.com/api/stac/v1/collections", |
| 172 | + status_code=200, |
| 173 | + json={"collections": [pc_collection_dict], "links": []}, |
| 174 | + ) |
| 175 | + _ = next(api.get_collections()) |
| 176 | + history = requests_mock.request_history |
| 177 | + assert len(history) == 2 |
| 178 | + assert ( |
| 179 | + history[1].url |
| 180 | + == "https://planetarycomputer.microsoft.com/api/stac/v1/collections" |
| 181 | + ) |
| 182 | + |
| 183 | + def test_error_if_no_self_href_or_data_link(self, requests_mock: Mocker) -> None: |
| 184 | + pc_root = read_data_file("planetary-computer-root.json", parse_json=True) |
| 185 | + assert isinstance(pc_root, Dict) |
| 186 | + pc_root["links"] = [link for link in pc_root["links"] if link["rel"] != "data"] |
| 187 | + root_url = "http://pystac-client.test/" |
| 188 | + requests_mock.get(root_url, status_code=200, text=json.dumps(pc_root)) |
| 189 | + api = Client.open(root_url) |
| 190 | + api.set_self_href(None) |
| 191 | + with pytest.raises(ValueError): |
| 192 | + _ = api.get_collection("an-id") |
| 193 | + |
149 | 194 | def test_custom_request_parameters(self, requests_mock: Mocker) -> None:
|
150 | 195 | pc_root_text = read_data_file("planetary-computer-root.json")
|
151 | 196 | pc_collection_dict = read_data_file(
|
|
0 commit comments