<commit_before>import asyncio
import socket
import pytest
import unittest
from aiohttp import web, request


@pytest.mark.run_loop
def test_middleware_modifies_response(create_app_and_client):

        @asyncio.coroutine
        def handler(request):
            return web.Response(body=b'OK')

        @asyncio.coroutine
        def middleware_factory(app, handler):
            def middleware(request):
                resp = yield from handler(request)
                assert 200 == resp.status
                resp.set_status(201)
                resp.text = resp.text + '[MIDDLEWARE]'
                return resp
            return middleware

        app, client = yield from create_app_and_client()
        app.middlewares.append(middleware_factory)
        app.router.add_route('GET', '/', handler)
        resp = yield from client.get('/')
        assert 201 == resp.status
        txt = yield from resp.text()
        assert 'OK[MIDDLEWARE]' == txt


class TestWebMiddlewareFunctional(unittest.TestCase):

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

    def tearDown(self):
        if self.handler:
            self.loop.run_until_complete(self.handler.finish_connections())
        self.loop.close()

    def find_unused_port(self):
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.bind(('127.0.0.1', 0))
        port = s.getsockname()[1]
        s.close()
        return port

    @asyncio.coroutine
    def create_server(self, method, path, handler, *middlewares):
        app = web.Application(loop=self.loop, middlewares=middlewares)
        app.router.add_route(method, path, handler)

        port = self.find_unused_port()
        self.handler = app.make_handler()
        srv = yield from self.loop.create_server(self.handler, '127.0.0.1',
                                                 port)

        url = "http://127.0.0.1:{}".format(port) + path
        self.addCleanup(srv.close)
        return app, srv, url

    def test_middleware_handles_exception(self):

        @asyncio.coroutine
        def handler(request):
            raise RuntimeError('Error text')

        @asyncio.coroutine
        def middleware_factory(app, handler):
            def middleware(request):
                with self.assertRaises(RuntimeError) as ctx:
                    yield from handler(request)
                return web.Response(status=501,
                                    text=str(ctx.exception) + '[MIDDLEWARE]')

            return middleware

        @asyncio.coroutine
        def go():
            _, _, url = yield from self.create_server('GET', '/', handler,
                                                      middleware_factory)
            resp = yield from request('GET', url, loop=self.loop)
            self.assertEqual(501, resp.status)
            txt = yield from resp.text()
            self.assertEqual('Error text[MIDDLEWARE]', txt)

        self.loop.run_until_complete(go())

    def test_middleware_chain(self):

        @asyncio.coroutine
        def handler(request):
            return web.Response(text='OK')

        def make_factory(num):

            @asyncio.coroutine
            def factory(app, handler):

                def middleware(request):
                    resp = yield from handler(request)
                    resp.text = resp.text + '[{}]'.format(num)
                    return resp

                return middleware
            return factory

        @asyncio.coroutine
        def go():
            _, _, url = yield from self.create_server('GET', '/', handler,
                                                      make_factory(1),
                                                      make_factory(2))
            resp = yield from request('GET', url, loop=self.loop)
            self.assertEqual(200, resp.status)
            txt = yield from resp.text()
            self.assertEqual('OK[2][1]', txt)

        self.loop.run_until_complete(go())
<commit_msg>Convert web middlewares test to pytest style<commit_after>import asyncio
import pytest
from aiohttp import web


@pytest.mark.run_loop
def test_middleware_modifies_response(create_app_and_client):

    @asyncio.coroutine
    def handler(request):
        return web.Response(body=b'OK')

    @asyncio.coroutine
    def middleware_factory(app, handler):

        @asyncio.coroutine
        def middleware(request):
            resp = yield from handler(request)
            assert 200 == resp.status
            resp.set_status(201)
            resp.text = resp.text + '[MIDDLEWARE]'
            return resp
        return middleware

    app, client = yield from create_app_and_client()
    app.middlewares.append(middleware_factory)
    app.router.add_route('GET', '/', handler)
    resp = yield from client.get('/')
    assert 201 == resp.status
    txt = yield from resp.text()
    assert 'OK[MIDDLEWARE]' == txt


@pytest.mark.run_loop
def test_middleware_handles_exception(create_app_and_client):

    @asyncio.coroutine
    def handler(request):
        raise RuntimeError('Error text')

    @asyncio.coroutine
    def middleware_factory(app, handler):

        @asyncio.coroutine
        def middleware(request):
            with pytest.raises(RuntimeError) as ctx:
                yield from handler(request)
            return web.Response(status=501,
                                text=str(ctx.value) + '[MIDDLEWARE]')

        return middleware

    app, client = yield from create_app_and_client()
    app.middlewares.append(middleware_factory)
    app.router.add_route('GET', '/', handler)
    resp = yield from client.get('/')
    assert 501 == resp.status
    txt = yield from resp.text()
    assert 'Error text[MIDDLEWARE]' == txt


@pytest.mark.run_loop
def test_middleware_chain(create_app_and_client):

    @asyncio.coroutine
    def handler(request):
        return web.Response(text='OK')

    def make_factory(num):

        @asyncio.coroutine
        def factory(app, handler):

            def middleware(request):
                resp = yield from handler(request)
                resp.text = resp.text + '[{}]'.format(num)
                return resp

            return middleware
        return factory

    app, client = yield from create_app_and_client()
    app.middlewares.append(make_factory(1))
    app.middlewares.append(make_factory(2))
    app.router.add_route('GET', '/', handler)
    resp = yield from client.get('/')
    assert 200 == resp.status
    txt = yield from resp.text()
    assert 'OK[2][1]' == txt

{'fraction_non_alphanumeric': 0.10248635536688902, 'fraction_numerical': 0.009702850212249849, 'mean_word_length': 2.034498620055198, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 3, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 9, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7433899', 'n_tokens_mistral': 1920, 'n_tokens_neox': 1840, 'n_words': 397}