Skip to content

Commit 23be4cb

Browse files
committed
add paging links tests
1 parent 00e642e commit 23be4cb

File tree

2 files changed

+96
-2
lines changed

2 files changed

+96
-2
lines changed

stac_fastapi/pgstac/models/links.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ def link_next(self) -> Optional[Dict[str, Any]]:
143143
"rel": Relations.next,
144144
"type": MimeTypes.geojson,
145145
"method": method,
146-
"href": f"{self.request.url}",
146+
"href": str(self.request.url),
147147
"body": {**self.request.postbody, "token": f"next:{self.next}"},
148148
}
149149

@@ -167,7 +167,7 @@ def link_prev(self) -> Optional[Dict[str, Any]]:
167167
"rel": Relations.previous,
168168
"type": MimeTypes.geojson,
169169
"method": method,
170-
"href": f"{self.request.url}",
170+
"href": str(self.request.url),
171171
"body": {**self.request.postbody, "token": f"prev:{self.prev}"},
172172
}
173173
return None

tests/api/test_links.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import pytest
2+
from fastapi import APIRouter, FastAPI
3+
from starlette.requests import Request
4+
from starlette.testclient import TestClient
5+
6+
from stac_fastapi.pgstac.models import links as app_links
7+
8+
9+
@pytest.mark.parametrize("root_path", ["", "/api/v1"])
10+
@pytest.mark.parametrize("prefix", ["", "/stac"])
11+
def tests_app_links(prefix, root_path):
12+
endpoint_prefix = root_path + prefix
13+
url_prefix = "http://stac.io" + endpoint_prefix
14+
15+
app = FastAPI(root_path=root_path)
16+
router = APIRouter(prefix=prefix)
17+
app.state.router_prefix = router.prefix
18+
19+
@router.get("/search")
20+
@router.post("/search")
21+
async def search(request: Request):
22+
links = app_links.PagingLinks(request, next="yo:2", prev="yo:1")
23+
return {
24+
"url": links.url,
25+
"base_url": links.base_url,
26+
"links": await links.get_links(),
27+
}
28+
29+
@router.get("/collections")
30+
async def collections(request: Request):
31+
pgstac_next = {
32+
"rel": "next",
33+
"body": {"offset": 1},
34+
"href": "./collections",
35+
"type": "application/json",
36+
"merge": True,
37+
"method": "GET",
38+
}
39+
pgstac_prev = {
40+
"rel": "prev",
41+
"body": {"offset": 0},
42+
"href": "./collections",
43+
"type": "application/json",
44+
"merge": True,
45+
"method": "GET",
46+
}
47+
links = app_links.CollectionSearchPagingLinks(
48+
request, next=pgstac_next, prev=pgstac_prev
49+
)
50+
return {
51+
"url": links.url,
52+
"base_url": links.base_url,
53+
"links": await links.get_links(),
54+
}
55+
56+
app.include_router(router)
57+
58+
with TestClient(
59+
app,
60+
base_url="http://stac.io",
61+
root_path=root_path,
62+
) as client:
63+
response = client.get(f"{endpoint_prefix}/search")
64+
assert response.status_code == 200
65+
assert response.json()["url"] == url_prefix + "/search"
66+
assert response.json()["base_url"].rstrip("/") == url_prefix
67+
links = response.json()["links"]
68+
for link in links:
69+
if link["rel"] in ["previous", "next"]:
70+
assert link["method"] == "GET"
71+
assert link["href"].startswith(url_prefix)
72+
assert {"next", "previous", "root", "self"} == {link["rel"] for link in links}
73+
74+
response = client.post(f"{endpoint_prefix}/search", json={})
75+
assert response.status_code == 200
76+
assert response.json()["url"] == url_prefix + "/search"
77+
assert response.json()["base_url"].rstrip("/") == url_prefix
78+
links = response.json()["links"]
79+
for link in links:
80+
if link["rel"] in ["previous", "next"]:
81+
assert link["method"] == "POST"
82+
assert link["href"].startswith(url_prefix)
83+
assert {"next", "previous", "root", "self"} == {link["rel"] for link in links}
84+
85+
response = client.get(f"{endpoint_prefix}/collections")
86+
assert response.status_code == 200
87+
assert response.json()["url"] == url_prefix + "/collections"
88+
assert response.json()["base_url"].rstrip("/") == url_prefix
89+
links = response.json()["links"]
90+
for link in links:
91+
if link["rel"] in ["previous", "next"]:
92+
assert link["method"] == "GET"
93+
assert link["href"].startswith(url_prefix)
94+
assert {"next", "previous", "root", "self"} == {link["rel"] for link in links}

0 commit comments

Comments
 (0)