always encode ASGI response bodies to bytes

This commit is contained in:
Miguel Grinberg
2025-10-18 00:02:42 +01:00
parent 79897e7980
commit f70c524fb0
2 changed files with 3 additions and 1 deletions

View File

@@ -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})

View File

@@ -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]