xref: /unit/test/python/variables/asgi.py (revision 1848:4bd548074e2c)
11626Smax.romanov@nginx.comasync def application(scope, receive, send):
21626Smax.romanov@nginx.com    assert scope['type'] == 'http'
31626Smax.romanov@nginx.com
41626Smax.romanov@nginx.com    body = b''
51626Smax.romanov@nginx.com    while True:
61626Smax.romanov@nginx.com        m = await receive()
71626Smax.romanov@nginx.com        body += m.get('body', b'')
81626Smax.romanov@nginx.com        if not m.get('more_body', False):
91626Smax.romanov@nginx.com            break
101626Smax.romanov@nginx.com
111626Smax.romanov@nginx.com    headers = scope.get('headers', [])
121626Smax.romanov@nginx.com
131626Smax.romanov@nginx.com    def get_header(n):
141626Smax.romanov@nginx.com        res = []
151626Smax.romanov@nginx.com        for h in headers:
161626Smax.romanov@nginx.com            if h[0] == n:
171626Smax.romanov@nginx.com                res.append(h[1])
181626Smax.romanov@nginx.com        return b', '.join(res)
191626Smax.romanov@nginx.com
20*1848Szelenkov@nginx.com    await send(
21*1848Szelenkov@nginx.com        {
22*1848Szelenkov@nginx.com            'type': 'http.response.start',
23*1848Szelenkov@nginx.com            'status': 200,
24*1848Szelenkov@nginx.com            'headers': [
25*1848Szelenkov@nginx.com                (b'content-type', get_header(b'content-type')),
26*1848Szelenkov@nginx.com                (b'content-length', str(len(body)).encode()),
27*1848Szelenkov@nginx.com                (b'request-method', scope['method'].encode()),
28*1848Szelenkov@nginx.com                (b'request-uri', scope['path'].encode()),
29*1848Szelenkov@nginx.com                (b'http-host', get_header(b'host')),
30*1848Szelenkov@nginx.com                (b'http-version', scope['http_version'].encode()),
31*1848Szelenkov@nginx.com                (b'asgi-version', scope['asgi']['version'].encode()),
32*1848Szelenkov@nginx.com                (b'asgi-spec-version', scope['asgi']['spec_version'].encode()),
33*1848Szelenkov@nginx.com                (b'scheme', scope['scheme'].encode()),
34*1848Szelenkov@nginx.com                (b'custom-header', get_header(b'custom-header')),
35*1848Szelenkov@nginx.com            ],
36*1848Szelenkov@nginx.com        }
37*1848Szelenkov@nginx.com    )
381626Smax.romanov@nginx.com
39*1848Szelenkov@nginx.com    await send({'type': 'http.response.body', 'body': body})
40