Skip to content

Commit

Permalink
remove middleware stack building to avoid conflict with exception han…
Browse files Browse the repository at this point in the history
…dler (#721)

* remove middleware stack building to avoid conflict with exception handler

* update changelog
  • Loading branch information
vincentsarago authored Jun 27, 2024
1 parent f311dc6 commit 4f410eb
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
### Removed

* Removed the Filter Extension depenency from `AggregationExtensionPostRequest` and `AggregationExtensionGetRequest` [#716](https://github.com/stac-utils/stac-fastapi/pull/716)
* Removed `add_middleware` method in `StacApi` object and let starlette handle the middleware stack creation [721](https://github.com/stac-utils/stac-fastapi/pull/721)

## [3.0.0a3] - 2024-06-13

Expand Down
7 changes: 1 addition & 6 deletions stac_fastapi/api/stac_fastapi/api/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -439,11 +439,6 @@ def add_route_dependencies(
"""
return add_route_dependencies(self.app.router.routes, scopes, dependencies)

def add_middleware(self, middleware: Middleware):
"""Add a middleware class to the application."""
self.app.user_middleware.insert(0, middleware)
self.app.middleware_stack = self.app.build_middleware_stack()

def __attrs_post_init__(self):
"""Post-init hook.
Expand Down Expand Up @@ -484,7 +479,7 @@ def __attrs_post_init__(self):

# add middlewares
for middleware in self.middlewares:
self.add_middleware(middleware)
self.app.user_middleware.insert(0, middleware)

# customize route dependencies
for scopes, dependencies in self.route_dependencies:
Expand Down
30 changes: 30 additions & 0 deletions stac_fastapi/api/tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
from unittest import mock

import pytest
from fastapi import Request
from fastapi.responses import JSONResponse
from starlette.applications import Starlette
from starlette.testclient import TestClient

Expand Down Expand Up @@ -166,3 +168,31 @@ def test_cors_middleware(test_client):
resp = test_client.get("/_mgmt/ping", headers={"Origin": "http://netloc"})
assert resp.status_code == 200
assert resp.headers["access-control-allow-origin"] == "*"


def test_middleware_stack():
stac_api = StacApi(
settings=ApiSettings(), client=mock.create_autospec(BaseCoreClient)
)

def exception_handler(request: Request, exc: Exception) -> JSONResponse:
return JSONResponse(
status_code=400,
content={"customerrordetail": "yoo", "body": "yo"},
)

class CustomException(Exception):
"Custom Exception"

pass

stac_api.app.add_exception_handler(CustomException, exception_handler)

@stac_api.app.get("/error")
def error_endpoint():
raise CustomException("got you!")

with TestClient(stac_api.app) as client:
resp = client.get("/error")
assert resp.status_code == 400
assert resp.json()["customerrordetail"] == "yoo"

0 comments on commit 4f410eb

Please sign in to comment.