<commit_before>import aiohttp
import asyncio
from functools import partial
from misuzu.protocol import HttpProtocol
try:
    import uvloop
except:
    uvloop = asyncio


__all__ = ['TestClient']


HOST = '127.0.0.1'
PORT = 40404


class TestClient:
    app = None
    loop = None
    server = None

    def __init__(self, app=None):
        self.app = app
        self.loop = uvloop.new_event_loop()
        self.server = None

    def __request(self, method, url, *args, **kwargs):

        async def test_middleware(context, next):
            await next()
            context.app.ctx = context

        async def __local_request(method, url, *args, **kwargs):
            url = 'http://{host}:{port}{uri}'.format(host=HOST, port=PORT, uri=url)
            async with aiohttp.ClientSession(loop=self.loop) as session:
                async with getattr(session, method)(url, *args, **kwargs) as response:
                    asyncio.get_event_loop().stop()

        self.app.chain.append(test_middleware)
        asyncio.set_event_loop(self.loop)

        server_coroutine = self.loop.create_server(partial(HttpProtocol, loop=self.loop, app=self.app), HOST, PORT)
        server_loop = self.loop.run_until_complete(server_coroutine)
        self.loop.run_until_complete(__local_request(method, url, *args, **kwargs))
        self.loop.run_forever()
        return self.app.ctx.res, self.app.ctx

    def head(self, url, *args, **kwargs):
        return self.__request('head', url, *args, **kwargs)<commit_msg>fixed: that raise one address and post can be listened once<commit_after>import aiohttp
import asyncio
from functools import partial
from misuzu.protocol import HttpProtocol
try:
    import uvloop
except:
    uvloop = asyncio


__all__ = ['TestClient']


HOST = '127.0.0.1'
PORT = 40404


class TestClient:
    app = None
    loop = None
    server = None

    def __init__(self, app=None):
        self.app = app
        self.loop = uvloop.new_event_loop()
        self.server = None

    def __request(self, method, url, *args, **kwargs):

        async def test_middleware(context, next):
            await next()
            context.app.ctx = context

        async def __local_request(method, url, *args, **kwargs):
            url = 'http://{host}:{port}{uri}'.format(host=HOST, port=PORT, uri=url)
            async with aiohttp.ClientSession(loop=self.loop) as session:
                async with getattr(session, method)(url, *args, **kwargs) as response:
                    asyncio.get_event_loop().stop()

        self.app.chain.append(test_middleware)
        asyncio.set_event_loop(self.loop)

        server_coroutine = self.loop.create_server(partial(HttpProtocol, loop=self.loop, app=self.app), HOST, PORT)
        server_loop = self.loop.run_until_complete(server_coroutine)
        self.loop.run_until_complete(__local_request(method, url, *args, **kwargs))
        self.loop.run_forever()
        server_loop.close()
        return self.app.ctx.res, self.app.ctx

    def head(self, url, *args, **kwargs):
        return self.__request('head', url, *args, **kwargs)
{'fraction_non_alphanumeric': 0.11757105943152454, 'fraction_numerical': 0.0071059431524547806, 'mean_word_length': 2.8567870485678704, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6305217', 'n_tokens_mistral': 988, 'n_tokens_neox': 950, 'n_words': 224}