Skip to content

Commit b6cbc81

Browse files
committed
fix: process links w/o the prefix
1 parent d3d3769 commit b6cbc81

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

src/stac_auth_proxy/middleware/ProcessLinksMiddleware.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ def transform_json(self, data: dict[str, Any], request: Request) -> dict[str, An
5555
# Remove the upstream_url path from the link if it exists
5656
if urlparse(self.upstream_url).path != "/":
5757
parsed_link = parsed_link._replace(
58-
path=parsed_link.path[len(urlparse(self.upstream_url).path) :]
58+
path=str(parsed_link.path).replace(
59+
str(urlparse(self.upstream_url).path), ""
60+
)
5961
)
6062

6163
# 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)