|
| 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): # noqa: C901 |
| 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"{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.get(f"{prefix}/search", params={"limit": 1}) |
| 75 | + assert response.status_code == 200 |
| 76 | + assert response.json()["url"] == url_prefix + "/search?limit=1" |
| 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"] == "GET" |
| 82 | + assert "limit=1" in link["href"] |
| 83 | + assert link["href"].startswith(url_prefix) |
| 84 | + assert {"next", "previous", "root", "self"} == {link["rel"] for link in links} |
| 85 | + |
| 86 | + response = client.post(f"{prefix}/search", json={}) |
| 87 | + assert response.status_code == 200 |
| 88 | + assert response.json()["url"] == url_prefix + "/search" |
| 89 | + assert response.json()["base_url"].rstrip("/") == url_prefix |
| 90 | + links = response.json()["links"] |
| 91 | + for link in links: |
| 92 | + if link["rel"] in ["previous", "next"]: |
| 93 | + assert link["method"] == "POST" |
| 94 | + assert link["href"].startswith(url_prefix) |
| 95 | + assert {"next", "previous", "root", "self"} == {link["rel"] for link in links} |
| 96 | + |
| 97 | + response = client.get(f"{prefix}/collections") |
| 98 | + assert response.status_code == 200 |
| 99 | + assert response.json()["url"] == url_prefix + "/collections" |
| 100 | + assert response.json()["base_url"].rstrip("/") == url_prefix |
| 101 | + links = response.json()["links"] |
| 102 | + for link in links: |
| 103 | + if link["rel"] in ["previous", "next"]: |
| 104 | + assert link["method"] == "GET" |
| 105 | + assert link["href"].startswith(url_prefix) |
| 106 | + assert {"next", "previous", "root", "self"} == {link["rel"] for link in links} |
0 commit comments