Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyverless"
version = "0.0.54"
version = "0.0.55"
authors = ["rperez <rperez@op2aim.io>"]
description="A mini-framework providing tools to help you make complex APIs with serverless"
readme="README.md"
Expand Down
15 changes: 9 additions & 6 deletions pyverless/api_gateway_handler/api_gateway_handler_standalone.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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,
}

Expand Down
35 changes: 28 additions & 7 deletions tests/api_gateway_handler/test_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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 = [
Expand Down
Loading