File: test_classbasedview.py

package info (click to toggle)
python-aiohttp 3.8.4-1%2Bdeb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 14,492 kB
  • sloc: python: 41,859; ansic: 25,006; makefile: 369; javascript: 31
file content (55 lines) | stat: -rw-r--r-- 1,311 bytes parent folder | download | duplicates (3)
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