diff --git a/pyproject.toml b/pyproject.toml index 9a59a09..29bf076 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "pyverless" -version = "0.0.54" +version = "0.0.55" authors = ["rperez "] description="A mini-framework providing tools to help you make complex APIs with serverless" readme="README.md" diff --git a/pyverless/api_gateway_handler/api_gateway_handler_standalone.py b/pyverless/api_gateway_handler/api_gateway_handler_standalone.py index be96215..deeb47f 100644 --- a/pyverless/api_gateway_handler/api_gateway_handler_standalone.py +++ b/pyverless/api_gateway_handler/api_gateway_handler_standalone.py @@ -2,12 +2,9 @@ from abc import ABC from typing import Dict -from pyverless.config import settings - from pyverless.api_gateway_handler.api_gateway_handler import ( - ApiGatewayHandler, - ApiGatewayWSHandler, -) + ApiGatewayHandler, ApiGatewayWSHandler) +from pyverless.config import settings class ApiGatewayHandlerStandalone(ApiGatewayHandler, ABC): @@ -21,9 +18,15 @@ def render_response(self): } if self.headers: headers = {**headers, **self.headers} + + content_type = headers.get("Content-Type", "application/json") return { "statusCode": self.response.status_code, - "body": json.dumps(self.response.body), + "body": ( + json.dumps(self.response.body) + if content_type == "application/json" + else self.response.body + ), "headers": headers, } diff --git a/tests/api_gateway_handler/test_handler.py b/tests/api_gateway_handler/test_handler.py index 14567af..6aaab94 100644 --- a/tests/api_gateway_handler/test_handler.py +++ b/tests/api_gateway_handler/test_handler.py @@ -2,14 +2,10 @@ from pyverless.api_gateway_handler.api_gateway_handler import ErrorHandler from pyverless.api_gateway_handler.api_gateway_handler_standalone import ( - ApiGatewayHandlerStandalone, - ApiGatewayWSHandlerStandalone, -) + ApiGatewayHandlerStandalone, ApiGatewayWSHandlerStandalone) from tests.utils.aws_events_creations import ( - create_api_gateway_event, - create_api_gateway_websocket_event, - create_lambda_context, -) + create_api_gateway_event, create_api_gateway_websocket_event, + create_lambda_context) class TestApiGatewayHandlerStandalone(unittest.TestCase): @@ -38,6 +34,31 @@ def perform_action(self): }, ) + def test_handler_ok_text_plain_response(self): + class TestHandler(ApiGatewayHandlerStandalone): + headers = {"Content-Type": "text/plain"} + + def perform_action(self): + return "hola" + + handler = TestHandler.as_handler() + output = handler( + create_api_gateway_event(path="test", method="GET"), create_lambda_context() + ) + self.assertEqual( + output, + { + "body": "hola", + "headers": { + "Content-Type": "text/plain", + "Access-Control-Allow-Headers": "*", + "Access-Control-Allow-Methods": "*", + "Access-Control-Allow-Origin": "*", + }, + "statusCode": 200, + }, + ) + def test_handler_controlled_error_response(self): class TestHandler(ApiGatewayHandlerStandalone): error_handlers = [