コード例 #1
0
    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"])
コード例 #2
0
ファイル: test_routing.py プロジェクト: akornatskyy/slickpy
    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, "")
コード例 #3
0
ファイル: test_routing.py プロジェクト: akornatskyy/slickpy
    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, "")
コード例 #4
0
ファイル: test_routing.py プロジェクト: akornatskyy/slickpy
    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!")
コード例 #5
0
    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"])
コード例 #6
0
ファイル: app.py プロジェクト: akornatskyy/slickpy
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()
コード例 #7
0
ファイル: test_router.py プロジェクト: akornatskyy/slickpy
        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:
    return TextResponse(req.method)