Skip to content

Commit 8a09873

Browse files
gadomskialukach
andauthored
fix: process links w/o the prefix (#70)
There's times when the underlying STAC API might not have a path on its root URL, but **stac-auth-proxy** has a path on its `upstream_url`; one such case is if there's an API Gateway in the way. This patch uses string replacement for link rewriting, rather than slicing, to handle these cases while preserving previous behavior. --------- Co-authored-by: Anthony Lukach <[email protected]>
1 parent d3d3769 commit 8a09873

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

src/stac_auth_proxy/middleware/ProcessLinksMiddleware.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,12 @@ def transform_json(self, data: dict[str, Any], request: Request) -> dict[str, An
5353
continue
5454

5555
# Remove the upstream_url path from the link if it exists
56-
if urlparse(self.upstream_url).path != "/":
56+
parsed_upstream_url = urlparse(self.upstream_url)
57+
if parsed_upstream_url.path != "/" and parsed_link.path.startswith(
58+
parsed_upstream_url.path
59+
):
5760
parsed_link = parsed_link._replace(
58-
path=parsed_link.path[len(urlparse(self.upstream_url).path) :]
61+
path=parsed_link.path[len(parsed_upstream_url.path) :]
5962
)
6063

6164
# Add the root_path to the link if it exists

tests/test_process_links.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,19 @@ def test_transform_json_nested_links(middleware, request_scope):
186186
transformed["collections"][0]["links"][1]["href"]
187187
== "http://proxy.example.com/proxy/collections/test-collection/items"
188188
)
189+
190+
191+
def test_transform_without_prefix(request_scope):
192+
"""Sometimes the upstream url will have a path, but the links won't."""
193+
middleware = ProcessLinksMiddleware(
194+
app=None, upstream_url="http://upstream.example.com/prod/", root_path=""
195+
)
196+
request = Request(request_scope)
197+
198+
data = {
199+
"links": [
200+
{"rel": "data", "href": "http://proxy.example.com/collections"},
201+
]
202+
}
203+
transformed = middleware.transform_json(data, request)
204+
assert transformed["links"][0]["href"] == "http://proxy.example.com/collections"

0 commit comments

Comments
 (0)