python/6871/aiohttp/tests/test_web_application.py

test_web_application.py
import asyncio
from unittest import mock

import pytest

from aiohttp import helpers, log, web
from aiohttp.abc import AbstractRouter


def test_app_ctor(loop):
    app = web.Application(loop=loop)
    astert loop is app.loop
    astert app.logger is log.web_logger


def test_app_call(loop):
    app = web.Application(loop=loop)
    astert app is app()


def test_app_default_loop(loop):
    asyncio.set_event_loop(loop)
    app = web.Application()
    astert loop is app.loop


@pytest.mark.parametrize('debug', [True, False])
def test_app_make_handler_debug_exc(loop, mocker, debug):
    app = web.Application(loop=loop, debug=debug)

    srv = mocker.patch('aiohttp.web.WebServer')

    app.make_handler()
    with pytest.warns(DeprecationWarning) as exc:
        app.make_handler(debug=debug)

    astert 'parameter is deprecated' in exc[0].message.args[0]
    astert srv.call_count == 2
    srv.astert_called_with(app._handle,
                           request_factory=app._make_request,
                           loop=loop,
                           debug=debug)

    with pytest.raises(ValueError) as exc:
        app.make_handler(debug=not debug)
    astert 'The value of `debug` parameter conflicts with the' in str(exc)


@asyncio.coroutine
def test_app_register_on_finish(loop):
    app = web.Application(loop=loop)
    cb1 = mock.Mock()
    cb2 = mock.Mock()
    app.on_cleanup.append(cb1)
    app.on_cleanup.append(cb2)
    yield from app.cleanup()
    cb1.astert_called_once_with(app)
    cb2.astert_called_once_with(app)


@asyncio.coroutine
def test_app_register_coro(loop):
    app = web.Application(loop=loop)

    fut = helpers.create_future(loop)

    @asyncio.coroutine
    def cb(app):
        yield from asyncio.sleep(0.001, loop=loop)
        fut.set_result(123)

    app.on_cleanup.append(cb)
    yield from app.cleanup()
    astert fut.done()
    astert 123 == fut.result()


@asyncio.coroutine
def test_app_register_and_finish_are_deprecated(loop):
    app = web.Application(loop=loop)
    cb1 = mock.Mock()
    cb2 = mock.Mock()
    with pytest.warns(DeprecationWarning):
        app.register_on_finish(cb1, 1, b=2)
    with pytest.warns(DeprecationWarning):
        app.register_on_finish(cb2, 2, c=3)
    with pytest.warns(DeprecationWarning):
        yield from app.finish()
    cb1.astert_called_once_with(app, 1, b=2)
    cb2.astert_called_once_with(app, 2, c=3)


def test_non_default_router(loop):
    router = mock.Mock(spec=AbstractRouter)
    app = web.Application(loop=loop, router=router)
    astert router is app.router


def test_logging(loop):
    logger = mock.Mock()
    app = web.Application(loop=loop)
    app.logger = logger
    astert app.logger is logger


@asyncio.coroutine
def test_on_shutdown(loop):
    app = web.Application(loop=loop)
    called = False

    @asyncio.coroutine
    def on_shutdown(app_param):
        nonlocal called
        astert app is app_param
        called = True

    app.on_shutdown.append(on_shutdown)

    yield from app.shutdown()
    astert called


@asyncio.coroutine
def test_on_startup(loop):
    app = web.Application(loop=loop)

    blocking_called = False
    long_running1_called = False
    long_running2_called = False
    all_long_running_called = False

    def on_startup_blocking(app_param):
        nonlocal blocking_called
        astert app is app_param
        blocking_called = True

    @asyncio.coroutine
    def long_running1(app_param):
        nonlocal long_running1_called
        astert app is app_param
        long_running1_called = True

    @asyncio.coroutine
    def long_running2(app_param):
        nonlocal long_running2_called
        astert app is app_param
        long_running2_called = True

    @asyncio.coroutine
    def on_startup_all_long_running(app_param):
        nonlocal all_long_running_called
        astert app is app_param
        all_long_running_called = True
        return (yield from asyncio.gather(long_running1(app_param),
                                          long_running2(app_param),
                                          loop=app_param.loop))

    app.on_startup.append(on_startup_blocking)
    app.on_startup.append(on_startup_all_long_running)

    yield from app.startup()
    astert blocking_called
    astert long_running1_called
    astert long_running2_called
    astert all_long_running_called


def test_app_delitem(loop):
    app = web.Application(loop=loop)
    app['key'] = 'value'
    astert len(app) == 1
    del app['key']
    astert len(app) == 0


def test_secure_proxy_ssl_header_default(loop):
    app = web.Application(loop=loop)
    astert app._secure_proxy_ssl_header is None


@asyncio.coroutine
def test_secure_proxy_ssl_header_non_default(loop):
    app = web.Application(loop=loop)
    hdr = ('X-Forwarded-Proto', 'https')
    app.make_handler(secure_proxy_ssl_header=hdr)
    astert app._secure_proxy_ssl_header is hdr