Skip to content

Commit

Permalink
feat(data-classes): decode base64 encoded body (#425)
Browse files Browse the repository at this point in the history
Co-authored-by: Heitor Lessa <heitor.lessa@hotmail.com>
  • Loading branch information
Michael Brewer and heitorlessa committed May 17, 2021
1 parent 22754d3 commit 77c1e40
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
9 changes: 9 additions & 0 deletions aws_lambda_powertools/utilities/data_classes/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import base64
import json
from typing import Any, Dict, Optional

Expand Down Expand Up @@ -66,6 +67,14 @@ def json_body(self) -> Any:
"""Parses the submitted body as json"""
return json.loads(self["body"])

@property
def decoded_body(self) -> str:
"""Dynamically base64 decode body as a str"""
body: str = self["body"]
if self.is_base64_encoded:
return base64.b64decode(body.encode()).decode()
return body

@property
def path(self) -> str:
return self["path"]
Expand Down
34 changes: 34 additions & 0 deletions tests/functional/test_data_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from secrets import compare_digest
from urllib.parse import quote_plus

import pytest
from pytest_mock import MockerFixture

from aws_lambda_powertools.utilities.data_classes import (
Expand Down Expand Up @@ -848,6 +849,39 @@ def test_base_proxy_event_get_header_value_case_insensitive():
assert value is None


def test_base_proxy_event_json_body_key_error():
event = BaseProxyEvent({})
with pytest.raises(KeyError) as ke:
assert not event.json_body
assert str(ke.value) == "'body'"


def test_base_proxy_event_json_body():
data = {"message": "Foo"}
event = BaseProxyEvent({"body": json.dumps(data)})
assert event.json_body == data


def test_base_proxy_event_decode_body_key_error():
event = BaseProxyEvent({})
with pytest.raises(KeyError) as ke:
assert not event.decoded_body
assert str(ke.value) == "'body'"


def test_base_proxy_event_decode_body_encoded_false():
data = "Foo"
event = BaseProxyEvent({"body": data, "isBase64Encoded": False})
assert event.decoded_body == data


def test_base_proxy_event_decode_body_encoded_true():
data = "Foo"
encoded_data = base64.b64encode(data.encode()).decode()
event = BaseProxyEvent({"body": encoded_data, "isBase64Encoded": True})
assert event.decoded_body == data


def test_kinesis_stream_event():
event = KinesisStreamEvent(load_event("kinesisStreamEvent.json"))

Expand Down

0 comments on commit 77c1e40

Please sign in to comment.