-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathtest_app_prefix.py
210 lines (159 loc) · 7.97 KB
/
test_app_prefix.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import urllib
from typing import Optional
import pytest
from fastapi import APIRouter
from starlette.testclient import TestClient
from stac_fastapi.api.app import StacApi
from stac_fastapi.types.config import ApiSettings
def get_link(landing_page, rel_type, method: Optional[str] = None):
return next(
filter(
lambda link: link["rel"] == rel_type
and (not method or link.get("method") == method),
landing_page["links"],
),
None,
)
@pytest.mark.parametrize("prefix", ["", "/a_prefix"])
def test_api_prefix(TestCoreClient, prefix):
api_settings = ApiSettings(
openapi_url=f"{prefix}/api",
docs_url=f"{prefix}/api.html",
)
api = StacApi(
settings=api_settings,
client=TestCoreClient(),
router=APIRouter(prefix=prefix),
)
with TestClient(api.app, base_url="http://stac.io") as client:
landing = client.get(f"{prefix}/")
assert landing.status_code == 200, landing.json()
service_doc = client.get(f"{prefix}/api.html")
assert service_doc.status_code == 200, service_doc.text
service_desc = client.get(f"{prefix}/api")
assert service_desc.status_code == 200, service_desc.json()
conformance = client.get(f"{prefix}/conformance")
assert conformance.status_code == 200, conformance.json()
# NOTE: The collections/collection/items/item links do not have the prefix
# because they are created in the fixtures
collections = client.get(f"{prefix}/collections")
assert collections.status_code == 200, collections.json()
collection_id = collections.json()["collections"][0]["id"]
collection = client.get(f"{prefix}/collections/{collection_id}")
assert collection.status_code == 200, collection.json()
items = client.get(f"{prefix}/collections/{collection_id}/items")
assert items.status_code == 200, items.json()
item_id = items.json()["features"][0]["id"]
item = client.get(f"{prefix}/collections/{collection_id}/items/{item_id}")
assert item.status_code == 200, item.json()
link_tests = [
("root", "application/json", "/"),
("conformance", "application/json", "/conformance"),
("data", "application/json", "/collections"),
("search", "application/geo+json", "/search"),
("service-doc", "text/html", "/api.html"),
("service-desc", "application/vnd.oai.openapi+json;version=3.0", "/api"),
]
for rel_type, expected_media_type, expected_path in link_tests:
link = get_link(landing.json(), rel_type)
assert link is not None, f"Missing {rel_type} link in landing page"
assert link.get("type") == expected_media_type
link_path = urllib.parse.urlsplit(link.get("href")).path
assert link_path == prefix + expected_path
resp = client.get(prefix + expected_path)
assert resp.status_code == 200
@pytest.mark.parametrize("prefix", ["", "/a_prefix"])
def test_async_api_prefix(AsyncTestCoreClient, prefix):
api_settings = ApiSettings(
openapi_url=f"{prefix}/api",
docs_url=f"{prefix}/api.html",
)
api = StacApi(
settings=api_settings,
client=AsyncTestCoreClient(),
router=APIRouter(prefix=prefix),
)
with TestClient(api.app, base_url="http://stac.io") as client:
landing = client.get(f"{prefix}/")
assert landing.status_code == 200, landing.json()
service_doc = client.get(f"{prefix}/api.html")
assert service_doc.status_code == 200, service_doc.text
service_desc = client.get(f"{prefix}/api")
assert service_desc.status_code == 200, service_desc.json()
conformance = client.get(f"{prefix}/conformance")
assert conformance.status_code == 200, conformance.json()
collections = client.get(f"{prefix}/collections")
assert collections.status_code == 200, collections.json()
collection_id = collections.json()["collections"][0]["id"]
collection = client.get(f"{prefix}/collections/{collection_id}")
assert collection.status_code == 200, collection.json()
items = client.get(f"{prefix}/collections/{collection_id}/items")
assert items.status_code == 200, items.json()
item_id = items.json()["features"][0]["id"]
item = client.get(f"{prefix}/collections/{collection_id}/items/{item_id}")
assert item.status_code == 200, item.json()
link_tests = [
("root", "application/json", "/"),
("conformance", "application/json", "/conformance"),
("data", "application/json", "/collections"),
("search", "application/geo+json", "/search"),
("service-doc", "text/html", "/api.html"),
("service-desc", "application/vnd.oai.openapi+json;version=3.0", "/api"),
]
for rel_type, expected_media_type, expected_path in link_tests:
link = get_link(landing.json(), rel_type)
assert link is not None, f"Missing {rel_type} link in landing page"
assert link.get("type") == expected_media_type
link_path = urllib.parse.urlsplit(link.get("href")).path
assert link_path == prefix + expected_path
resp = client.get(prefix + expected_path)
assert resp.status_code == 200
@pytest.mark.parametrize("prefix", ["", "/a_prefix"])
def test_api_prefix_with_root_path(TestCoreClient, prefix):
api_settings = ApiSettings(
openapi_url=f"{prefix}/api", docs_url=f"{prefix}/api.html", root_path="/api/v1"
)
api = StacApi(
settings=api_settings,
client=TestCoreClient(),
router=APIRouter(prefix=prefix),
)
prefix = "/api/v1" + prefix
with TestClient(api.app, base_url="http://stac.io", root_path="/api/v1") as client:
landing = client.get(f"{prefix}/")
assert landing.status_code == 200, landing.json()
print(landing.text)
service_doc = client.get(f"{prefix}/api.html")
assert service_doc.status_code == 200, service_doc.text
service_desc = client.get(f"{prefix}/api")
assert service_desc.status_code == 200, service_desc.json()
conformance = client.get(f"{prefix}/conformance")
assert conformance.status_code == 200, conformance.json()
# NOTE: The collections/collection/items/item links do not have the prefix
# because they are created in the fixtures
collections = client.get(f"{prefix}/collections")
assert collections.status_code == 200, collections.json()
collection_id = collections.json()["collections"][0]["id"]
collection = client.get(f"{prefix}/collections/{collection_id}")
assert collection.status_code == 200, collection.json()
items = client.get(f"{prefix}/collections/{collection_id}/items")
assert items.status_code == 200, items.json()
item_id = items.json()["features"][0]["id"]
item = client.get(f"{prefix}/collections/{collection_id}/items/{item_id}")
assert item.status_code == 200, item.json()
link_tests = [
("root", "application/json", "/"),
("conformance", "application/json", "/conformance"),
("data", "application/json", "/collections"),
("search", "application/geo+json", "/search"),
("service-doc", "text/html", "/api.html"),
("service-desc", "application/vnd.oai.openapi+json;version=3.0", "/api"),
]
for rel_type, expected_media_type, expected_path in link_tests:
link = get_link(landing.json(), rel_type)
assert link is not None, f"Missing {rel_type} link in landing page"
assert link.get("type") == expected_media_type
link_path = urllib.parse.urlsplit(link.get("href")).path
assert link_path == prefix + expected_path
resp = client.get(prefix + expected_path)
assert resp.status_code == 200