예제 #1
0
def get_application():
    app = web.Application()

    app.add_routes([
        web.get("/hello_batman", views.hello_batman),
        api.get("/hello_components", views.hello_components),
        api.get("/hello/{name}", views.hello_path),
        api.get("/hello_query", views.hello_query),
        api.post("/hello_body", views.hello_body),
        api.view("/hello_view", views.HelloView),
    ])
    app.middlewares.append(validation_error_middleware)

    return app
예제 #2
0
    async def test_path_param(self, client_for, req, resp_status, resp_body):
        async def handler(pp: api.PathParam[int]):
            return web.json_response({"pp": pp.cleaned})

        client = await client_for(routes=[api.get("/test/{pp}", handler)])
        resp = await client.get(f"/test/{req}")

        assert resp.status == resp_status
        assert await resp.json() == resp_body
예제 #3
0
    async def test_simple(self, client_for):
        async def handler():
            return web.json_response({"super": "simple"})

        client = await client_for(routes=[api.get("/test/simple", handler)])
        resp = await client.get("/test/simple")

        assert resp.status == HTTPStatus.OK
        assert await resp.json() == {"super": "simple"}
예제 #4
0
    async def test_request_and_application(self, client_for):
        async def handler(request: web.Request, app: web.Application):
            return web.json_response({"request": id(request), "app": id(app)})

        client = await client_for(routes=[api.get("/test/reqapp", handler)])
        resp = await client.get("/test/reqapp")

        assert resp.status == HTTPStatus.OK
        result = await resp.json()
        assert isinstance(result["request"], int)
        assert isinstance(result["app"], int)
예제 #5
0
    async def test_query_param(self, client_for, req, resp_status, resp_body):
        async def handler(
                qp: api.QueryParam[int],
                qpd: api.QueryParam[str] = api.QueryParam(
                    "random"),  # noqa: B009
        ):
            return web.json_response({"qp": qp.cleaned, "qpd": qpd.cleaned})

        client = await client_for(routes=[api.get("/test/query", handler)])
        resp = await client.get("/test/query", params=req)

        assert resp.status == resp_status
        assert await resp.json() == resp_body