Example #1
0
async def test_client_context_manager_response(method, app, loop):
    async with _TestClient(_TestServer(app), loop=loop) as client:
        async with getattr(client, method)('/') as resp:
            assert resp.status == 200
            if method != 'head':
                text = await resp.text()
                assert "Hello, world" in text
Example #2
0
async def test_test_server_context_manager(loop):
    app = _create_example_app()
    async with _TestServer(app, loop=loop) as server:
        client = aiohttp.ClientSession(loop=loop)
        resp = await client.head(server.make_url('/'))
        assert resp.status == 200
        resp.close()
        await client.close()
Example #3
0
async def test_server_make_url_yarl_compatibility(loop):
    app = _create_example_app()
    async with _TestServer(app, loop=loop) as server:
        make_url = server.make_url
        assert make_url(URL('/foo')) == make_url('/foo')
        with pytest.raises(AssertionError):
            make_url('http://foo.com')
        with pytest.raises(AssertionError):
            make_url(URL('http://foo.com'))
Example #4
0
async def test_aiohttp_client_close_is_idempotent() -> None:
    """
    a test client, called multiple times, should
    not attempt to close the server again.
    """
    app = _create_example_app()
    client = _TestClient(_TestServer(app))
    await client.close()
    await client.close()
Example #5
0
async def test_test_client_props(loop):
    app = _create_example_app()
    client = _TestClient(_TestServer(app, host='127.0.0.1', loop=loop),
                         loop=loop)
    assert client.host == '127.0.0.1'
    assert client.port is None
    async with client:
        assert isinstance(client.port, int)
        assert client.server is not None
    assert client.port is None
Example #6
0
def test_aiohttp_client_close_is_idempotent():
    """
    a test client, called multiple times, should
    not attempt to close the server again.
    """
    loop = setup_test_loop()
    app = _create_example_app()
    client = _TestClient(_TestServer(app, loop=loop), loop=loop)
    loop.run_until_complete(client.close())
    loop.run_until_complete(client.close())
    teardown_test_loop(loop)
Example #7
0
def test_test_server_context_manager(loop):
    app = _create_example_app(loop)
    with _TestServer(app) as server:
        @asyncio.coroutine
        def go():
            client = aiohttp.ClientSession(loop=loop)
            resp = yield from client.head(server.make_url('/'))
            assert resp.status == 200
            resp.close()
            yield from client.close()

        loop.run_until_complete(go())
Example #8
0
def test_server_with_create_test_teardown():
    with loop_context() as loop:
        app = _create_example_app()
        with _TestClient(_TestServer(app, loop=loop), loop=loop) as client:

            @asyncio.coroutine
            def test_get_route():
                resp = yield from client.request("GET", "/")
                assert resp.status == 200
                text = yield from resp.text()
                assert _hello_world_str == text

            loop.run_until_complete(test_get_route())
Example #9
0
async def test_custom_port(loop, app, aiohttp_unused_port):
    port = aiohttp_unused_port()
    client = _TestClient(_TestServer(app, loop=loop, port=port), loop=loop)
    await client.start_server()

    assert client.server.port == port

    resp = await client.get('/')
    assert resp.status == 200
    text = await resp.text()
    assert _hello_world_str == text

    await client.close()
Example #10
0
def test_auto_gzip_decompress():
    with loop_context() as loop:
        app = _create_example_app()
        with _TestClient(_TestServer(app, loop=loop), loop=loop) as client:

            @asyncio.coroutine
            def test_get_route():
                nonlocal client
                resp = yield from client.request("GET", "/gzip_hello")
                assert resp.status == 200
                data = yield from resp.read()
                assert data == _hello_world_bytes

            loop.run_until_complete(test_get_route())
Example #11
0
def test_with_client_fails(loop):
    app = _create_example_app()
    with pytest.raises(TypeError):
        with _TestClient(_TestServer(app, loop=loop), loop=loop):
            pass
Example #12
0
def test_client(loop, app):
    client = _TestClient(_TestServer(app, loop=loop), loop=loop)
    loop.run_until_complete(client.start_server())
    yield client
    loop.run_until_complete(client.close())
Example #13
0
async def test_server_context_manager(app, loop):
    async with _TestServer(app, loop=loop) as server:
        async with aiohttp.ClientSession(loop=loop) as client:
            async with client.head(server.make_url('/')) as resp:
                assert resp.status == 200
Example #14
0
def test_client_scheme_mutually_exclusive_with_server(loop):
    app = _create_example_app(loop)
    server = _TestServer(app)
    with pytest.raises(ValueError):
        _TestClient(server, scheme='http')
Example #15
0
def test_client_host_mutually_exclusive_with_server(loop):
    app = _create_example_app(loop)
    server = _TestServer(app)
    with pytest.raises(ValueError):
        _TestClient(server, host='127.0.0.1')
Example #16
0
def test_with_test_server_fails(loop) -> None:
    app = _create_example_app()
    with pytest.raises(TypeError):
        with _TestServer(app, loop=loop):
            pass
Example #17
0
 async def make_client():
     return _TestClient(_TestServer(app, loop=loop), loop=loop)