Skip to content

Commit 4beaaaa

Browse files
author
Michael Brewer
committed
feat(api-gateway): print json event when debug is on
1 parent fe76aa4 commit 4beaaaa

File tree

3 files changed

+86
-67
lines changed

3 files changed

+86
-67
lines changed

aws_lambda_powertools/event_handler/api_gateway.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,8 @@ def resolve(self, event, context) -> Dict[str, Any]:
434434
dict
435435
Returns the dict response
436436
"""
437+
if self._debug:
438+
print(self._json_dump(event))
437439
self.current_event = self._to_proxy_event(event)
438440
self.lambda_context = context
439441
return self._resolve().build(self.current_event, self._cors)

tests/functional/event_handler/test_api_gateway.py

Lines changed: 84 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import base64
2+
import builtins
23
import json
34
import zlib
45
from decimal import Decimal
56
from pathlib import Path
67
from typing import Dict
8+
from unittest.mock import MagicMock
79

810
import pytest
911

@@ -502,73 +504,6 @@ def custom_method():
502504
assert headers["Access-Control-Allow-Methods"] == "CUSTOM"
503505

504506

505-
def test_unhandled_exceptions_debug_on():
506-
# GIVEN debug is enabled
507-
# AND an unhandled exception is raised
508-
app = ApiGatewayResolver(debug=True)
509-
assert app._debug
510-
511-
@app.get("/raises-error")
512-
def raises_error():
513-
raise RuntimeError("Foo")
514-
515-
# WHEN calling the handler
516-
result = app({"path": "/raises-error", "httpMethod": "GET"}, None)
517-
518-
# THEN return a 500
519-
# AND Content-Type is set to text/plain
520-
# AND include the exception traceback in the response
521-
assert result["statusCode"] == 500
522-
assert "Traceback (most recent call last)" in result["body"]
523-
headers = result["headers"]
524-
assert headers["Content-Type"] == content_types.TEXT_PLAIN
525-
526-
527-
def test_unhandled_exceptions_debug_off():
528-
# GIVEN debug is disabled
529-
# AND an unhandled exception is raised
530-
app = ApiGatewayResolver(debug=False)
531-
assert not app._debug
532-
533-
@app.get("/raises-error")
534-
def raises_error():
535-
raise RuntimeError("Foo")
536-
537-
# WHEN calling the handler
538-
# THEN raise the original exception
539-
with pytest.raises(RuntimeError) as e:
540-
app({"path": "/raises-error", "httpMethod": "GET"}, None)
541-
542-
# AND include the original error
543-
assert e.value.args == ("Foo",)
544-
545-
546-
def test_debug_mode_environment_variable(monkeypatch):
547-
# GIVEN a debug mode environment variable is set
548-
monkeypatch.setenv(constants.EVENT_HANDLER_DEBUG_ENV, "true")
549-
app = ApiGatewayResolver()
550-
551-
# WHEN calling app._debug
552-
# THEN the debug mode is enabled
553-
assert app._debug
554-
555-
556-
def test_debug_json_formatting():
557-
# GIVEN debug is True
558-
app = ApiGatewayResolver(debug=True)
559-
response = {"message": "Foo"}
560-
561-
@app.get("/foo")
562-
def foo():
563-
return response
564-
565-
# WHEN calling the handler
566-
result = app({"path": "/foo", "httpMethod": "GET"}, None)
567-
568-
# THEN return a pretty print json in the body
569-
assert result["body"] == json.dumps(response, indent=4)
570-
571-
572507
def test_service_error_responses():
573508
# SCENARIO handling different kind of service errors being raised
574509
app = ApiGatewayResolver(cors=CORSConfig())
@@ -651,3 +586,85 @@ def service_error():
651586
assert "Access-Control-Allow-Origin" in result["headers"]
652587
expected = {"statusCode": 502, "message": "Something went wrong!"}
653588
assert result["body"] == json_dump(expected)
589+
590+
591+
def test_debug_unhandled_exceptions_debug_on():
592+
# GIVEN debug is enabled
593+
# AND an unhandled exception is raised
594+
app = ApiGatewayResolver(debug=True)
595+
assert app._debug
596+
597+
@app.get("/raises-error")
598+
def raises_error():
599+
raise RuntimeError("Foo")
600+
601+
# WHEN calling the handler
602+
result = app({"path": "/raises-error", "httpMethod": "GET"}, None)
603+
604+
# THEN return a 500
605+
# AND Content-Type is set to text/plain
606+
# AND include the exception traceback in the response
607+
assert result["statusCode"] == 500
608+
assert "Traceback (most recent call last)" in result["body"]
609+
headers = result["headers"]
610+
assert headers["Content-Type"] == content_types.TEXT_PLAIN
611+
612+
613+
def test_debug_unhandled_exceptions_debug_off():
614+
# GIVEN debug is disabled
615+
# AND an unhandled exception is raised
616+
app = ApiGatewayResolver(debug=False)
617+
assert not app._debug
618+
619+
@app.get("/raises-error")
620+
def raises_error():
621+
raise RuntimeError("Foo")
622+
623+
# WHEN calling the handler
624+
# THEN raise the original exception
625+
with pytest.raises(RuntimeError) as e:
626+
app({"path": "/raises-error", "httpMethod": "GET"}, None)
627+
628+
# AND include the original error
629+
assert e.value.args == ("Foo",)
630+
631+
632+
def test_debug_mode_environment_variable(monkeypatch):
633+
# GIVEN a debug mode environment variable is set
634+
monkeypatch.setenv(constants.EVENT_HANDLER_DEBUG_ENV, "true")
635+
app = ApiGatewayResolver()
636+
637+
# WHEN calling app._debug
638+
# THEN the debug mode is enabled
639+
assert app._debug
640+
641+
642+
def test_debug_json_formatting():
643+
# GIVEN debug is True
644+
app = ApiGatewayResolver(debug=True)
645+
response = {"message": "Foo"}
646+
647+
@app.get("/foo")
648+
def foo():
649+
return response
650+
651+
# WHEN calling the handler
652+
result = app({"path": "/foo", "httpMethod": "GET"}, None)
653+
654+
# THEN return a pretty print json in the body
655+
assert result["body"] == json.dumps(response, indent=4)
656+
657+
658+
def test_debug_print_event(monkeypatch):
659+
# GIVE debug is True
660+
app = ApiGatewayResolver(debug=True)
661+
mocked_print = MagicMock()
662+
monkeypatch.setattr(builtins, "print", mocked_print)
663+
664+
# WHEN calling resolve
665+
event = {"path": "/foo", "httpMethod": "GET"}
666+
app(event, None)
667+
668+
# THEN print the event
669+
# NOTE: other calls might have happened outside of this mock
670+
mocked_print.assert_any_call(json.dumps(event, indent=4))

tests/functional/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)