-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into router_prefix
- Loading branch information
Showing
13 changed files
with
616 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -129,4 +129,7 @@ docs/api/* | |
.envrc | ||
|
||
# Virtualenv | ||
venv | ||
venv | ||
|
||
# IDE | ||
.vscode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
import pytest | ||
from starlette.applications import Starlette | ||
|
||
from stac_fastapi.api.middleware import ProxyHeaderMiddleware | ||
|
||
|
||
@pytest.fixture | ||
def proxy_header_middleware() -> ProxyHeaderMiddleware: | ||
app = Starlette() | ||
return ProxyHeaderMiddleware(app) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"headers,key,expected", | ||
[ | ||
([(b"host", b"testserver")], "host", "testserver"), | ||
([(b"host", b"testserver")], "user-agent", None), | ||
( | ||
[(b"host", b"testserver"), (b"accept-encoding", b"gzip, deflate, br")], | ||
"accept-encoding", | ||
"gzip, deflate, br", | ||
), | ||
], | ||
) | ||
def test_get_header_value_by_name( | ||
proxy_header_middleware: ProxyHeaderMiddleware, headers, key, expected | ||
): | ||
scope = {"headers": headers} | ||
actual = proxy_header_middleware._get_header_value_by_name(scope, key) | ||
assert actual == expected | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"headers,key,value", | ||
[ | ||
([(b"host", b"testserver")], "host", "another-server"), | ||
([(b"host", b"testserver")], "user-agent", "agent"), | ||
( | ||
[(b"host", b"testserver"), (b"accept-encoding", b"gzip, deflate, br")], | ||
"accept-encoding", | ||
"deflate", | ||
), | ||
], | ||
) | ||
def test_replace_header_value_by_name( | ||
proxy_header_middleware: ProxyHeaderMiddleware, headers, key, value | ||
): | ||
scope = {"headers": headers} | ||
updated_headers = proxy_header_middleware._replace_header_value_by_name( | ||
scope, key, value | ||
) | ||
|
||
header_value = proxy_header_middleware._get_header_value_by_name( | ||
{"headers": updated_headers}, key | ||
) | ||
assert header_value == value | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"scope,expected", | ||
[ | ||
( | ||
{"scheme": "https", "server": ["testserver", 80], "headers": []}, | ||
("https", "testserver", 80), | ||
), | ||
( | ||
{ | ||
"scheme": "http", | ||
"server": ["testserver", 80], | ||
"headers": [(b"host", b"testserver:81")], | ||
}, | ||
("http", "testserver", 81), | ||
), | ||
( | ||
{ | ||
"scheme": "http", | ||
"server": ["testserver", 80], | ||
"headers": [(b"host", b"testserver")], | ||
}, | ||
("http", "testserver", None), | ||
), | ||
( | ||
{ | ||
"scheme": "http", | ||
"server": ["testserver", 80], | ||
"headers": [(b"forwarded", b"proto=https;host=test:1234")], | ||
}, | ||
("https", "test", 1234), | ||
), | ||
( | ||
{ | ||
"scheme": "http", | ||
"server": ["testserver", 80], | ||
"headers": [(b"forwarded", b"proto=https;host=test:not-an-integer")], | ||
}, | ||
("https", "test", 80), | ||
), | ||
( | ||
{ | ||
"scheme": "http", | ||
"server": ["testserver", 80], | ||
"headers": [(b"x-forwarded-proto", b"https")], | ||
}, | ||
("https", "testserver", 80), | ||
), | ||
( | ||
{ | ||
"scheme": "http", | ||
"server": ["testserver", 80], | ||
"headers": [(b"x-forwarded-port", b"1111")], | ||
}, | ||
("http", "testserver", 1111), | ||
), | ||
( | ||
{ | ||
"scheme": "http", | ||
"server": ["testserver", 80], | ||
"headers": [(b"x-forwarded-port", b"not-an-integer")], | ||
}, | ||
("http", "testserver", 80), | ||
), | ||
( | ||
{ | ||
"scheme": "http", | ||
"server": ["testserver", 80], | ||
"headers": [ | ||
(b"forwarded", b"proto=https;host=test:1234"), | ||
(b"x-forwarded-port", b"1111"), | ||
(b"x-forwarded-proto", b"https"), | ||
], | ||
}, | ||
("https", "test", 1234), | ||
), | ||
], | ||
) | ||
def test_get_forwarded_url_parts( | ||
proxy_header_middleware: ProxyHeaderMiddleware, scope, expected | ||
): | ||
actual = proxy_header_middleware._get_forwarded_url_parts(scope) | ||
assert actual == expected |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.