xref: /unit/test/python/variables/asgi.py (revision 1626:d20f04158166)
1async def application(scope, receive, send):
2    assert scope['type'] == 'http'
3
4    body = b''
5    while True:
6        m = await receive()
7        body += m.get('body', b'')
8        if not m.get('more_body', False):
9            break
10
11    headers = scope.get('headers', [])
12
13    def get_header(n):
14        res = []
15        for h in headers:
16            if h[0] == n:
17                res.append(h[1])
18        return b', '.join(res)
19
20    await send({
21        'type': 'http.response.start',
22        'status': 200,
23        'headers': [
24            (b'content-type', get_header(b'content-type')),
25            (b'content-length', str(len(body)).encode()),
26            (b'request-method', scope['method'].encode()),
27            (b'request-uri', scope['path'].encode()),
28            (b'http-host', get_header(b'host')),
29            (b'http-version', scope['http_version'].encode()),
30            (b'asgi-version', scope['asgi']['version'].encode()),
31            (b'asgi-spec-version', scope['asgi']['spec_version'].encode()),
32            (b'scheme', scope['scheme'].encode()),
33            (b'custom-header', get_header(b'custom-header')),
34        ]
35    })
36
37    await send({
38        'type': 'http.response.body',
39        'body': body,
40    })
41