1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
|
from __future__ import annotations
from typing import Any
from typing import Callable
import pytest
from quart import Quart
from quart import request
from quart import ResponseReturnValue
from quart.views import MethodView
from quart.views import View
@pytest.fixture
def app() -> Quart:
app = Quart(__name__)
return app
async def test_view(app: Quart) -> None:
class Views(View):
methods = ["GET", "POST"]
async def dispatch_request(
self, *args: Any, **kwargs: Any
) -> ResponseReturnValue:
return request.method
app.add_url_rule("/", view_func=Views.as_view("simple"))
test_client = app.test_client()
response = await test_client.get("/")
assert "GET" == (await response.get_data(as_text=True))
response = await test_client.put("/")
assert response.status_code == 405
async def test_method_view(app: Quart) -> None:
class Views(MethodView):
async def get(self) -> ResponseReturnValue:
return "GET"
async def post(self) -> ResponseReturnValue:
return "POST"
app.add_url_rule("/", view_func=Views.as_view("simple"))
test_client = app.test_client()
response = await test_client.get("/")
assert "GET" == (await response.get_data(as_text=True))
response = await test_client.post("/")
assert "POST" == (await response.get_data(as_text=True))
async def test_view_decorators(app: Quart) -> None:
def decorate_status_code(func: Callable) -> Callable:
async def wrapper(*args: Any, **kwargs: Any) -> ResponseReturnValue:
response = await func(*args, **kwargs)
return response, 201
return wrapper
class Views(View):
decorators = [decorate_status_code]
methods = ["GET"]
async def dispatch_request(
self, *args: Any, **kwargs: Any
) -> ResponseReturnValue:
return request.method
app.add_url_rule("/", view_func=Views.as_view("simple"))
test_client = app.test_client()
response = await test_client.get("/")
assert response.status_code == 201
|