In ASGI, return headers as strings and not binary (Fixes #144)

This commit is contained in:
Miguel Grinberg
2023-06-07 23:50:19 +01:00
parent 9b9b7aa76d
commit e92310fa55
2 changed files with 9 additions and 8 deletions

View File

@@ -59,8 +59,9 @@ class Microdot(BaseMicrodot):
headers = NoCaseDict()
content_length = 0
for key, value in scope.get('headers', []):
headers[key] = value
if key.lower() == 'content-length':
key = key.decode().title()
headers[key] = value.decode()
if key == 'Content-Length':
content_length = int(value)
if content_length and content_length <= Request.max_body_length:

View File

@@ -53,9 +53,9 @@ class TestMicrodotASGI(unittest.TestCase):
'type': 'http',
'path': '/foo/bar',
'query_string': b'baz=1',
'headers': [('Authorization', 'Bearer 123'),
('Cookie', 'session=xyz'),
('Content-Length', 4)],
'headers': [(b'Authorization', b'Bearer 123'),
(b'Cookie', b'session=xyz'),
(b'Content-Length', b'4')],
'client': ['1.2.3.4', 1234],
'method': 'POST',
'http_version': '1.1',
@@ -114,9 +114,9 @@ class TestMicrodotASGI(unittest.TestCase):
scope = {
'type': 'http',
'path': '/foo/bar',
'headers': [('Authorization', 'Bearer 123'),
('Cookie', 'session=xyz'),
('Content-Length', 4)],
'headers': [(b'Authorization', b'Bearer 123'),
(b'Cookie', b'session=xyz'),
(b'Content-Length', b'4')],
'client': ['1.2.3.4', 1234],
'method': 'POST',
'http_version': '1.1',