From f70c524fb0bdc8c5fef2223c82f5e339445bc5fa Mon Sep 17 00:00:00 2001 From: Miguel Grinberg Date: Sat, 18 Oct 2025 00:02:42 +0100 Subject: [PATCH] always encode ASGI response bodies to bytes --- src/microdot/asgi.py | 2 ++ tests/test_asgi.py | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/microdot/asgi.py b/src/microdot/asgi.py index caf5b47..2b1e88f 100644 --- a/src/microdot/asgi.py +++ b/src/microdot/asgi.py @@ -130,6 +130,8 @@ class Microdot(BaseMicrodot): try: while not cancelled: # pragma: no branch res_body = await body_iter.__anext__() + if isinstance(res_body, str): + res_body = res_body.encode() await send({'type': 'http.response.body', 'body': res_body, 'more_body': True}) diff --git a/tests/test_asgi.py b/tests/test_asgi.py index d2d2e04..80bed02 100644 --- a/tests/test_asgi.py +++ b/tests/test_asgi.py @@ -35,7 +35,7 @@ class TestASGI(unittest.TestCase): class R: def __init__(self): self.i = 0 - self.body = [b're', b'sp', b'on', b'se', b''] + self.body = [b're', b'sp', b'on', 'se', b''] async def read(self, n): data = self.body[self.i]