Example #1
0
async def test_post_multipart_to_django(live_server):
    """Test post multipart."""
    url = live_server.url + '/post_file'
    data = {'foo': open('tests/files/bar.txt', 'rb'), 'field1': 'foo'}

    async with aiosonic.HTTPClient() as client:
        res = await client.post(url, data=data, multipart=True)
        assert res.status_code == 200
        assert await res.text() == 'bar-foo'
Example #2
0
async def test_max_redirects(app, aiohttp_server):
    """Test simple get."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/max_redirects' % server.port
    client = aiosonic.HTTPClient()

    with pytest.raises(MaxRedirects):
        await client.get(url, follow=True)
    await server.close()
Example #3
0
async def test_simple_get_ssl_no_valid(app, aiohttp_server, ssl_context):
    """Test simple get with https no valid."""
    server = await aiohttp_server(app, ssl=ssl_context)
    url = 'https://localhost:%d' % server.port
    async with aiosonic.HTTPClient() as client:

        with pytest.raises(ssl.SSLError):
            await client.get(url)
        await server.close()
Example #4
0
async def test_read_timeout(app, aiohttp_server, mocker):
    """Test read timeout."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/slow_request' % server.port
    connector = TCPConnector(timeouts=Timeouts(sock_read=0.2))
    async with aiosonic.HTTPClient(connector) as client:
        with pytest.raises(ReadTimeout):
            await client.get(url)
        await server.close()
Example #5
0
async def test_get_http2(http2_serv):
    """Test simple get to node http2 server."""
    url = http2_serv
    connector = TCPConnector(timeouts=Timeouts(sock_connect=3, sock_read=4))

    client = aiosonic.HTTPClient(connector)
    res = await client.get(url, verify=False)
    assert res.status_code == 200
    assert 'Hello World' == await res.text()
Example #6
0
async def test_get_with_params_in_url(app, aiohttp_server):
    """Test get with params."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d?foo=bar' % server.port

    async with aiosonic.HTTPClient() as client:
        res = await client.get(url)
        assert res.status_code == 200
        assert await res.text() == 'bar'
        await server.close()
Example #7
0
async def test_delete(app, aiohttp_server):
    """Test delete."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/delete' % server.port

    async with aiosonic.HTTPClient() as client:
        res = await client.delete(url)
        assert res.status_code == 200
        assert await res.text() == 'deleted'
        await server.close()
Example #8
0
async def test_simple_get_ssl(app, aiohttp_server, ssl_context):
    """Test simple get with https."""
    server = await aiohttp_server(app, ssl=ssl_context)
    url = 'https://localhost:%d' % server.port

    async with aiosonic.HTTPClient() as client:
        res = await client.get(url, verify=False)
        assert res.status_code == 200
        assert await res.text() == 'Hello, world'
        await server.close()
Example #9
0
async def test_simple_get_ssl_no_valid(app, aiohttp_server, ssl_context):
    """Test simple get with https no valid."""
    server = await aiohttp_server(app, ssl=ssl_context)
    url = 'https://localhost:%d' % server.port
    client = aiosonic.HTTPClient()

    # python 3.5 compatibility
    with pytest.raises(getattr(ssl, 'SSLCertVerificationError', ssl.SSLError)):
        await client.get(url)
    await server.close()
Example #10
0
async def test_get_image(http2_serv):
    """Test get image."""
    url = http2_serv + 'sample.png'

    async with aiosonic.HTTPClient() as client:
        res = await client.get(url, verify=False)
        assert res.status_code == 200
        assert res.chunked
        with open('tests/sample.png', 'rb') as _file:
            assert (await res.content()) == _file.read()
Example #11
0
async def test_post_tuple_form_urlencoded(app, aiohttp_server):
    """Test post form urlencoded tuple."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/post' % server.port
    data = (('foo', 'bar'), )

    async with aiosonic.HTTPClient() as client:
        res = await client.post(url, data=data)
        assert res.status_code == 200
        assert await res.text() == 'bar'
        await server.close()
Example #12
0
async def test_get_with_params_tuple(app, aiohttp_server):
    """Test get with params as tuple."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d' % server.port
    params = (('foo', 'bar'), )

    async with aiosonic.HTTPClient() as client:
        res = await client.get(url, params=params)
        assert res.status_code == 200
        assert await res.text() == 'bar'
        await server.close()
Example #13
0
async def test_simple_get(app, aiohttp_server):
    """Test simple get."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d' % server.port

    client = aiosonic.HTTPClient()
    res = await client.get(url)
    assert res.status_code == 200
    assert await res.content() == b'Hello, world'
    assert await res.text() == 'Hello, world'
    await server.close()
Example #14
0
async def test_post_form_urlencoded(app, aiohttp_server):
    """Test post form urlencoded."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/post' % server.port
    data = {'foo': 'bar'}

    client = aiosonic.HTTPClient()
    res = await client.post(url, data=data)
    assert res.status_code == 200
    assert await res.text() == 'bar'
    await server.close()
Example #15
0
async def test_get_with_params(app, aiohttp_server):
    """Test get with params."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d' % server.port
    params = {'foo': 'bar'}

    client = aiosonic.HTTPClient()
    res = await client.get(url, params=params)
    assert res.status_code == 200
    assert await res.text() == 'bar'
    await server.close()
Example #16
0
async def test_post_json(app, aiohttp_server):
    """Test post json."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/post_json' % server.port
    data = {'foo': 'bar'}

    async with aiosonic.HTTPClient() as client:
        res = await client.post(url, json=data, headers=[['x-foo', 'bar']])
        assert res.status_code == 200
        assert await res.text() == 'bar'
        await server.close()
Example #17
0
async def test_get_body_gzip(app, aiohttp_server):
    """Test simple get."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/gzip' % server.port

    async with aiosonic.HTTPClient() as client:
        res = await client.get(
            url, headers={'Accept-Encoding': 'gzip, deflate, br'})
        content = await res.content()
        assert res.status_code == 200
        assert content == b'Hello, world'
        await server.close()
Example #18
0
async def test_read_chunks_by_text_method(app, aiohttp_server):
    """Test read chunks by text method."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/chunked' % server.port

    async with aiosonic.HTTPClient() as client:
        res = await client.get(url)
        assert res.connection
        assert res.status_code == 200
        assert await res.text() == 'foobar'
        assert await res.text() == 'foobar'  # cached body in response object
        await server.close()
Example #19
0
async def test_cache(app, aiohttp_server):
    """Test simple get."""
    server = await aiohttp_server(app)
    client = aiosonic.HTTPClient()

    for time in range(520):
        url = 'http://localhost:%d/%d' % (server.port, time)

        await client.get(url, headers={'Accept-Encoding': 'gzip, deflate, br'})
    assert len(_get_url_parsed.cache) == 512
    assert next(iter(_get_url_parsed.cache)) == url.replace('/519', '/8')
    await server.close()
Example #20
0
async def test_get_chunked_response_and_not_read_it(app, aiohttp_server):
    """Test get chunked response and not read it.

    Also, trigger gc delete.
    """
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/chunked' % server.port

    async with aiosonic.HTTPClient() as client:
        res = await client.get(url)
        assert client.connector.pool.free_conns(), 24
        del res
        assert client.connector.pool.free_conns(), 25

    connector = aiosonic.TCPConnector(pool_cls=CyclicQueuePool)
    async with aiosonic.HTTPClient(connector) as client:
        res = await client.get(url)
        assert client.connector.pool.free_conns(), 24
        del res
        assert client.connector.pool.free_conns(), 25
        await server.close()
Example #21
0
async def test_simple_get(app, aiohttp_server):
    """Test simple get."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d' % server.port

    connector = TCPConnector(timeouts=Timeouts(sock_connect=3, sock_read=4))
    async with aiosonic.HTTPClient(connector) as client:
        res = await client.get(url)
        assert res.status_code == 200
        assert await res.content() == b'Hello, world'
        assert await res.text() == 'Hello, world'
        await server.close()
Example #22
0
async def test_simple_get_ssl_ctx(app, aiohttp_server, ssl_context):
    """Test simple get with https and ctx."""
    server = await aiohttp_server(app, ssl=ssl_context)
    url = 'https://localhost:%d' % server.port

    ssl_context = ssl.create_default_context(ssl.Purpose.SERVER_AUTH, )
    ssl_context.check_hostname = False
    ssl_context.verify_mode = ssl.CERT_NONE
    async with aiosonic.HTTPClient() as client:
        res = await client.get(url, ssl=ssl_context)
        assert res.status_code == 200
        assert await res.text() == 'Hello, world'
        await server.close()
Example #23
0
    async def initialize(self):
        """
        Override this method if you need to do some initialization
        in your subclass before run() is called,
        (for example initiate state variables).
        """
        # Adapters
        await self._create_secondary_adapters()

        # Client
        if self.makes_requests:
            self.client = aiosonic.HTTPClient()
            await self._login()
Example #24
0
async def test_get_image_chunked(http2_serv):
    """Test get image chunked."""
    url = http2_serv + 'sample.png'

    async with aiosonic.HTTPClient() as client:
        res = await client.get(url, verify=False)
        assert res.status_code == 200
        assert res.chunked
        filebytes = b''
        async for chunk in res.read_chunks():
            filebytes += chunk
        with open('tests/sample.png', 'rb') as _file:
            assert filebytes == _file.read()
Example #25
0
async def test_close_connection(app, aiohttp_server):
    """Test close connection."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/post' % server.port

    connector = TCPConnector(pool_size=1, connection_cls=MyConnection)
    async with aiosonic.HTTPClient(connector) as client:

        res = await client.post(url, data=b'close')
        async with await connector.pool.acquire() as connection:

            assert res.status_code == 200
            assert not connection.keep
            assert await res.text() == 'close'
Example #26
0
async def test_pool_acquire_timeout(app, aiohttp_server, mocker):
    """Test pool acquirere timeout."""
    server = await aiohttp_server(app)
    url = 'http://localhost:%d/slow_request' % server.port

    connector = TCPConnector(pool_size=1, timeouts=Timeouts(pool_acquire=0.3))
    async with aiosonic.HTTPClient(connector) as client:

        with pytest.raises(ConnectionPoolAcquireTimeout):
            await asyncio.gather(
                client.get(url),
                client.get(url),
            )
        await server.close()
Example #27
0
    def __init__(self, logger, args, **kwargs):
        """Initialize runner."""
        self.logger = logger
        self.args = args
        self.kwargs = kwargs
        self.client = aiosonic.HTTPClient(
            connector=TCPConnector(pool_size=args.concurrency),
            verify_ssl=not args.insecure)

        logger.info('preparing_requests')
        self.requests_data = [
            self.prepare_request(**kwargs)
            for _ in range(args.number_of_requests)
        ]
        logger.info('prepared_requests')
Example #28
0
async def test_get_with_cookies(app, aiohttp_server):
    """Test simple get."""
    server = await aiohttp_server(app)
    url = f'http://localhost:{server.port}/cookies'

    connector = TCPConnector(timeouts=Timeouts(sock_connect=3, sock_read=4))
    async with aiosonic.HTTPClient(connector, handle_cookies=True) as client:
        res = await client.get(url)
        assert res.status_code == 200
        assert res.cookies

        # check if server got cookies
        res = await client.get(url)
        assert await res.text() == 'Got cookies'
        await server.close()
Example #29
0
async def test_connect_timeout(mocker):
    """Test connect timeout."""
    url = 'http://localhost:1234'

    async def long_connect(*_args, **_kwargs):
        await asyncio.sleep(3)

    _connect = mocker.patch('aiosonic.connection.Connection._connect',
                            new=long_connect)
    _connect.return_value = long_connect()
    connector = TCPConnector(timeouts=Timeouts(sock_connect=0.2))

    with pytest.raises(ConnectTimeout):
        client = aiosonic.HTTPClient(connector)
        await client.get(url)
Example #30
0
async def test_get_python():
    """Test simple get."""
    url = 'https://www.python.org/'

    client = aiosonic.HTTPClient()
    res = await client.get(
        url,
        headers={
            'user-agent':
            ('Mozilla/5.0 (Macintosh; Intel Mac OS X 10.14; rv:70.0)'
             ' Gecko/20100101 Firefox/70.0')
        },
        http2=True)
    assert res.status_code == 200
    assert '<title>Welcome to Python.org</title>' in await res.text()