Skip to content

Commit e92310f

Browse files
In ASGI, return headers as strings and not binary (Fixes miguelgrinberg#144)
1 parent 9b9b7aa commit e92310f

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

src/microdot_asgi.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,9 @@ async def asgi_app(self, scope, receive, send):
5959
headers = NoCaseDict()
6060
content_length = 0
6161
for key, value in scope.get('headers', []):
62-
headers[key] = value
63-
if key.lower() == 'content-length':
62+
key = key.decode().title()
63+
headers[key] = value.decode()
64+
if key == 'Content-Length':
6465
content_length = int(value)
6566

6667
if content_length and content_length <= Request.max_body_length:

tests/test_microdot_asgi.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ def after_request(req, res):
5353
'type': 'http',
5454
'path': '/foo/bar',
5555
'query_string': b'baz=1',
56-
'headers': [('Authorization', 'Bearer 123'),
57-
('Cookie', 'session=xyz'),
58-
('Content-Length', 4)],
56+
'headers': [(b'Authorization', b'Bearer 123'),
57+
(b'Cookie', b'session=xyz'),
58+
(b'Content-Length', b'4')],
5959
'client': ['1.2.3.4', 1234],
6060
'method': 'POST',
6161
'http_version': '1.1',
@@ -114,9 +114,9 @@ async def index(req):
114114
scope = {
115115
'type': 'http',
116116
'path': '/foo/bar',
117-
'headers': [('Authorization', 'Bearer 123'),
118-
('Cookie', 'session=xyz'),
119-
('Content-Length', 4)],
117+
'headers': [(b'Authorization', b'Bearer 123'),
118+
(b'Cookie', b'session=xyz'),
119+
(b'Content-Length', b'4')],
120120
'client': ['1.2.3.4', 1234],
121121
'method': 'POST',
122122
'http_version': '1.1',

0 commit comments

Comments
 (0)