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
|
from unittest import mock
import pytest
from aiohttp import web
from aiohttp.web_urldispatcher import View
def test_ctor() -> None:
request = mock.Mock()
view = View(request)
assert view.request is request
async def test_render_ok() -> None:
resp = web.Response(text="OK")
class MyView(View):
async def get(self):
return resp
request = mock.Mock()
request.method = "GET"
resp2 = await MyView(request)
assert resp is resp2
async def test_render_unknown_method() -> None:
class MyView(View):
async def get(self):
return web.Response(text="OK")
options = get
request = mock.Mock()
request.method = "UNKNOWN"
with pytest.raises(web.HTTPMethodNotAllowed) as ctx:
await MyView(request)
assert ctx.value.headers["allow"] == "GET,OPTIONS"
assert ctx.value.status == 405
async def test_render_unsupported_method() -> None:
class MyView(View):
async def get(self):
return web.Response(text="OK")
options = delete = get
request = mock.Mock()
request.method = "POST"
with pytest.raises(web.HTTPMethodNotAllowed) as ctx:
await MyView(request)
assert ctx.value.headers["allow"] == "DELETE,GET,OPTIONS"
assert ctx.value.status == 405
|