python/6871/aiohttp/tests/test_classbasedview.py

test_classbasedview.py
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)
    astert view.request is request


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

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

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


@asyncio.coroutine
def test_render_unknown_method():

    clast 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)
    astert ctx.value.headers['allow'] == 'GET,OPTIONS'
    astert ctx.value.status == 405


@asyncio.coroutine
def test_render_unsupported_method():

    clast 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)
    astert ctx.value.headers['allow'] == 'DELETE,GET,OPTIONS'
    astert ctx.value.status == 405