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
8 changes: 6 additions & 2 deletions mangum/handlers/aws_alb.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,13 @@ def request(self) -> Request:

@property
def body(self) -> bytes:
body = self.trigger_event.get("body", b"")
body = self.trigger_event.get("body", b"") or b""

if self.trigger_event.get("isBase64Encoded", False):
body = base64.b64decode(body)
return base64.b64decode(body)
if not isinstance(body, bytes):
body = body.encode()

return body

def transform_response(self, response: Response) -> Dict[str, Any]:
Expand Down
8 changes: 6 additions & 2 deletions mangum/handlers/aws_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,13 @@ def request(self) -> Request:

@property
def body(self) -> bytes:
body = self.trigger_event.get("body", b"")
body = self.trigger_event.get("body", b"") or b""

if self.trigger_event.get("isBase64Encoded", False):
body = base64.b64decode(body)
return base64.b64decode(body)
if not isinstance(body, bytes):
body = body.encode()

return body

def transform_response(self, response: Response) -> Dict[str, Any]:
Expand Down
8 changes: 6 additions & 2 deletions mangum/handlers/aws_cf_lambda_at_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ def request(self) -> Request:
@property
def body(self) -> bytes:
request = self.trigger_event["Records"][0]["cf"]["request"]
body = request.get("body", {}).get("data", None)
body = request.get("body", {}).get("data", None) or b""

if request.get("body", {}).get("encoding", "") == "base64":
body = base64.b64decode(body)
return base64.b64decode(body)
if not isinstance(body, bytes):
body = body.encode()

return body

def transform_response(self, response: Response) -> Dict[str, Any]:
Expand Down
8 changes: 6 additions & 2 deletions mangum/handlers/aws_http_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,9 +102,13 @@ def request(self) -> Request:

@property
def body(self) -> bytes:
body = self.trigger_event.get("body", b"")
body = self.trigger_event.get("body", b"") or b""

if self.trigger_event.get("isBase64Encoded", False):
body = base64.b64decode(body)
return base64.b64decode(body)
if not isinstance(body, bytes):
body = body.encode()

return body

def transform_response(self, response: Response) -> Dict[str, Any]:
Expand Down
17 changes: 15 additions & 2 deletions tests/handlers/test_aws_alb.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ def test_aws_alb_basic():

example_context = {}
handler = AwsAlb(example_event, example_context)

assert type(handler.body) == bytes
assert handler.request.scope == {
"asgi": {"version": "3.0"},
"aws.context": {},
Expand Down Expand Up @@ -119,7 +121,15 @@ def test_aws_alb_basic():
"query_string,scope_body",
[
("GET", "/hello/world", None, None, False, b"", None),
("POST", "/", {"name": ["me"]}, None, False, b"name=me", None),
(
"POST",
"/",
{"name": ["me"]},
"field1=value1&field2=value2",
False,
b"name=me",
b"field1=value1&field2=value2",
),
(
"GET",
"/my/resource",
Expand Down Expand Up @@ -210,7 +220,10 @@ def test_aws_alb_scope_real(
"type": "http",
}

assert handler.body == scope_body
if handler.body:
assert handler.body == scope_body
else:
assert handler.body == b""


def test_aws_alb_set_cookies() -> None:
Expand Down
17 changes: 15 additions & 2 deletions tests/handlers/test_aws_api_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ def test_aws_api_gateway_scope_basic():
}
example_context = {}
handler = AwsApiGateway(example_event, example_context)

assert type(handler.body) == bytes
assert handler.request.scope == {
"asgi": {"version": "3.0"},
"aws.context": {},
Expand Down Expand Up @@ -128,7 +130,15 @@ def test_aws_api_gateway_scope_basic():
"query_string,scope_body",
[
("GET", "/hello/world", None, None, False, b"", None),
("POST", "/", {"name": ["me"]}, None, False, b"name=me", None),
(
"POST",
"/",
{"name": ["me"]},
"field1=value1&field2=value2",
False,
b"name=me",
b"field1=value1&field2=value2",
),
(
"GET",
"/my/resource",
Expand Down Expand Up @@ -218,7 +228,10 @@ def test_aws_api_gateway_scope_real(
"type": "http",
}

assert handler.body == scope_body
if handler.body:
assert handler.body == scope_body
else:
assert handler.body == b""


@pytest.mark.parametrize(
Expand Down
16 changes: 14 additions & 2 deletions tests/handlers/test_aws_cf_lambda_at_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ def test_aws_cf_lambda_at_edge_scope_basic():
example_context = {}
handler = AwsCfLambdaAtEdge(example_event, example_context)

assert type(handler.body) == bytes
assert handler.request.scope == {
"asgi": {"version": "3.0"},
"aws.context": {},
Expand Down Expand Up @@ -169,7 +170,15 @@ def test_aws_cf_lambda_at_edge_scope_basic():
"body_base64_encoded,query_string,scope_body",
[
("GET", "/hello/world", None, None, False, b"", None),
("POST", "/", {"name": ["me"]}, None, False, b"name=me", None),
(
"POST",
"/",
{"name": ["me"]},
"field1=value1&field2=value2",
False,
b"name=me",
b"field1=value1&field2=value2",
),
(
"GET",
"/my/resource",
Expand Down Expand Up @@ -241,7 +250,10 @@ def test_aws_api_gateway_scope_real(
"type": "http",
}

assert handler.body == scope_body
if handler.body:
assert handler.body == scope_body
else:
assert handler.body == b""


@pytest.mark.parametrize(
Expand Down
14 changes: 12 additions & 2 deletions tests/handlers/test_aws_http_gateway.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ def test_aws_http_gateway_scope_basic_v1():
}
example_context = {}
handler = AwsHttpGateway(example_event, example_context)

assert type(handler.body) == bytes
assert handler.request.scope == {
"asgi": {"version": "3.0"},
"aws.context": {},
Expand Down Expand Up @@ -297,6 +299,8 @@ def test_aws_http_gateway_scope_basic_v2():
}
example_context = {}
handler = AwsHttpGateway(example_event, example_context)

assert type(handler.body) == bytes
assert handler.request.scope == {
"asgi": {"version": "3.0"},
"aws.context": {},
Expand Down Expand Up @@ -396,7 +400,10 @@ def test_aws_http_gateway_scope_real_v1(
"type": "http",
}

assert handler.body == scope_body
if handler.body:
assert handler.body == scope_body
else:
assert handler.body == b""


@pytest.mark.parametrize(
Expand Down Expand Up @@ -461,7 +468,10 @@ def test_aws_http_gateway_scope_real_v2(
"type": "http",
}

assert handler.body == scope_body
if handler.body:
assert handler.body == scope_body
else:
assert handler.body == b""


@pytest.mark.parametrize(
Expand Down