Skip to content

Commit

Permalink
Enable binary support by default for ALB events (#528)
Browse files Browse the repository at this point in the history
Fixes #513.
  • Loading branch information
adamchainz authored Oct 7, 2024
1 parent bbafe4b commit facd679
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Changelog

Thanks to Zoe Guillen for the report in `PR #496 <https://github.com/adamchainz/apig-wsgi/pull/496>`__.

* Enable binary support by default for ALB events.

Thanks to Oliver Ford for the report in `Issue #513 <https://github.com/adamchainz/apig-wsgi/issues/513>`__.

* Treat the content-type header "application/problem+json" as non binary by default.

Thanks to Ido Savion in `PR #503 <https://github.com/adamchainz/apig-wsgi/pull/503>`__.
Expand Down
14 changes: 12 additions & 2 deletions src/apig_wsgi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,23 @@ def handler(event: dict[str, Any], context: Any) -> dict[str, Any]:
version = event.get("version", "1.0")

if version in ("1.0", "alb"):
# Binary support deafults 'off' on version 1
event_binary_support = binary_support or False
environ = get_environ_v1(
event,
context,
encode_query_params=(version == "1.0"),
)
if version == "1.0":
# Binary support defaults to 'off' on version 1
if binary_support is None:
event_binary_support = False
else:
event_binary_support = binary_support
else:
# Binary support defaults to 'on' on ALBs
if binary_support is None:
event_binary_support = True
else:
event_binary_support = binary_support
response: BaseResponse = V1Response(
binary_support=event_binary_support,
non_binary_content_type_prefixes=non_binary_prefixes_tuple,
Expand Down
28 changes: 28 additions & 0 deletions tests/test_apig_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,34 @@ def test_get_binary_support_binary(self, simple_app: App) -> None:
"body": b64encode(b"\x13\x37").decode("utf-8"),
}

def test_get_binary_support_alb_default(self, simple_app: App) -> None:
simple_app.handler = make_lambda_handler(simple_app)
simple_app.headers = [("Content-Type", "application/octet-stream")]
simple_app.response = b"\x13\x37"

response = simple_app.handler(make_alb_event(), None)

assert response == {
"statusCode": 200,
"multiValueHeaders": {"Content-Type": ["application/octet-stream"]},
"isBase64Encoded": True,
"body": b64encode(b"\x13\x37").decode("utf-8"),
}

def test_get_binary_support_alb_enabled(self, simple_app: App) -> None:
simple_app.handler = make_lambda_handler(simple_app, binary_support=True)
simple_app.headers = [("Content-Type", "application/octet-stream")]
simple_app.response = b"\x13\x37"

response = simple_app.handler(make_alb_event(), None)

assert response == {
"statusCode": 200,
"multiValueHeaders": {"Content-Type": ["application/octet-stream"]},
"isBase64Encoded": True,
"body": b64encode(b"\x13\x37").decode("utf-8"),
}

@parametrize_default_text_content_type
def test_get_binary_support_binary_default_text_with_gzip_content_encoding(
self, simple_app: App, text_content_type: str
Expand Down

0 comments on commit facd679

Please sign in to comment.