forked from tomwojcik/starlette-context
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ApiKeyPlugin to extract X-API-Key header (tomwojcik#26)
`X-API-Key` is the header specified by [AWS API Gateway](https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-api-key-source.html), and also detailed in [Swagger documentation](https://swagger.io/docs/specification/authentication/api-keys/), to send an API key.
- Loading branch information
1 parent
a935fbf
commit 0022215
Showing
6 changed files
with
60 additions
and
2 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
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,6 @@ | ||
from starlette_context.header_keys import HeaderKeys | ||
from starlette_context.plugins.base import Plugin | ||
|
||
|
||
class ApiKeyPlugin(Plugin): | ||
key = HeaderKeys.api_key |
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,42 @@ | ||
from starlette import status | ||
from starlette.applications import Starlette | ||
from starlette.middleware import Middleware | ||
from starlette.requests import Request | ||
from starlette.responses import JSONResponse, Response | ||
from starlette.testclient import TestClient | ||
|
||
from starlette_context import plugins | ||
from starlette_context.header_keys import HeaderKeys | ||
from starlette_context.middleware import ContextMiddleware | ||
from tests.conftest import dummy_api_key | ||
|
||
middleware = [ | ||
Middleware( | ||
ContextMiddleware, | ||
plugins=(plugins.ApiKeyPlugin(),), | ||
) | ||
] | ||
app = Starlette(middleware=middleware) | ||
client = TestClient(app) | ||
headers = {HeaderKeys.api_key: dummy_api_key} | ||
|
||
|
||
@app.route("/") | ||
async def index(request: Request) -> Response: | ||
return JSONResponse( | ||
{"headers": str(request.headers.get(HeaderKeys.api_key))} | ||
) | ||
|
||
|
||
def test_valid_request_returns_proper_response(): | ||
response = client.get("/", headers=headers) | ||
assert response.status_code == status.HTTP_200_OK | ||
assert dummy_api_key in response.text | ||
assert HeaderKeys.api_key not in response.text | ||
|
||
|
||
def test_missing_forwarded_for_header(): | ||
response = client.get("/", headers={}) | ||
assert response.status_code == status.HTTP_200_OK | ||
assert dummy_api_key not in response.text | ||
assert HeaderKeys.api_key not in response.headers |