Skip to content

Commit

Permalink
Fix ValueError when decoding non-unicode body (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
suda authored Nov 27, 2024
1 parent 451f6ca commit 8081e45
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
2 changes: 1 addition & 1 deletion scaleway_functions_python/local/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 15 additions & 1 deletion tests/test_local/test_event.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -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

0 comments on commit 8081e45

Please sign in to comment.