def test_lifespan_event_fails(self) -> None: main = App() dispatched_events = [] sent_events = [] @main.on("lifespan.shutdown") async def shutdown() -> None: dispatched_events.append("shutdown") raise ValueError() @main.on("lifespan.shutdown.failed") async def failed() -> None: dispatched_events.append("shutdown.failed") @main.on("lifespan.shutdown.complete") async def complete() -> None: # pragma: nocover self.fail() async def receive() -> Message: return {"type": "lifespan.shutdown"} async def send(m: Message) -> None: sent_events.append(m["type"]) loop = asyncio.get_event_loop() loop.run_until_complete(main.asgi()({ "type": "lifespan" }, receive, send)) self.assertEqual(dispatched_events, ["shutdown", "shutdown.failed"]) self.assertEqual(sent_events, ["lifespan.shutdown.failed"])
def test_unknown_asgi_adapter(self) -> None: async def root(f: float) -> None: # pragma: nocover pass main = App() self.assertRaises(AssertionError, lambda: main.route("/")(root)) async def unknown() -> None: pass # pragma: nocover self.assertRaises(AssertionError, lambda: main.route("/")(unknown))
def test_http_not_found(self) -> None: app = App() @app.route("/") async def a() -> ASGICallable: # pragma: nocover self.fail() @app.route(r"^/(?P<locale>en|de|uk)/welcome$") async def b() -> ASGICallable: # pragma: nocover self.fail() client = ASGIClient(app.asgi()) for pattern in ["/x", "/ru/welcome"]: res = client.go(pattern) self.assertEqual(res.status_code, 404) self.assertEqual(res.text, "")
def test_http_method_not_allowed(self) -> None: app = App() @app.route("/", methods=("POST", )) async def a() -> ASGICallable: # pragma: nocover self.fail() @app.route(r"^/(?P<locale>en|de|uk)/welcome$", methods=("GET", )) async def b() -> ASGICallable: # pragma: nocover self.fail() client = ASGIClient(app.asgi()) for pattern in ["/", "/uk/welcome"]: res = client.go(pattern, method="PUT") self.assertEqual(res.status_code, 405) self.assertEqual(res.text, "")
def test_regex_route(self) -> None: app = App() @app.route(r"^/(?P<user>\w+)/(?P<repo>\w+)$") async def handler(scope: Scope, receive: Receive, send: Send) -> None: self.assertEqual(scope["route_params"], { "repo": "slickpy", "user": "******" }) await send({ "type": "http.response.start", "status": 200, "headers": [] }) await send({ "type": "http.response.body", "body": b"Hello, world!" }) client = ASGIClient(app.asgi()) res = client.go("/smith/slickpy") self.assertEqual(res.status_code, 200) self.assertEqual(res.text, "Hello, world!")
def test_middleware(self) -> None: main = App() calls = [] @main.middleware def my_middleware(following: ASGICallable) -> ASGICallable: calls.append("init") async def w(scope: Scope, receive: Receive, send: Send) -> None: calls.append("before") await following(scope, receive, send) calls.append("after") return w @main.route("/") async def root() -> TextResponse: return TextResponse("root") client = ASGIClient(main.asgi()) res = client.go() self.assertEqual(res.text, "root") self.assertEqual(calls, ["init", "before", "after"])
from slickpy import App, Writer app = App() @app.route("/") async def welcome(w: Writer) -> None: await w.end(b"Hello, world!") # uvicorn --app-dir examples/helloworld app:main main = app.asgi()
def test_route_head_method(self) -> None: res = client.go(method="HEAD") self.assertEqual(res.status_code, 200) self.assertEqual(res.text, "") res = client.go("/uk/welcome", method="HEAD") self.assertEqual(res.status_code, 200) self.assertEqual(res.text, "") def test_route_not_found(self) -> None: for method in ["GET", "POST", "PUT", "DELETE"]: res = client.go("/does-not-exist", method=method) self.assertEqual(res.status_code, 404) self.assertEqual(res.text, "") app = App() client = ASGIClient(app.asgi()) @app.route("/") async def root(req: Request) -> TextResponse: return TextResponse(req.method) @app.route("/", methods=("POST", "PUT", "DELETE")) async def other_methods(req: Request) -> TextResponse: return TextResponse(req.method) @app.route(r"^/(?P<locale>en|de|uk)/welcome$") async def welcome(req: Request) -> TextResponse: