Skip to content

Commit

Permalink
[Response] Ensure body is a string when encoding is text (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
TomerShor authored Jun 8, 2023
1 parent aec8417 commit 3930947
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
13 changes: 10 additions & 3 deletions nuclio_sdk/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ def from_entrypoint_output(json_encoder, handler_output):
else:
response["body"] = handler_output

if isinstance(response["body"], bytes):
response["body"] = base64.b64encode(response["body"]).decode("ascii")
response["body_encoding"] = "base64"
Response._ensure_str_body(response)

return response

Expand All @@ -92,3 +90,12 @@ def empty_response():
"status_code": 200,
"body_encoding": "text",
}

@staticmethod
def _ensure_str_body(response):
if isinstance(response["body"], bytes):
response["body"] = base64.b64encode(response["body"]).decode("ascii")
response["body_encoding"] = "base64"

if response["body_encoding"] == "text":
response["body"] = str(response["body"])
9 changes: 7 additions & 2 deletions nuclio_sdk/test/test_response.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,12 @@ def test_str(self):

def test_int(self):
handler_return = 2020
expected_response = self._compile_output_response(body=2020)
expected_response = self._compile_output_response(body="2020")
self._validate_response(handler_return, expected_response)

def test_float(self):
handler_return = 12.34
expected_response = self._compile_output_response(body="12.34")
self._validate_response(handler_return, expected_response)

def test_bytes(self):
Expand All @@ -56,7 +61,7 @@ def test_iterable(self):

def test_datetime(self):
handler_return = datetime.datetime.now()
expected_response = self._compile_output_response(body=handler_return)
expected_response = self._compile_output_response(body=str(handler_return))
self._validate_response(handler_return, expected_response)

def test_status_code_and_str(self):
Expand Down

0 comments on commit 3930947

Please sign in to comment.