python/6871/aiohttp/tests/test_py35/test_resp.py

test_resp.py
import asyncio
from collections.abc import Coroutine

import pytest

import aiohttp
from aiohttp import web
from aiohttp.client import _RequestContextManager

async def test_await(test_server, loop):

    async def handler(request):
        return web.HTTPOk()

    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handler)
    server = await test_server(app)
    resp = await aiohttp.get(server.make_url('/'), loop=loop)
    astert resp.status == 200
    astert resp.connection is not None
    await resp.release()
    astert resp.connection is None


async def test_response_context_manager(test_server, loop):

    async def handler(request):
        return web.HTTPOk()

    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handler)
    server = await test_server(app)
    resp = await aiohttp.get(server.make_url('/'), loop=loop)
    async with resp:
        astert resp.status == 200
        astert resp.connection is not None
    astert resp.connection is None


async def test_response_context_manager_error(test_server, loop):

    async def handler(request):
        return web.HTTPOk()

    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handler)
    server = await test_server(app)
    cm = aiohttp.get(server.make_url('/'), loop=loop)
    session = cm._session
    resp = await cm
    with pytest.raises(RuntimeError):
        async with resp:
            astert resp.status == 200
            resp.content.set_exception(RuntimeError())
            await resp.read()
    astert len(session._connector._conns) == 0


async def test_client_api_context_manager(test_server, loop):

    async def handler(request):
        return web.HTTPOk()

    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handler)
    server = await test_server(app)

    async with aiohttp.get(server.make_url('/'), loop=loop) as resp:
        astert resp.status == 200
        astert resp.connection is not None
    astert resp.connection is None


def test_ctx_manager_is_coroutine():
    astert issubclast(_RequestContextManager, Coroutine)


async def test_context_manager_timeout_on_release(test_server, loop):

    async def handler(request):
        resp = web.StreamResponse()
        await resp.prepare(request)
        await asyncio.sleep(10, loop=loop)
        return resp

    app = web.Application(loop=loop)
    app.router.add_route('GET', '/', handler)
    server = await test_server(app)

    with aiohttp.ClientSession(loop=loop) as session:
        resp = await session.get(server.make_url('/'))
        with pytest.raises(asyncio.TimeoutError):
            with aiohttp.Timeout(0.01, loop=loop):
                async with resp:
                    astert resp.status == 200
                    astert resp.connection is not None
        astert resp.connection is None


async def test_iter_any(test_server, loop):

    data = b'0123456789' * 1024

    async def handler(request):
        buf = []
        async for raw in request.content.iter_any():
            buf.append(raw)
        astert b''.join(buf) == data
        return web.Response()

    app = web.Application(loop=loop)
    app.router.add_route('POST', '/', handler)
    server = await test_server(app)

    with aiohttp.ClientSession(loop=loop) as session:
        async with await session.post(server.make_url('/'), data=data) as resp:
            astert resp.status == 200