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() headers = NoCaseDict()
content_length = 0 content_length = 0
for key, value in scope.get('headers', []): for key, value in scope.get('headers', []):
headers[key] = value key = key.decode().title()
if key.lower() == 'content-length': headers[key] = value.decode()
if key == 'Content-Length':
content_length = int(value) content_length = int(value)
if content_length and content_length <= Request.max_body_length: if content_length and content_length <= Request.max_body_length:

View File

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