xref: /unit/test/python/variables/asgi.py (revision 1848:4bd548074e2c)
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        {
22            'type': 'http.response.start',
23            'status': 200,
24            'headers': [
25                (b'content-type', get_header(b'content-type')),
26                (b'content-length', str(len(body)).encode()),
27                (b'request-method', scope['method'].encode()),
28                (b'request-uri', scope['path'].encode()),
29                (b'http-host', get_header(b'host')),
30                (b'http-version', scope['http_version'].encode()),
31                (b'asgi-version', scope['asgi']['version'].encode()),
32                (b'asgi-spec-version', scope['asgi']['spec_version'].encode()),
33                (b'scheme', scope['scheme'].encode()),
34                (b'custom-header', get_header(b'custom-header')),
35            ],
36        }
37    )
38
39    await send({'type': 'http.response.body', 'body': body})
40