-
Notifications
You must be signed in to change notification settings - Fork 112
Fix/forwarding headers #415
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
Merged
geospatial-jeff
merged 9 commits into
stac-utils:master
from
captaincoordinates:fix/forwarding-headers
Aug 1, 2022
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e909b04
Respect forwarding headers in pgstac
captaincoordinates 95c2781
Respect forwarding headers in sqlalchemy
captaincoordinates d77370c
Linting fixes
captaincoordinates 47bfc85
Moved to proxy header middleware
captaincoordinates 3d00480
Merge branch 'master' into fix/forwarding-headers
geospatial-jeff bdaff3b
Merge branch 'master' into fix/forwarding-headers
geospatial-jeff 0e9c7ed
add makefile command to run api test cases
geospatial-jeff 9dbf355
bugfix, make method static
geospatial-jeff 998a02e
add unittests for proxy header middleware
geospatial-jeff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.