File: test_classbasedview.py

package info (click to toggle)
python-aiohttp 1.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,288 kB
  • ctags: 4,380
  • sloc: python: 27,221; makefile: 236
file content (62 lines) | stat: -rw-r--r-- 1,411 bytes parent folder | download
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
import asyncio
from unittest import mock

import pytest

from aiohttp import web
from aiohttp.web_urldispatcher import View


def test_ctor():
    request = mock.Mock()
    view = View(request)
    assert view.request is request


@asyncio.coroutine
def test_render_ok():
    resp = web.Response(text='OK')

    class MyView(View):
        @asyncio.coroutine
        def get(self):
            return resp

    request = mock.Mock()
    request.method = 'GET'
    resp2 = yield from MyView(request)
    assert resp is resp2


@asyncio.coroutine
def test_render_unknown_method():

    class MyView(View):
        @asyncio.coroutine
        def get(self):
            return web.Response(text='OK')
        options = get

    request = mock.Mock()
    request.method = 'UNKNOWN'
    with pytest.raises(web.HTTPMethodNotAllowed) as ctx:
        yield from MyView(request)
    assert ctx.value.headers['allow'] == 'GET,OPTIONS'
    assert ctx.value.status == 405


@asyncio.coroutine
def test_render_unsupported_method():

    class MyView(View):
        @asyncio.coroutine
        def get(self):
            return web.Response(text='OK')
        options = delete = get

    request = mock.Mock()
    request.method = 'POST'
    with pytest.raises(web.HTTPMethodNotAllowed) as ctx:
        yield from MyView(request)
    assert ctx.value.headers['allow'] == 'DELETE,GET,OPTIONS'
    assert ctx.value.status == 405