File tree Expand file tree Collapse file tree 3 files changed +60
-3
lines changed Expand file tree Collapse file tree 3 files changed +60
-3
lines changed Original file line number Diff line number Diff line change 1414from starlette .responses import JSONResponse , Response
1515
1616from stac_fastapi .api .errors import DEFAULT_STATUS_CODES , add_exception_handlers
17- from stac_fastapi .api .middleware import ProxyHeaderMiddleware
17+ from stac_fastapi .api .middleware import CORSMiddleware , ProxyHeaderMiddleware
1818from stac_fastapi .api .models import (
1919 APIRequest ,
2020 CollectionUri ,
@@ -93,7 +93,9 @@ class StacApi:
9393 pagination_extension = attr .ib (default = TokenPaginationExtension )
9494 response_class : Type [Response ] = attr .ib (default = JSONResponse )
9595 middlewares : List = attr .ib (
96- default = attr .Factory (lambda : [BrotliMiddleware , ProxyHeaderMiddleware ])
96+ default = attr .Factory (
97+ lambda : [BrotliMiddleware , CORSMiddleware , ProxyHeaderMiddleware ]
98+ )
9799 )
98100 route_dependencies : List [Tuple [List [Scope ], List [Depends ]]] = attr .ib (default = [])
99101
Original file line number Diff line number Diff line change 11"""api middleware."""
2-
32import re
3+ import typing
44from http .client import HTTP_PORT , HTTPS_PORT
55from typing import List , Tuple
66
7+ from starlette .middleware .cors import CORSMiddleware as _CORSMiddleware
78from starlette .types import ASGIApp , Receive , Scope , Send
89
910
11+ class CORSMiddleware (_CORSMiddleware ):
12+ """
13+ Subclass of Starlette's standard CORS middleware with default values set to those reccomended by the STAC API spec.
14+
15+ https://github.com/radiantearth/stac-api-spec/blob/914cf8108302e2ec734340080a45aaae4859bb63/implementation.md#cors
16+ """
17+
18+ def __init__ (
19+ self ,
20+ app : ASGIApp ,
21+ allow_origins : typing .Sequence [str ] = ("*" ,),
22+ allow_methods : typing .Sequence [str ] = (
23+ "OPTIONS" ,
24+ "POST" ,
25+ "GET" ,
26+ ),
27+ allow_headers : typing .Sequence [str ] = ("Content-Type" ,),
28+ allow_credentials : bool = False ,
29+ allow_origin_regex : typing .Optional [str ] = None ,
30+ expose_headers : typing .Sequence [str ] = (),
31+ max_age : int = 600 ,
32+ ) -> None :
33+ """Create CORS middleware."""
34+ super ().__init__ (
35+ app ,
36+ allow_origins ,
37+ allow_methods ,
38+ allow_headers ,
39+ allow_credentials ,
40+ allow_origin_regex ,
41+ expose_headers ,
42+ max_age ,
43+ )
44+
45+
1046class ProxyHeaderMiddleware :
1147 """
1248 Account for forwarding headers when deriving base URL.
Original file line number Diff line number Diff line change 1+ from unittest import mock
2+
13import pytest
24from starlette .applications import Starlette
5+ from starlette .testclient import TestClient
36
7+ from stac_fastapi .api .app import StacApi
48from stac_fastapi .api .middleware import ProxyHeaderMiddleware
9+ from stac_fastapi .types .config import ApiSettings
10+ from stac_fastapi .types .core import BaseCoreClient
511
612
713@pytest .fixture
@@ -10,6 +16,13 @@ def proxy_header_middleware() -> ProxyHeaderMiddleware:
1016 return ProxyHeaderMiddleware (app )
1117
1218
19+ @pytest .fixture
20+ def test_client () -> TestClient :
21+ app = StacApi (settings = ApiSettings (), client = mock .create_autospec (BaseCoreClient ))
22+ with TestClient (app .app ) as client :
23+ yield client
24+
25+
1326@pytest .mark .parametrize (
1427 "headers,key,expected" ,
1528 [
@@ -138,3 +151,9 @@ def test_get_forwarded_url_parts(
138151):
139152 actual = proxy_header_middleware ._get_forwarded_url_parts (scope )
140153 assert actual == expected
154+
155+
156+ def test_cors_middleware (test_client ):
157+ resp = test_client .get ("/_mgmt/ping" , headers = {"Origin" : "http://netloc" })
158+ assert resp .status_code == 200
159+ assert resp .headers ["access-control-allow-origin" ] == "*"
You can’t perform that action at this time.
0 commit comments