Skip to content

Commit

Permalink
Encoding handling, correct binary support
Browse files Browse the repository at this point in the history
  • Loading branch information
jordaneremieff committed Jan 30, 2019
1 parent 07ca0c9 commit d6dbbbf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 8 additions & 13 deletions mangum/__init__.py
Expand Up @@ -72,11 +72,10 @@ def on_response_start(self, headers: dict, status_code: int) -> None:
self.response["headers"] = {k.decode(): v.decode() for k, v in headers.items()}

def on_response_close(self) -> None:
body = self.body
if self.binary:
body = base64.b64encode(self.body)
else:
body = self.body.decode()
self.response["body"] = body
body = base64.b64encode(body)
self.response["body"] = body.decode()


class Mangum:
Expand Down Expand Up @@ -131,16 +130,12 @@ def asgi(self, event: dict, context: dict) -> dict:
}

binary = event.get("isBase64Encoded", False)
body = event["body"]

if body:
if binary:
body = base64.b64decode(body)
body = event["body"] or b""

else:
body = body.encode()
else:
body = b""
if binary:
body = base64.b64decode(body)
elif not isinstance(body, bytes):
body = body.encode()

response = ASGICycle(scope, binary=binary)(self.app, body=body)
return response
Expand Down
2 changes: 1 addition & 1 deletion tests/test_aws.py
Expand Up @@ -100,7 +100,7 @@ async def asgi(receive, send):
"statusCode": 200,
"isBase64Encoded": True,
"headers": {"content-length": "3", "content-type": "text/plain; charset=utf-8"},
"body": body_encoded,
"body": body_encoded.decode(),
}


Expand Down

0 comments on commit d6dbbbf

Please sign in to comment.