Beispiel #1
0
    def test_callback(self, mocker, aiohttp_session_mock):
        # Setup
        import asyncio

        expected_response = mocker.Mock(spec=aiohttp_.aiohttp.ClientResponse)

        @asyncio.coroutine
        def request(*args, **kwargs):
            return expected_response

        aiohttp_session_mock.request = request
        client = aiohttp_.AiohttpClient(aiohttp_session_mock)
        client._sync_callback_adapter = asyncio.coroutine

        # Run
        globals_ = dict(globals(), client=client)
        locals_ = {"asyncio": asyncio}
        exec(
            """@asyncio.coroutine
def call():
    response = yield from client.send((1, 2, {}))
    response = yield from client.apply_callback(lambda x: 2, response)
    return response
""",
            globals_,
            locals_,
        )

        loop = asyncio.get_event_loop()
        value = loop.run_until_complete(
            asyncio.ensure_future(locals_["call"]()))

        # Verify
        assert value == 2
Beispiel #2
0
    async def test_request_send(self, mocker, aiohttp_session_mock):
        # Setup
        expected_response = mocker.Mock()

        async def request(*args, **kwargs):
            return expected_response

        aiohttp_session_mock.request = request
        client = aiohttp_.AiohttpClient(aiohttp_session_mock)

        # Run
        response = await client.send((1, 2, {}))

        # Verify
        assert response == expected_response
Beispiel #3
0
    def test_request_send(self, aiohttp_session_mock):
        # Setup
        import asyncio

        @asyncio.coroutine
        def request(*args, **kwargs):
            return 0

        aiohttp_session_mock.request = request
        client = aiohttp_.AiohttpClient(aiohttp_session_mock)
        request = aiohttp_.Request(client)

        # Run
        response = request.send(1, 2, {})
        loop = asyncio.get_event_loop()
        value = loop.run_until_complete(asyncio.ensure_future(response))

        # Verify
        assert value == 0
Beispiel #4
0
    def test_request_send(self, mocker, aiohttp_session_mock):
        # Setup
        import asyncio

        expected_response = mocker.Mock()

        @asyncio.coroutine
        def request(*args, **kwargs):
            return expected_response

        aiohttp_session_mock.request = request
        client = aiohttp_.AiohttpClient(aiohttp_session_mock)

        # Run
        response = client.send((1, 2, {}))
        loop = asyncio.get_event_loop()
        value = loop.run_until_complete(asyncio.ensure_future(response))

        # Verify
        assert value == expected_response
Beispiel #5
0
    def test_callback(self, aiohttp_session_mock):
        # Setup
        import asyncio

        @asyncio.coroutine
        def request(*args, **kwargs):
            return 2

        aiohttp_session_mock.request = request
        client = aiohttp_.AiohttpClient(aiohttp_session_mock)
        client._sync_callback_adapter = asyncio.coroutine
        request = aiohttp_.Request(client)

        # Run
        request.add_callback(lambda x: 2)
        response = request.send(1, 2, {})
        loop = asyncio.get_event_loop()
        value = loop.run_until_complete(asyncio.ensure_future(response))

        # Verify
        assert value == 2
Beispiel #6
0
    async def test_callback(self, mocker, aiohttp_session_mock):
        # Setup
        expected_response = mocker.Mock(spec=aiohttp_.aiohttp.ClientResponse)
        expected_response.text = AsyncMock()

        async def request(*args, **kwargs):
            return expected_response

        aiohttp_session_mock.request = request
        client = aiohttp_.AiohttpClient(aiohttp_session_mock)

        # Run
        async def call():
            response = await client.send((1, 2, {}))
            response = await client.apply_callback(lambda x: 2, response)
            return response

        value = await call()

        # Verify
        assert value == 2
        assert expected_response.text.called
Beispiel #7
0
 def test_create_request(self, aiohttp_session_mock):
     aiohttp = aiohttp_.AiohttpClient(aiohttp_session_mock)
     assert isinstance(aiohttp.create_request(), aiohttp_.Request)