Skip to content

Commit

Permalink
Test ASGI lifespan state (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
gi0baro committed Jan 10, 2024
1 parent d47741e commit 283eae8
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/apps/asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async def info(scope, receive, send):
'path': scope['path'],
'query_string': scope['query_string'].decode('latin-1'),
'headers': {k.decode('utf8'): v.decode('utf8') for k, v in scope['headers']},
'state': scope['state']
}
).encode('utf8'),
'more_body': False,
Expand Down Expand Up @@ -102,7 +103,16 @@ async def err_proto(scope, receive, send):
await send({'type': 'wrong.msg'})


async def lifespan(scope, receive, send):
msg = await receive()
if msg['type'] == 'lifespan.startup':
scope['state']['global'] = 'test'
await send({'type': 'lifespan.startup.complete'})


def app(scope, receive, send):
if scope['type'] == 'lifespan':
return lifespan(scope, receive, send)
return {
'/info': info,
'/echo': echo,
Expand Down
1 change: 1 addition & 0 deletions tests/test_asgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ async def test_scope(asgi_server, threading_mode):
assert data['path'] == '/info'
assert data['query_string'] == 'test=true'
assert data['headers']['host'] == f'localhost:{port}'
assert data['state']['global'] == 'test'


@pytest.mark.asyncio
Expand Down

0 comments on commit 283eae8

Please sign in to comment.