Skip to content

Commit fe76aa4

Browse files
author
Michael Brewer
committed
chore: add text/html content types
1 parent 0239138 commit fe76aa4

File tree

3 files changed

+50
-47
lines changed

3 files changed

+50
-47
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,43 +32,46 @@ class ProxyEventType(Enum):
3232
class CORSConfig(object):
3333
"""CORS Config
3434
35-
3635
Examples
3736
--------
3837
3938
Simple cors example using the default permissive cors, not this should only be used during early prototyping
4039
41-
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
40+
```python
41+
from aws_lambda_powertools.event_handler.api_gateway import ApiGatewayResolver
4242
43-
app = ApiGatewayResolver()
43+
app = ApiGatewayResolver()
4444
45-
@app.get("/my/path", cors=True)
46-
def with_cors():
47-
return {"message": "Foo"}
45+
@app.get("/my/path", cors=True)
46+
def with_cors():
47+
return {"message": "Foo"}
48+
```
4849
4950
Using a custom CORSConfig where `with_cors` used the custom provided CORSConfig and `without_cors`
5051
do not include any cors headers.
5152
52-
from aws_lambda_powertools.event_handler.api_gateway import (
53-
ApiGatewayResolver, CORSConfig
54-
)
55-
56-
cors_config = CORSConfig(
57-
allow_origin="https://wwww.example.com/",
58-
expose_headers=["x-exposed-response-header"],
59-
allow_headers=["x-custom-request-header"],
60-
max_age=100,
61-
allow_credentials=True,
62-
)
63-
app = ApiGatewayResolver(cors=cors_config)
64-
65-
@app.get("/my/path")
66-
def with_cors():
67-
return {"message": "Foo"}
53+
```python
54+
from aws_lambda_powertools.event_handler.api_gateway import (
55+
ApiGatewayResolver, CORSConfig
56+
)
57+
58+
cors_config = CORSConfig(
59+
allow_origin="https://wwww.example.com/",
60+
expose_headers=["x-exposed-response-header"],
61+
allow_headers=["x-custom-request-header"],
62+
max_age=100,
63+
allow_credentials=True,
64+
)
65+
app = ApiGatewayResolver(cors=cors_config)
66+
67+
@app.get("/my/path")
68+
def with_cors():
69+
return {"message": "Foo"}
6870
69-
@app.get("/another-one", cors=False)
70-
def without_cors():
71-
return {"message": "Foo"}
71+
@app.get("/another-one", cors=False)
72+
def without_cors():
73+
return {"message": "Foo"}
74+
```
7275
"""
7376

7477
_REQUIRED_HEADERS = ["Authorization", "Content-Type", "X-Amz-Date", "X-Api-Key", "X-Amz-Security-Token"]
@@ -512,7 +515,7 @@ def _call_route(self, route: Route, args: Dict[str, str]) -> ResponseBuilder:
512515
return ResponseBuilder(
513516
Response(
514517
status_code=500,
515-
content_type="text/plain",
518+
content_type=content_types.TEXT_PLAIN,
516519
body="".join(traceback.format_exc()),
517520
)
518521
)
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# use mimetypes library to be certain, e.g., mimetypes.types_map[".json"]
22

33
APPLICATION_JSON = "application/json"
4-
PLAIN_TEXT = "text/plain"
4+
TEXT_PLAIN = "text/plain"
5+
TEXT_HTML = "text/html"

tests/functional/event_handler/test_api_gateway.py

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ def read_media(file_name: str) -> bytes:
3434

3535

3636
LOAD_GW_EVENT = load_event("apiGatewayProxyEvent.json")
37-
TEXT_HTML = "text/html"
3837

3938

4039
def test_alb_event():
@@ -45,15 +44,15 @@ def test_alb_event():
4544
def foo():
4645
assert isinstance(app.current_event, ALBEvent)
4746
assert app.lambda_context == {}
48-
return Response(200, TEXT_HTML, "foo")
47+
return Response(200, content_types.TEXT_HTML, "foo")
4948

5049
# WHEN calling the event handler
5150
result = app(load_event("albEvent.json"), {})
5251

5352
# THEN process event correctly
5453
# AND set the current_event type as ALBEvent
5554
assert result["statusCode"] == 200
56-
assert result["headers"]["Content-Type"] == TEXT_HTML
55+
assert result["headers"]["Content-Type"] == content_types.TEXT_HTML
5756
assert result["body"] == "foo"
5857

5958

@@ -83,15 +82,15 @@ def test_api_gateway():
8382
@app.get("/my/path")
8483
def get_lambda() -> Response:
8584
assert isinstance(app.current_event, APIGatewayProxyEvent)
86-
return Response(200, TEXT_HTML, "foo")
85+
return Response(200, content_types.TEXT_HTML, "foo")
8786

8887
# WHEN calling the event handler
8988
result = app(LOAD_GW_EVENT, {})
9089

9190
# THEN process event correctly
9291
# AND set the current_event type as APIGatewayProxyEvent
9392
assert result["statusCode"] == 200
94-
assert result["headers"]["Content-Type"] == TEXT_HTML
93+
assert result["headers"]["Content-Type"] == content_types.TEXT_HTML
9594
assert result["body"] == "foo"
9695

9796

@@ -103,15 +102,15 @@ def test_api_gateway_v2():
103102
def my_path() -> Response:
104103
assert isinstance(app.current_event, APIGatewayProxyEventV2)
105104
post_data = app.current_event.json_body
106-
return Response(200, content_types.PLAIN_TEXT, post_data["username"])
105+
return Response(200, content_types.TEXT_PLAIN, post_data["username"])
107106

108107
# WHEN calling the event handler
109108
result = app(load_event("apiGatewayProxyV2Event.json"), {})
110109

111110
# THEN process event correctly
112111
# AND set the current_event type as APIGatewayProxyEventV2
113112
assert result["statusCode"] == 200
114-
assert result["headers"]["Content-Type"] == content_types.PLAIN_TEXT
113+
assert result["headers"]["Content-Type"] == content_types.TEXT_PLAIN
115114
assert result["body"] == "tom"
116115

117116

@@ -122,14 +121,14 @@ def test_include_rule_matching():
122121
@app.get("/<name>/<my_id>")
123122
def get_lambda(my_id: str, name: str) -> Response:
124123
assert name == "my"
125-
return Response(200, TEXT_HTML, my_id)
124+
return Response(200, content_types.TEXT_HTML, my_id)
126125

127126
# WHEN calling the event handler
128127
result = app(LOAD_GW_EVENT, {})
129128

130129
# THEN
131130
assert result["statusCode"] == 200
132-
assert result["headers"]["Content-Type"] == TEXT_HTML
131+
assert result["headers"]["Content-Type"] == content_types.TEXT_HTML
133132
assert result["body"] == "path"
134133

135134

@@ -190,11 +189,11 @@ def test_cors():
190189

191190
@app.get("/my/path", cors=True)
192191
def with_cors() -> Response:
193-
return Response(200, TEXT_HTML, "test")
192+
return Response(200, content_types.TEXT_HTML, "test")
194193

195194
@app.get("/without-cors")
196195
def without_cors() -> Response:
197-
return Response(200, TEXT_HTML, "test")
196+
return Response(200, content_types.TEXT_HTML, "test")
198197

199198
def handler(event, context):
200199
return app.resolve(event, context)
@@ -205,7 +204,7 @@ def handler(event, context):
205204
# THEN the headers should include cors headers
206205
assert "headers" in result
207206
headers = result["headers"]
208-
assert headers["Content-Type"] == TEXT_HTML
207+
assert headers["Content-Type"] == content_types.TEXT_HTML
209208
assert headers["Access-Control-Allow-Origin"] == "*"
210209
assert "Access-Control-Allow-Credentials" not in headers
211210
assert headers["Access-Control-Allow-Headers"] == ",".join(sorted(CORSConfig._REQUIRED_HEADERS))
@@ -271,7 +270,7 @@ def test_compress_no_accept_encoding():
271270

272271
@app.get("/my/path", compress=True)
273272
def return_text() -> Response:
274-
return Response(200, content_types.PLAIN_TEXT, expected_value)
273+
return Response(200, content_types.TEXT_PLAIN, expected_value)
275274

276275
# WHEN calling the event handler
277276
result = app({"path": "/my/path", "httpMethod": "GET", "headers": {}}, None)
@@ -287,7 +286,7 @@ def test_cache_control_200():
287286

288287
@app.get("/success", cache_control="max-age=600")
289288
def with_cache_control() -> Response:
290-
return Response(200, TEXT_HTML, "has 200 response")
289+
return Response(200, content_types.TEXT_HTML, "has 200 response")
291290

292291
def handler(event, context):
293292
return app.resolve(event, context)
@@ -298,7 +297,7 @@ def handler(event, context):
298297

299298
# THEN return the set Cache-Control
300299
headers = result["headers"]
301-
assert headers["Content-Type"] == TEXT_HTML
300+
assert headers["Content-Type"] == content_types.TEXT_HTML
302301
assert headers["Cache-Control"] == "max-age=600"
303302

304303

@@ -308,7 +307,7 @@ def test_cache_control_non_200():
308307

309308
@app.delete("/fails", cache_control="max-age=600")
310309
def with_cache_control_has_500() -> Response:
311-
return Response(503, TEXT_HTML, "has 503 response")
310+
return Response(503, content_types.TEXT_HTML, "has 503 response")
312311

313312
def handler(event, context):
314313
return app.resolve(event, context)
@@ -319,7 +318,7 @@ def handler(event, context):
319318

320319
# THEN return a Cache-Control of "no-cache"
321320
headers = result["headers"]
322-
assert headers["Content-Type"] == TEXT_HTML
321+
assert headers["Content-Type"] == content_types.TEXT_HTML
323322
assert headers["Cache-Control"] == "no-cache"
324323

325324

@@ -482,7 +481,7 @@ def test_custom_preflight_response():
482481
def custom_preflight():
483482
return Response(
484483
status_code=200,
485-
content_type=TEXT_HTML,
484+
content_type=content_types.TEXT_HTML,
486485
body="Foo",
487486
headers={"Access-Control-Allow-Methods": "CUSTOM"},
488487
)
@@ -498,7 +497,7 @@ def custom_method():
498497
assert result["statusCode"] == 200
499498
assert result["body"] == "Foo"
500499
headers = result["headers"]
501-
assert headers["Content-Type"] == TEXT_HTML
500+
assert headers["Content-Type"] == content_types.TEXT_HTML
502501
assert "Access-Control-Allow-Origin" in result["headers"]
503502
assert headers["Access-Control-Allow-Methods"] == "CUSTOM"
504503

@@ -522,7 +521,7 @@ def raises_error():
522521
assert result["statusCode"] == 500
523522
assert "Traceback (most recent call last)" in result["body"]
524523
headers = result["headers"]
525-
assert headers["Content-Type"] == "text/plain"
524+
assert headers["Content-Type"] == content_types.TEXT_PLAIN
526525

527526

528527
def test_unhandled_exceptions_debug_off():

0 commit comments

Comments
 (0)