diff --git a/scaleway_functions_python/local/event.py b/scaleway_functions_python/local/event.py index d6a8840..1e98727 100644 --- a/scaleway_functions_python/local/event.py +++ b/scaleway_functions_python/local/event.py @@ -41,6 +41,6 @@ def format_http_event(request: "Request") -> "Event": try: b64decode(body, validate=True).decode("utf-8") event["isBase64Encoded"] = True - except (binascii.Error, UnicodeDecodeError): + except (binascii.Error, UnicodeDecodeError, ValueError): pass return event diff --git a/tests/test_local/test_event.py b/tests/test_local/test_event.py index 37af42e..06137ff 100644 --- a/tests/test_local/test_event.py +++ b/tests/test_local/test_event.py @@ -1,5 +1,6 @@ import pytest -from flask import Flask, request +from flask import Flask, Request, request +from werkzeug.test import EnvironBuilder from scaleway_functions_python.local.event import format_http_event @@ -48,3 +49,16 @@ def test_format_http_event(app): assert event["requestContext"] == expected_request_context assert event["body"] == "" + +def test_format_http_event_with_non_unicode_body(): + # Create a request with non-unicode body + non_unicode_body = b'\xff\xfe\xfd' # Invalid UTF-8 sequence + builder = EnvironBuilder(method='POST', data=non_unicode_body) + r = Request(builder.get_environ()) + + # Call the function and check the result + event = format_http_event(r) + + assert event is not None + assert 'body' in event + assert event.get('isBase64Encoded', False) is False