Skip to content
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

fix: LowerCaseQueryStringMiddleware should not truncate query parameters #677

Merged
merged 3 commits into from
Aug 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Release Notes

## [unreleased]

- fix `LowerCaseQueryStringMiddleware` unexpectedly truncating query parameters (https://github.com/developmentseed/titiler/pull/677)

## 0.13.0 (2023-07-27)

* update core requirements to libraries using pydantic **~=2.0**
Expand Down
23 changes: 23 additions & 0 deletions src/titiler/core/tests/test_case_middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,26 @@ async def route1(value: Annotated[List[str], Query()]):

response = client.get("/route1?VALUE=lorenzori&VALUE=dogs&value=trucks")
assert response.json() == {"value": ["lorenzori", "dogs", "trucks"]}


def test_lowercase_middleware_url_with_query_parameters():
"""Make sure all query parameters return."""
app = FastAPI()

@app.get("/route1")
async def route1(url: List[str] = Query(...)):
"""route1."""
return {"url": url}

app.add_middleware(LowerCaseQueryStringMiddleware)

client = TestClient(app)

url = "https://developmentseed.org?solutions=geospatial&planet=better"
url_encoded = (
"https%3A%2F%2Fdevelopmentseed.org%3Fsolutions%3Dgeospatial%26planet%3Dbetter"
)

response = client.get(f"/route1?url={url_encoded}")

assert response.json() == {"url": [url]}
3 changes: 2 additions & 1 deletion src/titiler/core/titiler/core/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging
import re
import time
import urllib.parse
from typing import Optional, Set

from fastapi.logger import logger
Expand Down Expand Up @@ -160,7 +161,7 @@ async def __call__(self, scope: Scope, receive: Receive, send: Send):

query_string = ""
for k, v in request.query_params.multi_items():
query_string += k.lower() + "=" + v + "&"
query_string += k.lower() + "=" + urllib.parse.quote(v) + "&"

query_string = query_string[:-1]
request.scope["query_string"] = query_string.encode(DECODE_FORMAT)
Expand Down