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 14
14
from starlette .responses import JSONResponse , Response
15
15
16
16
from 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
18
18
from stac_fastapi .api .models import (
19
19
APIRequest ,
20
20
CollectionUri ,
@@ -93,7 +93,9 @@ class StacApi:
93
93
pagination_extension = attr .ib (default = TokenPaginationExtension )
94
94
response_class : Type [Response ] = attr .ib (default = JSONResponse )
95
95
middlewares : List = attr .ib (
96
- default = attr .Factory (lambda : [BrotliMiddleware , ProxyHeaderMiddleware ])
96
+ default = attr .Factory (
97
+ lambda : [BrotliMiddleware , CORSMiddleware , ProxyHeaderMiddleware ]
98
+ )
97
99
)
98
100
route_dependencies : List [Tuple [List [Scope ], List [Depends ]]] = attr .ib (default = [])
99
101
Original file line number Diff line number Diff line change 1
1
"""api middleware."""
2
-
3
2
import re
3
+ import typing
4
4
from http .client import HTTP_PORT , HTTPS_PORT
5
5
from typing import List , Tuple
6
6
7
+ from starlette .middleware .cors import CORSMiddleware as _CORSMiddleware
7
8
from starlette .types import ASGIApp , Receive , Scope , Send
8
9
9
10
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
+
10
46
class ProxyHeaderMiddleware :
11
47
"""
12
48
Account for forwarding headers when deriving base URL.
Original file line number Diff line number Diff line change
1
+ from unittest import mock
2
+
1
3
import pytest
2
4
from starlette .applications import Starlette
5
+ from starlette .testclient import TestClient
3
6
7
+ from stac_fastapi .api .app import StacApi
4
8
from stac_fastapi .api .middleware import ProxyHeaderMiddleware
9
+ from stac_fastapi .types .config import ApiSettings
10
+ from stac_fastapi .types .core import BaseCoreClient
5
11
6
12
7
13
@pytest .fixture
@@ -10,6 +16,13 @@ def proxy_header_middleware() -> ProxyHeaderMiddleware:
10
16
return ProxyHeaderMiddleware (app )
11
17
12
18
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
+
13
26
@pytest .mark .parametrize (
14
27
"headers,key,expected" ,
15
28
[
@@ -138,3 +151,9 @@ def test_get_forwarded_url_parts(
138
151
):
139
152
actual = proxy_header_middleware ._get_forwarded_url_parts (scope )
140
153
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