Skip to content

Commit

Permalink
Added asgi dict to the lifespan scope (#754)
Browse files Browse the repository at this point in the history
* Added asgi dict to the lifespan scope

* Added lifespan scope tests
  • Loading branch information
euri10 committed Aug 15, 2020
1 parent fbce393 commit 8150c3e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
37 changes: 37 additions & 0 deletions tests/test_lifespan.py
Expand Up @@ -144,3 +144,40 @@ async def test():

loop = asyncio.new_event_loop()
loop.run_until_complete(test())


@pytest.mark.parametrize("mode", ("auto", "on"))
def test_lifespan_scope_asgi3app(mode):
async def asgi3app(scope, receive, send):
assert scope == {"version": "3.0", "spec_version": "2.0"}

async def test():
config = Config(app=asgi3app, lifespan=mode)
lifespan = LifespanOn(config)

await lifespan.startup()
await lifespan.shutdown()

loop = asyncio.new_event_loop()
loop.run_until_complete(test())


@pytest.mark.parametrize("mode", ("auto", "on"))
def test_lifespan_scope_asgi2app(mode):
def asgi2app(scope):
assert scope == {"version": "2.0", "spec_version": "2.0"}

async def asgi(receive, send):
pass

return asgi

async def test():
config = Config(app=asgi2app, lifespan=mode)
lifespan = LifespanOn(config)

await lifespan.startup()
await lifespan.shutdown()

loop = asyncio.new_event_loop()
loop.run_until_complete(test())
5 changes: 4 additions & 1 deletion uvicorn/lifespan/on.py
Expand Up @@ -44,7 +44,10 @@ async def shutdown(self):
async def main(self):
try:
app = self.config.loaded_app
scope = {"type": "lifespan"}
scope = {
"type": "lifespan",
"asgi": {"version": self.config.asgi_version, "spec_version": "2.0"},
}
await app(scope, self.receive, self.send)
except BaseException as exc:
self.asgi = None
Expand Down

0 comments on commit 8150c3e

Please sign in to comment.