import asyncio
import unittest
from unittest import mock

from aiohttp import web
from aiohttp.multidict import MultiDict
from aiohttp.protocol import HttpVersion11, RawRequestMessage


class TestWeb(unittest.TestCase):

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)

    def tearDown(self):
        self.loop.close()

    def test_handler_returns_not_response(self):
        app = web.Application(loop=self.loop)

        def handler(request):
            return 'abc'

        app.router.add_route('GET', '/', handler)
        h = app.make_handler()
        message = RawRequestMessage('GET', '/', HttpVersion11,
                                    MultiDict(), False, False)
        payload = mock.Mock()

        with self.assertRaises(RuntimeError):
            self.loop.run_until_complete(h.handle_request(message, payload))

    def test_app_loop(self):
        app = web.Application(loop=self.loop)
        self.assertIs(self.loop, app.loop)

    def test_app_default_loop(self):
        asyncio.set_event_loop(self.loop)
        app = web.Application()
        self.assertIs(self.loop, app.loop)

@@ -1136,28 +1136,1357 @@
 sertIs(self.loop, app.loop)%0A
+%0A    def test_app_register_on_finish(self):%0A        app = web.Application(loop=self.loop)%0A        cb1 = mock.Mock()%0A        cb2 = mock.Mock()%0A        app.register_on_finish(cb1, 1, b=2)%0A        app.register_on_finish(cb2, 2, c=3)%0A        self.loop.run_until_complete(app.finish())%0A        cb1.assert_called_once_with(1, b=2)%0A        cb2.assert_called_once_with(2, c=3)%0A%0A    def test_app_register_coro(self):%0A        app = web.Application(loop=self.loop)%0A%0A        fut = asyncio.Future(loop=self.loop)%0A%0A        @asyncio.coroutine%0A        def cb():%0A            yield from asyncio.sleep(0.001, loop=self.loop)%0A            fut.set_result(123)%0A%0A        app.register_on_finish(cb)%0A        self.loop.run_until_complete(app.finish())%0A        self.assertTrue(fut.done())%0A        self.assertEqual(123, fut.result())%0A%0A    def test_app_error_in_finish_callbacks(self):%0A        app = web.Application(loop=self.loop)%0A%0A        err = RuntimeError('bad call')%0A        app.register_on_finish(mock.Mock(side_effect=err))%0A        handler = mock.Mock()%0A        self.loop.set_exception_handler(handler)%0A        self.loop.run_until_complete(app.finish())%0A        exc_info = %7B'exception': err,%0A                    'application': app,%0A                    'message': 'Error in finish callback'%7D%0A        handler.assert_called_once_with(self.loop, exc_info)%0A

Add tests for finish callback