-
Couldn't load subscription status.
- Fork 42
add paging links tests #195
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
23be4cb
32b3c6d
e413875
b1521b8
79c3cc6
f08c89d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -51,7 +51,11 @@ def base_url(self): | |
| @property | ||
| def url(self): | ||
| """Get the current request url.""" | ||
| return str(self.request.url) | ||
| url = urljoin(str(self.request.base_url), self.request.url.path.lstrip("/")) | ||
| if qs := self.request.url.query: | ||
| url += f"?{qs}" | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This feels over engineered but it's the only way I could find to make it works 🤷 |
||
|
|
||
| return url | ||
|
|
||
| def resolve(self, url): | ||
| """Resolve url to the current request url.""" | ||
|
|
@@ -143,7 +147,7 @@ def link_next(self) -> Optional[Dict[str, Any]]: | |
| "rel": Relations.next.value, | ||
| "type": MimeTypes.geojson.value, | ||
| "method": method, | ||
| "href": f"{self.request.url}", | ||
| "href": self.url, | ||
| "body": {**self.request.postbody, "token": f"next:{self.next}"}, | ||
| } | ||
|
|
||
|
|
@@ -167,7 +171,7 @@ def link_prev(self) -> Optional[Dict[str, Any]]: | |
| "rel": Relations.previous.value, | ||
| "type": MimeTypes.geojson.value, | ||
| "method": method, | ||
| "href": f"{self.request.url}", | ||
| "href": self.url, | ||
| "body": {**self.request.postbody, "token": f"prev:{self.prev}"}, | ||
| } | ||
| return None | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,106 @@ | ||||||||||
| import pytest | ||||||||||
| from fastapi import APIRouter, FastAPI | ||||||||||
| from starlette.requests import Request | ||||||||||
| from starlette.testclient import TestClient | ||||||||||
|
|
||||||||||
| from stac_fastapi.pgstac.models import links as app_links | ||||||||||
|
|
||||||||||
|
|
||||||||||
| @pytest.mark.parametrize("root_path", ["", "/api/v1"]) | ||||||||||
| @pytest.mark.parametrize("prefix", ["", "/stac"]) | ||||||||||
| def tests_app_links(prefix, root_path): # noqa: C901 | ||||||||||
| endpoint_prefix = root_path + prefix | ||||||||||
| url_prefix = "http://stac.io" + endpoint_prefix | ||||||||||
|
Comment on lines
+12
to
+13
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #201 , but this should also work:
Suggested change
we want to model situation when There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🤔 I'm not sure to get this to be honest In all cases, the client should be called There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I don't understand how you come to this conclusion from that one test alone. I'm just trying to replicate the scenario I see in the debugger of a real system. Omitting the root path in the call to the test client still routes to the endpoint in question, but this time with the request object behaving the same way as in the real system behind a reverse proxy that removes routing prefix. |
||||||||||
|
|
||||||||||
| app = FastAPI(root_path=root_path) | ||||||||||
| router = APIRouter(prefix=prefix) | ||||||||||
| app.state.router_prefix = router.prefix | ||||||||||
|
|
||||||||||
| @router.get("/search") | ||||||||||
| @router.post("/search") | ||||||||||
| async def search(request: Request): | ||||||||||
| links = app_links.PagingLinks(request, next="yo:2", prev="yo:1") | ||||||||||
| return { | ||||||||||
| "url": links.url, | ||||||||||
| "base_url": links.base_url, | ||||||||||
| "links": await links.get_links(), | ||||||||||
| } | ||||||||||
|
|
||||||||||
| @router.get("/collections") | ||||||||||
| async def collections(request: Request): | ||||||||||
| pgstac_next = { | ||||||||||
| "rel": "next", | ||||||||||
| "body": {"offset": 1}, | ||||||||||
| "href": "./collections", | ||||||||||
| "type": "application/json", | ||||||||||
| "merge": True, | ||||||||||
| "method": "GET", | ||||||||||
| } | ||||||||||
| pgstac_prev = { | ||||||||||
| "rel": "prev", | ||||||||||
| "body": {"offset": 0}, | ||||||||||
| "href": "./collections", | ||||||||||
| "type": "application/json", | ||||||||||
| "merge": True, | ||||||||||
| "method": "GET", | ||||||||||
| } | ||||||||||
| links = app_links.CollectionSearchPagingLinks( | ||||||||||
| request, next=pgstac_next, prev=pgstac_prev | ||||||||||
| ) | ||||||||||
| return { | ||||||||||
| "url": links.url, | ||||||||||
| "base_url": links.base_url, | ||||||||||
| "links": await links.get_links(), | ||||||||||
| } | ||||||||||
|
|
||||||||||
| app.include_router(router) | ||||||||||
|
|
||||||||||
| with TestClient( | ||||||||||
| app, | ||||||||||
| base_url="http://stac.io", | ||||||||||
| root_path=root_path, | ||||||||||
| ) as client: | ||||||||||
| response = client.get(f"{prefix}/search") | ||||||||||
| assert response.status_code == 200 | ||||||||||
| assert response.json()["url"] == url_prefix + "/search" | ||||||||||
| assert response.json()["base_url"].rstrip("/") == url_prefix | ||||||||||
| links = response.json()["links"] | ||||||||||
| for link in links: | ||||||||||
| if link["rel"] in ["previous", "next"]: | ||||||||||
| assert link["method"] == "GET" | ||||||||||
| assert link["href"].startswith(url_prefix) | ||||||||||
| assert {"next", "previous", "root", "self"} == {link["rel"] for link in links} | ||||||||||
|
|
||||||||||
| response = client.get(f"{prefix}/search", params={"limit": 1}) | ||||||||||
| assert response.status_code == 200 | ||||||||||
| assert response.json()["url"] == url_prefix + "/search?limit=1" | ||||||||||
| assert response.json()["base_url"].rstrip("/") == url_prefix | ||||||||||
| links = response.json()["links"] | ||||||||||
| for link in links: | ||||||||||
| if link["rel"] in ["previous", "next"]: | ||||||||||
| assert link["method"] == "GET" | ||||||||||
| assert "limit=1" in link["href"] | ||||||||||
| assert link["href"].startswith(url_prefix) | ||||||||||
| assert {"next", "previous", "root", "self"} == {link["rel"] for link in links} | ||||||||||
|
|
||||||||||
| response = client.post(f"{prefix}/search", json={}) | ||||||||||
| assert response.status_code == 200 | ||||||||||
| assert response.json()["url"] == url_prefix + "/search" | ||||||||||
| assert response.json()["base_url"].rstrip("/") == url_prefix | ||||||||||
| links = response.json()["links"] | ||||||||||
| for link in links: | ||||||||||
| if link["rel"] in ["previous", "next"]: | ||||||||||
| assert link["method"] == "POST" | ||||||||||
| assert link["href"].startswith(url_prefix) | ||||||||||
| assert {"next", "previous", "root", "self"} == {link["rel"] for link in links} | ||||||||||
|
|
||||||||||
| response = client.get(f"{prefix}/collections") | ||||||||||
| assert response.status_code == 200 | ||||||||||
| assert response.json()["url"] == url_prefix + "/collections" | ||||||||||
| assert response.json()["base_url"].rstrip("/") == url_prefix | ||||||||||
| links = response.json()["links"] | ||||||||||
| for link in links: | ||||||||||
| if link["rel"] in ["previous", "next"]: | ||||||||||
| assert link["method"] == "GET" | ||||||||||
| assert link["href"].startswith(url_prefix) | ||||||||||
| assert {"next", "previous", "root", "self"} == {link["rel"] for link in links} | ||||||||||
Uh oh!
There was an error while loading. Please reload this page.