async def test_multiple(self, client_for, req, resp_status, resp_body): class User(BaseModel): name: str age: int = 42 class View(web.View): async def put( self, request: web.Request, body: api.Body[User], pp: api.PathParam[int], qp: api.QueryParam[int], qpd: api.QueryParam[str] = api.QueryParam( "random"), # noqa: B009 ): return web.json_response({ "user": body.cleaned.dict(), "pp": pp.cleaned, "qp": qp.cleaned, "qpd": qpd.cleaned, }) client = await client_for(routes=[api.view("/test/{pp}", View)]) resp = await client.put(f"/test/{req['path']}", json=req["body"], params=req["query"]) assert resp.status == resp_status assert await resp.json() == resp_body
async def test_path_param(self, client_for, req, resp_status, resp_body): class View(web.View): async def get(self, pp: api.PathParam[int]): return web.json_response({"pp": pp.cleaned}) client = await client_for(routes=[api.view("/test/{pp}", View)]) resp = await client.get(f"/test/{req}") assert resp.status == resp_status assert await resp.json() == resp_body
async def test_simple(self, client_for): class View(web.View): async def get(self): return web.json_response({"super": "simple"}) client = await client_for(routes=[api.view("/test/simple", View)]) resp = await client.get("/test/simple") assert resp.status == HTTPStatus.OK assert await resp.json() == {"super": "simple"}
async def test_body(self, client_for, req, resp_status, resp_body): class User(BaseModel): name: str age: int = 42 class View(web.View): async def post(self, body: api.Body[User]): return web.json_response({"user": body.cleaned.dict()}) client = await client_for(routes=[api.view("/test/body", View)]) resp = await client.post("/test/body", json=req) assert resp.status == resp_status assert await resp.json() == resp_body
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
async def test_request_and_application(self, client_for): class View(web.View): async def get(self, request: web.Request, app: web.Application): return web.json_response({ "request": id(request), "app": id(app) }) client = await client_for(routes=[api.view("/test/reqapp", View)]) 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)
async def test_view(client_for): class View(web.View): async def get(self): return web.json_response({}) async def post(self): return web.json_response({}) client = await client_for(routes=[api.view("/test/view", View)]) resp = await client.get("/test/view") assert resp.status == HTTPStatus.OK resp = await client.post("/test/view") assert resp.status == HTTPStatus.OK
async def test_query_param(self, client_for, req, resp_status, resp_body): class View(web.View): async def get( self, 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.view("/test/query", View)]) resp = await client.get("/test/query", params=req) assert resp.status == resp_status assert await resp.json() == resp_body