Beispiel #1
0
    def handle(self, method, request):
        """Handle a request

        :param method: string - HTTP Verb
        :param request: aiohttp.client_reqrep.ClientRequest

        :returns: aiohttp.client_reqrep.ClientResponse

        Note: Returns an HTTP 405 if the HTTP Verb is not
              supported
        """

        # If the method has a registered handler, then
        # return it. Otherwise, create a 405 response
        if method in self._method_handlers:
            handler = self._method_handlers[method]
            # Callbacks must be callables
            if hasattr(handler, '__call__'):
                return self._method_handlers[method](_request)
            else:
                return handler

        else:
            response = ClientResponse(method, self.uri, host='aiohttp_mock')
            response.status = 405
            response.reason = 'Method Not Supported'
            response._should_close = False
            response._headers = cidict({
                'x-agent': 'aiohttp-mock',
                'content-length': 0
            })
            return response
Beispiel #2
0
 def test_repr_non_ascii_reason(self):
     response = ClientResponse('get', 'http://fake-host.org/path')
     response.reason = '\u03bb'
     self.assertIn(
         "<ClientResponse(http://fake-host.org/path) [None \\u03bb]>",
         repr(response))
     response.close()
    def test_https_connect_runtime_error(self, ClientRequestMock):
        loop_mock = _create_mocked_loop()
        proxy_req = ClientRequest('GET', 'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_resp._loop = loop_mock
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(start_mock, unittest.mock.Mock(status=200))

        connector = aiohttp.ProxyConnector(
            'http://proxy.example.com', loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        tr.get_extra_info.return_value = None
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'https://www.python.org', loop=self.loop)
        with self.assertRaisesRegex(
                RuntimeError, "Transport does not expose socket instance"):
            self.loop.run_until_complete(connector._create_connection(req))

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
async def test_response_read_triggers_callback(loop, session) -> None:
    trace = mock.Mock()
    trace.send_response_chunk_received = make_mocked_coro()
    response_body = b'This is response'

    response = ClientResponse(
        'get', URL('http://def-cl-resp.org'),
        request_info=mock.Mock,
        writer=mock.Mock(),
        continue100=None,
        timer=TimerNoop(),
        loop=loop,
        session=session,
        traces=[trace]
    )

    def side_effect(*args, **kwargs):
        fut = loop.create_future()
        fut.set_result(response_body)
        return fut

    response._headers = {
        'Content-Type': 'application/json;charset=cp1251'}
    content = response.content = mock.Mock()
    content.read.side_effect = side_effect

    res = await response.read()
    assert res == response_body
    assert response._connection is None

    assert trace.send_response_chunk_received.called
    assert (
        trace.send_response_chunk_received.call_args ==
        mock.call(response_body)
    )
Beispiel #5
0
def test_repr(loop, session):
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response._post_init(loop, session)
    response.status = 200
    response.reason = 'Ok'
    assert '<ClientResponse(http://def-cl-resp.org) [200 Ok]>'\
        in repr(response)
def test_response_links_comma_separated(loop, session) -> None:
    url = URL('http://def-cl-resp.org/')
    response = ClientResponse('get', url,
                              request_info=mock.Mock(),
                              writer=mock.Mock(),
                              continue100=None,
                              timer=TimerNoop(),
                              traces=[],
                              loop=loop,
                              session=session)
    response._headers = CIMultiDict([
        (
            "Link",
            ('<http://example.com/page/1.html>; rel=next, '
             '<http://example.com/>; rel=home')
        )
    ])
    assert (
        response.links ==
        {'next':
         {'url': URL('http://example.com/page/1.html'),
          'rel': 'next'},
         'home':
         {'url': URL('http://example.com/'),
          'rel': 'home'}
         }
    )
async def test_json_override_encoding(loop, session) -> None:
    response = ClientResponse('get', URL('http://def-cl-resp.org'),
                              request_info=mock.Mock(),
                              writer=mock.Mock(),
                              continue100=None,
                              timer=TimerNoop(),
                              traces=[],
                              loop=loop,
                              session=session)

    def side_effect(*args, **kwargs):
        fut = loop.create_future()
        fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
        return fut

    response._headers = {
        'Content-Type': 'application/json;charset=utf8'}
    content = response.content = mock.Mock()
    content.read.side_effect = side_effect
    response.get_encoding = mock.Mock()

    res = await response.json(encoding='cp1251')
    assert res == {'тест': 'пройден'}
    assert response._connection is None
    assert not response.get_encoding.called
Beispiel #8
0
    def test_https_connect_resp_start_error(self, ClientRequestMock):
        proxy_req = ClientRequest('GET', URL('http://proxy.example.com'),
                                  loop=self.loop)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', URL('http://proxy.example.com'))
        proxy_resp._loop = self.loop
        proxy_req.send = send_mock = mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = make_mocked_coro(
            raise_exception=OSError("error message"))

        connector = aiohttp.TCPConnector(loop=self.loop)
        connector._resolve_host = make_mocked_coro(
            [{'hostname': 'hostname', 'host': '127.0.0.1', 'port': 80,
              'family': socket.AF_INET, 'proto': 0, 'flags': 0}])

        tr, proto = mock.Mock(), mock.Mock()
        tr.get_extra_info.return_value = None
        self.loop.create_connection = make_mocked_coro((tr, proto))

        req = ClientRequest(
            'GET', URL('https://www.python.org'),
            proxy=URL('http://proxy.example.com'),
            loop=self.loop,
        )
        with self.assertRaisesRegex(OSError, "error message"):
            self.loop.run_until_complete(connector._create_connection(req))
async def test_text_bad_encoding(loop, session) -> None:
    response = ClientResponse('get', URL('http://def-cl-resp.org'),
                              request_info=mock.Mock(),
                              writer=mock.Mock(),
                              continue100=None,
                              timer=TimerNoop(),
                              traces=[],
                              loop=loop,
                              session=session)

    def side_effect(*args, **kwargs):
        fut = loop.create_future()
        fut.set_result('{"тестkey": "пройденvalue"}'.encode('cp1251'))
        return fut

    # lie about the encoding
    response._headers = {
        'Content-Type': 'application/json;charset=utf-8'}
    content = response.content = mock.Mock()
    content.read.side_effect = side_effect
    with pytest.raises(UnicodeDecodeError):
        await response.text()
    # only the valid utf-8 characters will be returned
    res = await response.text(errors='ignore')
    assert res == '{"key": "value"}'
    assert response._connection is None
Beispiel #10
0
    def test_https_connect_resp_start_error(self, ClientRequestMock):
        loop_mock = _create_mocked_loop()
        proxy_req = ClientRequest('GET', 'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_resp._loop = loop_mock
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(start_mock, OSError("error message"))

        connector = aiohttp.TCPConnector(loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        tr.get_extra_info.return_value = None
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest(
            'GET', 'https://www.python.org',
            proxy='http://proxy.example.com',
            loop=self.loop,
        )
        with self.assertRaisesRegex(OSError, "error message"):
            self.loop.run_until_complete(connector._create_connection(req))
def test_content_disposition_no_parameters():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.headers = {'Content-Disposition': 'attachment'}

    assert 'attachment' == response.content_disposition.type
    assert response.content_disposition.filename is None
    assert {} == response.content_disposition.parameters
    def test_https_connect(self, ClientRequestMock):
        loop_mock = unittest.mock.Mock()
        proxy_req = ClientRequest('GET', 'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_resp._loop = loop_mock
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(start_mock, unittest.mock.Mock(status=200))

        connector = aiohttp.ProxyConnector(
            'http://proxy.example.com', loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'https://www.python.org', loop=self.loop)
        self.loop.run_until_complete(connector._create_connection(req))

        self.assertEqual(req.path, '/')
        self.assertEqual(proxy_req.method, 'CONNECT')
        self.assertEqual(proxy_req.path, 'www.python.org:443')
        tr.pause_reading.assert_called_once_with()
        tr.get_extra_info.assert_called_with('socket', default=None)

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
Beispiel #13
0
async def test_client_protocol_readuntil_eof(loop):
    proto = ResponseHandler(loop=loop)
    transport = mock.Mock()
    proto.connection_made(transport)
    conn = mock.Mock()
    conn.protocol = proto

    proto.data_received(b'HTTP/1.1 200 Ok\r\n\r\n')

    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response._post_init(loop, mock.Mock())
    await response.start(conn, read_until_eof=True)

    assert not response.content.is_eof()

    proto.data_received(b'0000')
    data = await response.content.readany()
    assert data == b'0000'

    proto.data_received(b'1111')
    data = await response.content.readany()
    assert data == b'1111'

    proto.connection_lost(None)
    assert response.content.is_eof()
    def test_https_connect_http_proxy_error(self, ClientRequestMock):
        loop_mock = unittest.mock.Mock()
        proxy_req = ClientRequest('GET', 'http://proxy.example.com',
                                  loop=loop_mock)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', 'http://proxy.example.com')
        proxy_resp._loop = loop_mock
        proxy_req.send = send_mock = unittest.mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = start_mock = unittest.mock.Mock()
        self._fake_coroutine(
            start_mock, unittest.mock.Mock(status=400, reason='bad request'))

        connector = aiohttp.ProxyConnector(
            'http://proxy.example.com', loop=loop_mock)

        tr, proto = unittest.mock.Mock(), unittest.mock.Mock()
        tr.get_extra_info.return_value = None
        self._fake_coroutine(loop_mock.create_connection, (tr, proto))

        req = ClientRequest('GET', 'https://www.python.org', loop=self.loop)
        with self.assertRaisesRegex(
                aiohttp.HttpProxyError, "400, message='bad request'"):
            self.loop.run_until_complete(connector._create_connection(req))

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
Beispiel #15
0
    def test_https_connect(self, ClientRequestMock):
        proxy_req = ClientRequest('GET', URL('http://proxy.example.com'),
                                  loop=self.loop)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', URL('http://proxy.example.com'))
        proxy_resp._loop = self.loop
        proxy_req.send = send_mock = mock.Mock()
        send_mock.return_value = proxy_resp
        proxy_resp.start = make_mocked_coro(mock.Mock(status=200))

        connector = aiohttp.TCPConnector(loop=self.loop)
        connector._resolve_host = make_mocked_coro(
            [{'hostname': 'hostname', 'host': '127.0.0.1', 'port': 80,
              'family': socket.AF_INET, 'proto': 0, 'flags': 0}])

        tr, proto = mock.Mock(), mock.Mock()
        self.loop.create_connection = make_mocked_coro((tr, proto))

        req = ClientRequest(
            'GET', URL('https://www.python.org'),
            proxy=URL('http://proxy.example.com'),
            loop=self.loop,
        )
        self.loop.run_until_complete(connector._create_connection(req))

        self.assertEqual(req.url.path, '/')
        self.assertEqual(proxy_req.method, 'CONNECT')
        self.assertEqual(proxy_req.url, URL('https://www.python.org'))
        tr.pause_reading.assert_called_once_with()
        tr.get_extra_info.assert_called_with('socket', default=None)

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
def test_raise_for_status_4xx():
    response = ClientResponse("get", URL("http://def-cl-resp.org"))
    response.status = 409
    response.reason = "CONFLICT"
    with pytest.raises(aiohttp.HttpProcessingError) as cm:
        response.raise_for_status()
    assert str(cm.value.code) == "409"
    assert str(cm.value.message) == "CONFLICT"
def test_get_encoding_unknown(loop):
    response = ClientResponse("get", URL("http://def-cl-resp.org"))
    response._post_init(loop)

    response.headers = {"Content-Type": "application/json"}
    with mock.patch("aiohttp.client_reqrep.chardet") as m_chardet:
        m_chardet.detect.return_value = {"encoding": None}
        assert response._get_encoding() == "utf-8"
Beispiel #18
0
def test_get_encoding_unknown(loop, session):
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response._post_init(loop, session)

    response.headers = {'Content-Type': 'application/json'}
    with mock.patch('aiohttp.client_reqrep.chardet') as m_chardet:
        m_chardet.detect.return_value = {'encoding': None}
        assert response._get_encoding() == 'utf-8'
Beispiel #19
0
def test_raise_for_status_4xx():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.status = 409
    response.reason = 'CONFLICT'
    with pytest.raises(aiohttp.ClientResponseError) as cm:
        response.raise_for_status()
    assert str(cm.value.code) == '409'
    assert str(cm.value.message) == "CONFLICT"
async def test_json_no_content(loop, session):
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response._post_init(loop, session)
    response.headers = {
        'Content-Type': 'data/octet-stream'}
    response._content = b''

    res = await response.json(content_type=None)
    assert res is None
def test_release(loop):
    response = ClientResponse("get", URL("http://def-cl-resp.org"))
    response._post_init(loop)
    fut = helpers.create_future(loop)
    fut.set_result(b"")
    content = response.content = mock.Mock()
    content.readany.return_value = fut

    yield from response.release()
    assert response._connection is None
def test_content_disposition_full():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.headers = {'Content-Disposition':
                        'attachment; filename="archive.tar.gz"; foo=bar'}

    assert 'attachment' == response.content_disposition.type
    assert 'bar' == response.content_disposition.parameters["foo"]
    assert 'archive.tar.gz' == response.content_disposition.filename
    with pytest.raises(TypeError):
        response.content_disposition.parameters["foo"] = "baz"
Beispiel #23
0
def test_release(loop, session):
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response._post_init(loop, session)
    fut = helpers.create_future(loop)
    fut.set_result(b'')
    content = response.content = mock.Mock()
    content.readany.return_value = fut

    response.release()
    assert response._connection is None
def test_read_and_release_connection_with_error(loop):
    response = ClientResponse("get", URL("http://def-cl-resp.org"))
    response._post_init(loop)
    content = response.content = mock.Mock()
    content.read.return_value = helpers.create_future(loop)
    content.read.return_value.set_exception(ValueError)

    with pytest.raises(ValueError):
        yield from response.read()
    assert response._closed
Beispiel #25
0
def test_response_eof_upgraded(loop, session):
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response._post_init(loop, session)

    conn = response._connection = mock.Mock()
    conn.protocol.upgraded = True

    response._response_eof()
    assert not conn.release.called
    assert response._connection is conn
async def test_read_and_release_connection_with_error(loop, session):
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response._post_init(loop, session)
    content = response.content = mock.Mock()
    content.read.return_value = loop.create_future()
    content.read.return_value.set_exception(ValueError)

    with pytest.raises(ValueError):
        await response.read()
    assert response._closed
Beispiel #27
0
    def test_https_auth(self, ClientRequestMock) -> None:
        proxy_req = ClientRequest('GET', URL('http://proxy.example.com'),
                                  auth=aiohttp.helpers.BasicAuth('user',
                                                                 'pass'),
                                  loop=self.loop)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get', URL('http://proxy.example.com'),
                                    request_info=mock.Mock(),
                                    writer=mock.Mock(),
                                    continue100=None,
                                    timer=TimerNoop(),
                                    traces=[],
                                    loop=self.loop,
                                    session=mock.Mock())
        proxy_req.send = make_mocked_coro(proxy_resp)
        proxy_resp.start = make_mocked_coro(mock.Mock(status=200))

        async def make_conn():
            return aiohttp.TCPConnector()
        connector = self.loop.run_until_complete(make_conn())
        connector._resolve_host = make_mocked_coro(
            [{'hostname': 'hostname', 'host': '127.0.0.1', 'port': 80,
              'family': socket.AF_INET, 'proto': 0, 'flags': 0}])

        tr, proto = mock.Mock(), mock.Mock()
        self.loop.create_connection = make_mocked_coro((tr, proto))

        self.assertIn('AUTHORIZATION', proxy_req.headers)
        self.assertNotIn('PROXY-AUTHORIZATION', proxy_req.headers)

        req = ClientRequest(
            'GET', URL('https://www.python.org'),
            proxy=URL('http://proxy.example.com'),
            loop=self.loop
        )
        self.assertNotIn('AUTHORIZATION', req.headers)
        self.assertNotIn('PROXY-AUTHORIZATION', req.headers)
        self.loop.run_until_complete(
            connector._create_connection(req, None, aiohttp.ClientTimeout()))

        self.assertEqual(req.url.path, '/')
        self.assertNotIn('AUTHORIZATION', req.headers)
        self.assertNotIn('PROXY-AUTHORIZATION', req.headers)
        self.assertNotIn('AUTHORIZATION', proxy_req.headers)
        self.assertIn('PROXY-AUTHORIZATION', proxy_req.headers)

        connector._resolve_host.assert_called_with(
            'proxy.example.com',
            80,
            traces=mock.ANY)

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
    def test_del(self):
        response = ClientResponse('get', 'http://python.org')
        response._post_init(self.loop)

        connection = unittest.mock.Mock()
        response._setup_connection(connection)
        with self.assertWarns(ResourceWarning):
            del response
            gc.collect()

        connection.close.assert_called_with()
async def test_json_invalid_content_type(loop, session):
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response._post_init(loop, session)
    response.headers = {
        'Content-Type': 'data/octet-stream'}
    response._content = b''

    with pytest.raises(aiohttp.ContentTypeError) as info:
        await response.json()

    assert info.value.request_info == response.request_info
def test_wait_for_100_1(loop, session) -> None:
    response = ClientResponse(
        'get', URL('http://python.org'), continue100=object(),
        request_info=mock.Mock(),
        writer=mock.Mock(),
        timer=TimerNoop(),
        traces=[],
        loop=loop,
        session=session)
    assert response._continue is not None
    response.close()
Beispiel #31
0
def test_resp_host():
    response = ClientResponse('get', URL('http://del-cl-resp.org'))
    with pytest.warns(DeprecationWarning):
        assert 'del-cl-resp.org' == response.host
Beispiel #32
0
    def __init__(self, *args, **kwargs):
        super().__init__(ClientResponse(*args, **kwargs))

        # this matches ClientResponse._body
        self._self_body = None
def test_resp_host():
    response = ClientResponse('get', URL('http://del-cl-resp.org'))
    assert 'del-cl-resp.org' == response.host
Beispiel #34
0
class TestClientResponse(unittest.TestCase):

    def setUp(self):
        self.loop = asyncio.new_event_loop()
        asyncio.set_event_loop(None)

        self.connection = unittest.mock.Mock()
        self.stream = aiohttp.StreamParser(loop=self.loop)
        self.response = ClientResponse('get', 'http://def-cl-resp.org')
        self.response._post_init(self.loop)
        self.response._setup_connection(self.connection)

    def tearDown(self):
        self.response.close()
        self.loop.close()
        gc.collect()

    def test_del(self):
        response = ClientResponse('get', 'http://del-cl-resp.org')
        response._post_init(self.loop)

        connection = unittest.mock.Mock()
        response._setup_connection(connection)
        self.loop.set_exception_handler(lambda loop, ctx: None)

        with self.assertWarns(ResourceWarning):
            del response
            gc.collect()

        connection.close.assert_called_with()

    def test_close(self):
        self.response._connection = self.connection
        self.response.close()
        self.assertIsNone(self.response.connection)
        self.response.close()
        self.response.close()

    def test_wait_for_100_1(self):
        response = ClientResponse(
            'get', 'http://python.org', continue100=object())
        response._post_init(self.loop)
        self.assertTrue(response.waiting_for_continue())
        response.close()

    def test_wait_for_100_2(self):
        response = ClientResponse(
            'get', 'http://python.org')
        response._post_init(self.loop)
        self.assertFalse(response.waiting_for_continue())
        response.close()

    def test_repr(self):
        self.response.status = 200
        self.response.reason = 'Ok'
        self.assertIn(
            '<ClientResponse(http://def-cl-resp.org) [200 Ok]>',
            repr(self.response))

    def test_read_and_release_connection(self):
        def side_effect(*args, **kwargs):
            fut = asyncio.Future(loop=self.loop)
            fut.set_result(b'payload')
            return fut
        content = self.response.content = unittest.mock.Mock()
        content.read.side_effect = side_effect

        res = self.loop.run_until_complete(self.response.read())
        self.assertEqual(res, b'payload')
        self.assertIsNone(self.response._connection)

    def test_read_and_release_connection_with_error(self):
        content = self.response.content = unittest.mock.Mock()
        content.read.return_value = asyncio.Future(loop=self.loop)
        content.read.return_value.set_exception(ValueError)

        self.assertRaises(
            ValueError,
            self.loop.run_until_complete, self.response.read())
        self.assertTrue(self.response._closed)

    def test_release(self):
        fut = asyncio.Future(loop=self.loop)
        fut.set_result(b'')
        content = self.response.content = unittest.mock.Mock()
        content.readany.return_value = fut

        self.loop.run_until_complete(self.response.release())
        self.assertIsNone(self.response._connection)

    def test_read_and_close(self):
        self.response.read = unittest.mock.Mock()
        self.response.read.return_value = asyncio.Future(loop=self.loop)
        self.response.read.return_value.set_result(b'data')

        with self.assertWarns(DeprecationWarning):
            res = self.loop.run_until_complete(self.response.read_and_close())
        self.assertEqual(res, b'data')
        self.assertTrue(self.response.read.called)

    def test_read_decode_deprecated(self):
        self.response._content = b'data'
        self.response.json = unittest.mock.Mock()
        self.response.json.return_value = asyncio.Future(loop=self.loop)
        self.response.json.return_value.set_result('json')

        with self.assertWarns(DeprecationWarning):
            res = self.loop.run_until_complete(self.response.read(decode=True))
        self.assertEqual(res, 'json')
        self.assertTrue(self.response.json.called)

    def test_text(self):
        def side_effect(*args, **kwargs):
            fut = asyncio.Future(loop=self.loop)
            fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
            return fut
        self.response.headers = {
            'CONTENT-TYPE': 'application/json;charset=cp1251'}
        content = self.response.content = unittest.mock.Mock()
        content.read.side_effect = side_effect

        res = self.loop.run_until_complete(self.response.text())
        self.assertEqual(res, '{"тест": "пройден"}')
        self.assertIsNone(self.response._connection)

    def test_text_custom_encoding(self):
        def side_effect(*args, **kwargs):
            fut = asyncio.Future(loop=self.loop)
            fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
            return fut
        self.response.headers = {
            'CONTENT-TYPE': 'application/json'}
        content = self.response.content = unittest.mock.Mock()
        content.read.side_effect = side_effect
        self.response._get_encoding = unittest.mock.Mock()

        res = self.loop.run_until_complete(
            self.response.text(encoding='cp1251'))
        self.assertEqual(res, '{"тест": "пройден"}')
        self.assertIsNone(self.response._connection)
        self.assertFalse(self.response._get_encoding.called)

    def test_text_detect_encoding(self):
        def side_effect(*args, **kwargs):
            fut = asyncio.Future(loop=self.loop)
            fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
            return fut
        self.response.headers = {'CONTENT-TYPE': 'application/json'}
        content = self.response.content = unittest.mock.Mock()
        content.read.side_effect = side_effect

        self.loop.run_until_complete(self.response.read())
        res = self.loop.run_until_complete(self.response.text())
        self.assertEqual(res, '{"тест": "пройден"}')
        self.assertIsNone(self.response._connection)

    def test_text_after_read(self):
        def side_effect(*args, **kwargs):
            fut = asyncio.Future(loop=self.loop)
            fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
            return fut
        self.response.headers = {
            'CONTENT-TYPE': 'application/json;charset=cp1251'}
        content = self.response.content = unittest.mock.Mock()
        content.read.side_effect = side_effect

        res = self.loop.run_until_complete(self.response.text())
        self.assertEqual(res, '{"тест": "пройден"}')
        self.assertIsNone(self.response._connection)

    def test_json(self):
        def side_effect(*args, **kwargs):
            fut = asyncio.Future(loop=self.loop)
            fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
            return fut
        self.response.headers = {
            'CONTENT-TYPE': 'application/json;charset=cp1251'}
        content = self.response.content = unittest.mock.Mock()
        content.read.side_effect = side_effect

        res = self.loop.run_until_complete(self.response.json())
        self.assertEqual(res, {'тест': 'пройден'})
        self.assertIsNone(self.response._connection)

    def test_json_custom_loader(self):
        self.response.headers = {
            'CONTENT-TYPE': 'application/json;charset=cp1251'}
        self.response._content = b'data'

        def custom(content):
            return content + '-custom'

        res = self.loop.run_until_complete(self.response.json(loads=custom))
        self.assertEqual(res, 'data-custom')

    @unittest.mock.patch('aiohttp.client_reqrep.client_logger')
    def test_json_no_content(self, m_log):
        self.response.headers = {
            'CONTENT-TYPE': 'data/octet-stream'}
        self.response._content = b''

        res = self.loop.run_until_complete(self.response.json())
        self.assertIsNone(res)
        m_log.warning.assert_called_with(
            'Attempt to decode JSON with unexpected mimetype: %s',
            'data/octet-stream')

    def test_json_override_encoding(self):
        def side_effect(*args, **kwargs):
            fut = asyncio.Future(loop=self.loop)
            fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
            return fut
        self.response.headers = {
            'CONTENT-TYPE': 'application/json;charset=utf8'}
        content = self.response.content = unittest.mock.Mock()
        content.read.side_effect = side_effect
        self.response._get_encoding = unittest.mock.Mock()

        res = self.loop.run_until_complete(
            self.response.json(encoding='cp1251'))
        self.assertEqual(res, {'тест': 'пройден'})
        self.assertIsNone(self.response._connection)
        self.assertFalse(self.response._get_encoding.called)

    def test_json_detect_encoding(self):
        def side_effect(*args, **kwargs):
            fut = asyncio.Future(loop=self.loop)
            fut.set_result('{"тест": "пройден"}'.encode('cp1251'))
            return fut
        self.response.headers = {'CONTENT-TYPE': 'application/json'}
        content = self.response.content = unittest.mock.Mock()
        content.read.side_effect = side_effect

        res = self.loop.run_until_complete(self.response.json())
        self.assertEqual(res, {'тест': 'пройден'})
        self.assertIsNone(self.response._connection)

    def test_override_flow_control(self):
        class MyResponse(ClientResponse):
            flow_control_class = aiohttp.FlowControlDataQueue
        response = MyResponse('get', 'http://my-cl-resp.org')
        response._post_init(self.loop)
        response._setup_connection(self.connection)
        self.assertIsInstance(response.content, aiohttp.FlowControlDataQueue)
        response.close()

    @unittest.mock.patch('aiohttp.client_reqrep.chardet')
    def test_get_encoding_unknown(self, m_chardet):
        m_chardet.detect.return_value = {'encoding': None}

        self.response.headers = {'CONTENT-TYPE': 'application/json'}
        self.assertEqual(self.response._get_encoding(), 'utf-8')

    def test_close_deprecated(self):
        self.response._connection = self.connection
        with self.assertWarns(DeprecationWarning):
            self.response.close(force=False)
        self.assertIsNone(self.response._connection)
Beispiel #35
0
def test_content_disposition_no_header():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.headers = {}

    assert response.content_disposition is None
Beispiel #36
0
class ServiceBasicTest(TestCase):
    @patch('service_client.ClientSession')
    def setUp(self, mock_session):
        self.mock_session = mock_session
        self._mock_session()

        self.config = {}
        self.spec = {
            'testService1': {
                'path': '/path/to/service1',
                'method': 'get'
            },
            'testService2': {
                'path': '/path/to/service2',
                'method': 'post'
            },
            'testService3': {
                'path': '/path/to/service3',
                'method': 'put'
            },
            'testService4': {
                'path': '/path/to/service4',
                'method': 'get',
                'stream_response': True
            },
            'testService5': {
                'path': '/path/to/service5',
                'method': 'post',
                'stream_request': True
            }
        }

        self.plugin = FakePlugin()

        self.service_client = ServiceClient(rest_service_name="TestService",
                                            spec=self.spec,
                                            plugins=[self.plugin],
                                            config=self.config,
                                            base_path='http://foo.com/sdsd')

    def tearDown(self):
        pass

    def _mock_session(self):
        @coroutine
        def request(*args, **kwargs):
            self.request = {'args': args, 'kwargs': kwargs}
            self.response = ClientResponse('get', 'http://test.test')
            self.response._post_init(self.loop)
            self.response._content = b'bbbb'
            return self.response

        self.mock_session.request.side_effect = request
        self.mock_session.return_value = self.mock_session

    @coroutine
    def test_workflow_get(self):
        response = yield from self.service_client.call('testService1')
        self.assertEqual(response, self.response)

        self.assertIn('assign_service_client', self.plugin.calls,
                      "Assign service_client call")
        self.assertEqual(self.plugin.calls['assign_service_client']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['assign_service_client']['kwargs'],
            {'service_client': self.service_client})

        self.assertIn('prepare_session', self.plugin.calls,
                      "Prepare session call")
        self.assertEqual(self.plugin.calls['prepare_session']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_session']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service1',
                    'method': 'get',
                    'service_name': 'testService1'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'GET',
                    'url': 'http://foo.com/sdsd/path/to/service1'
                }
            })

        self.assertIn('prepare_path', self.plugin.calls, "Prepare path call")
        self.assertEqual(self.plugin.calls['prepare_path']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_path']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service1',
                    'method': 'get',
                    'service_name': 'testService1'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'GET',
                    'url': 'http://foo.com/sdsd/path/to/service1'
                },
                'path': 'http://foo.com/sdsd/path/to/service1'
            })

        self.assertIn('prepare_request_params', self.plugin.calls,
                      "Prepare request params call")
        self.assertEqual(self.plugin.calls['prepare_request_params']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['prepare_request_params']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service1',
                    'method': 'get',
                    'service_name': 'testService1'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'GET',
                    'url': 'http://foo.com/sdsd/path/to/service1'
                }
            })

        self.assertIn('prepare_payload', self.plugin.calls,
                      "Prepare request payload call")
        self.assertEqual(self.plugin.calls['prepare_payload']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_payload']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service1',
                    'method': 'get',
                    'service_name': 'testService1'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'GET',
                    'url': 'http://foo.com/sdsd/path/to/service1'
                },
                'payload': None
            })

        self.assertIn('before_request', self.plugin.calls,
                      "Before request call")
        self.assertEqual(self.plugin.calls['before_request']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['before_request']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service1',
                    'method': 'get',
                    'service_name': 'testService1'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'GET',
                    'url': 'http://foo.com/sdsd/path/to/service1'
                }
            })

        self.assertIn('on_response', self.plugin.calls, "On response call")
        self.assertEqual(self.plugin.calls['on_response']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['on_response']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service1',
                    'method': 'get',
                    'service_name': 'testService1'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'GET',
                    'url': 'http://foo.com/sdsd/path/to/service1'
                },
                'response': self.response
            })

        self.assertIn('on_parsed_response', self.plugin.calls,
                      "On parse response call")
        self.assertEqual(self.plugin.calls['on_parsed_response']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['on_parsed_response']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service1',
                    'method': 'get',
                    'service_name': 'testService1'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'GET',
                    'url': 'http://foo.com/sdsd/path/to/service1'
                },
                'response': self.response
            })

        self.assertNotIn('on_exception', self.plugin.calls,
                         "On exception call")
        self.assertNotIn('on_parse_exception', self.plugin.calls,
                         "On parse exception call")

    @coroutine
    def test_workflow_post(self):

        response = yield from self.service_client.call('testService2',
                                                       payload='aaaa')
        self.assertEqual(response, self.response)

        self.assertIn('assign_service_client', self.plugin.calls,
                      "Assign service_client call")
        self.assertEqual(self.plugin.calls['assign_service_client']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['assign_service_client']['kwargs'],
            {'service_client': self.service_client})

        self.assertIn('prepare_session', self.plugin.calls,
                      "Prepare session call")
        self.assertEqual(self.plugin.calls['prepare_session']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_session']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'data': 'aaaa',
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2'
                }
            })

        self.assertIn('prepare_path', self.plugin.calls, "Prepare path call")
        self.assertEqual(self.plugin.calls['prepare_path']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_path']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'path': 'http://foo.com/sdsd/path/to/service2'
            })

        self.assertIn('prepare_request_params', self.plugin.calls,
                      "Prepare request params call")
        self.assertEqual(self.plugin.calls['prepare_request_params']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['prepare_request_params']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                }
            })

        self.assertIn('prepare_payload', self.plugin.calls,
                      "Prepare request payload call")
        self.assertEqual(self.plugin.calls['prepare_payload']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_payload']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'payload': 'aaaa'
            })

        self.assertIn('before_request', self.plugin.calls,
                      "Before request call")
        self.assertEqual(self.plugin.calls['before_request']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['before_request']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                }
            })

        self.assertIn('on_response', self.plugin.calls, "On response call")
        self.assertEqual(self.plugin.calls['on_response']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['on_response']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'response': self.response
            })

        self.assertIn('on_parsed_response', self.plugin.calls,
                      "On parse response call")
        self.assertEqual(self.plugin.calls['on_parsed_response']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['on_parsed_response']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'response': self.response
            })

        self.assertEqual(self.response.data, b'bbbb')

        self.assertNotIn('on_exception', self.plugin.calls,
                         "On exception call")
        self.assertNotIn('on_parse_exception', self.plugin.calls,
                         "On parse exception call")

    @coroutine
    def test_workflow_post_direct_call(self):
        response = yield from self.service_client.testService2(payload='aaaa')
        self.assertEqual(response, self.response)

        self.assertIn('assign_service_client', self.plugin.calls,
                      "Assign service_client call")
        self.assertEqual(self.plugin.calls['assign_service_client']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['assign_service_client']['kwargs'],
            {'service_client': self.service_client})

        self.assertIn('prepare_session', self.plugin.calls,
                      "Prepare session call")
        self.assertEqual(self.plugin.calls['prepare_session']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_session']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'data': 'aaaa',
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2'
                }
            })

        self.assertIn('prepare_path', self.plugin.calls, "Prepare path call")
        self.assertEqual(self.plugin.calls['prepare_path']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_path']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'path': 'http://foo.com/sdsd/path/to/service2'
            })

        self.assertIn('prepare_request_params', self.plugin.calls,
                      "Prepare request params call")
        self.assertEqual(self.plugin.calls['prepare_request_params']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['prepare_request_params']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                }
            })

        self.assertIn('prepare_payload', self.plugin.calls,
                      "Prepare request payload call")
        self.assertEqual(self.plugin.calls['prepare_payload']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_payload']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'payload': 'aaaa'
            })

        self.assertIn('before_request', self.plugin.calls,
                      "Before request call")
        self.assertEqual(self.plugin.calls['before_request']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['before_request']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                }
            })

        self.assertIn('on_response', self.plugin.calls, "On response call")
        self.assertEqual(self.plugin.calls['on_response']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['on_response']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'response': self.response
            })

        self.assertIn('on_parsed_response', self.plugin.calls,
                      "On parse response call")
        self.assertEqual(self.plugin.calls['on_parsed_response']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['on_parsed_response']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'response': self.response
            })

        self.assertEqual(self.response.data, b'bbbb')

        self.assertNotIn('on_exception', self.plugin.calls,
                         "On exception call")
        self.assertNotIn('on_parse_exception', self.plugin.calls,
                         "On parse exception call")

    @coroutine
    def test_workflow_post_exception_response(self):
        @coroutine
        def request(*args, **kwargs):
            self.request = {'args': args, 'kwargs': kwargs}
            self.ex = Exception()
            raise self.ex

        self.mock_session.request.side_effect = request

        with self.assertRaises(Exception) as ex:
            yield from self.service_client.call('testService2', payload='aaaa')

        self.assertEqual(self.ex, ex.exception)

        self.assertIn('assign_service_client', self.plugin.calls,
                      "Assign service_client call")
        self.assertEqual(self.plugin.calls['assign_service_client']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['assign_service_client']['kwargs'],
            {'service_client': self.service_client})

        self.assertIn('prepare_session', self.plugin.calls,
                      "Prepare session call")
        self.assertEqual(self.plugin.calls['prepare_session']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_session']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'data': 'aaaa',
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2'
                }
            })

        self.assertIn('prepare_path', self.plugin.calls, "Prepare path call")
        self.assertEqual(self.plugin.calls['prepare_path']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_path']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'path': 'http://foo.com/sdsd/path/to/service2'
            })

        self.assertIn('prepare_request_params', self.plugin.calls,
                      "Prepare request params call")
        self.assertEqual(self.plugin.calls['prepare_request_params']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['prepare_request_params']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                }
            })

        self.assertIn('prepare_payload', self.plugin.calls,
                      "Prepare request payload call")
        self.assertEqual(self.plugin.calls['prepare_payload']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_payload']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'payload': 'aaaa'
            })

        self.assertIn('before_request', self.plugin.calls,
                      "Before request call")
        self.assertEqual(self.plugin.calls['before_request']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['before_request']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                }
            })

        self.assertIn('on_exception', self.plugin.calls, "On exception call")
        self.assertEqual(self.plugin.calls['on_exception']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['on_exception']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'ex': self.ex
            })

        self.assertNotIn('on_response', self.plugin.calls, "On response call")
        self.assertNotIn('on_parsed_response', self.plugin.calls,
                         "On parsed response call")
        self.assertNotIn('on_parse_exception', self.plugin.calls,
                         "On parse exception call")

    @coroutine
    def test_workflow_post_exception_parser(self):
        def parse(data, *args, **kwargs):
            self.ex = Exception()
            raise self.ex

        self.service_client.parser = parse

        with self.assertRaises(Exception) as ex:
            yield from self.service_client.call('testService2', payload='aaaa')
        self.assertEqual(self.ex, ex.exception)

        self.assertIn('assign_service_client', self.plugin.calls,
                      "Assign service_client call")
        self.assertEqual(self.plugin.calls['assign_service_client']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['assign_service_client']['kwargs'],
            {'service_client': self.service_client})

        self.assertIn('prepare_session', self.plugin.calls,
                      "Prepare session call")
        self.assertEqual(self.plugin.calls['prepare_session']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_session']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'data': 'aaaa',
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2'
                }
            })

        self.assertIn('prepare_path', self.plugin.calls, "Prepare path call")
        self.assertEqual(self.plugin.calls['prepare_path']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_path']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'path': 'http://foo.com/sdsd/path/to/service2'
            })

        self.assertIn('prepare_request_params', self.plugin.calls,
                      "Prepare request params call")
        self.assertEqual(self.plugin.calls['prepare_request_params']['args'],
                         ())
        self.assertDictEqual(
            self.plugin.calls['prepare_request_params']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                }
            })

        self.assertIn('prepare_payload', self.plugin.calls,
                      "Prepare request payload call")
        self.assertEqual(self.plugin.calls['prepare_payload']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['prepare_payload']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'payload': 'aaaa'
            })

        self.assertIn('before_request', self.plugin.calls,
                      "Before request call")
        self.assertEqual(self.plugin.calls['before_request']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['before_request']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                }
            })

        self.assertIn('on_response', self.plugin.calls, "On response call")
        self.assertEqual(self.plugin.calls['on_response']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['on_response']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'response': self.response
            })

        self.assertIn('on_parse_exception', self.plugin.calls,
                      "On parse exception call")
        self.assertEqual(self.plugin.calls['on_parse_exception']['args'], ())
        self.assertDictEqual(
            self.plugin.calls['on_parse_exception']['kwargs'], {
                'service_desc': {
                    'path': '/path/to/service2',
                    'method': 'post',
                    'service_name': 'testService2'
                },
                'session': self.mock_session,
                'request_params': {
                    'method': 'POST',
                    'url': 'http://foo.com/sdsd/path/to/service2',
                    'data': 'aaaa'
                },
                'response': self.response,
                'ex': self.ex
            })

        self.assertNotIn('on_exception', self.plugin.calls,
                         "On exception call")
        self.assertNotIn('on_parsed_response', self.plugin.calls,
                         "On parse response call")

    @coroutine
    def test_workflow_stream_response(self):
        def parse(data, *args, **kwargs):
            self.ex = Exception()
            raise self.ex

        self.service_client.parser = parse

        response = yield from self.service_client.call('testService4',
                                                       payload='aaaa')
        self.assertFalse(hasattr(response, 'data'))

    @coroutine
    def test_workflow_stream_request(self):
        def serializer(data, *args, **kwargs):
            self.ex = Exception()
            raise self.ex

        self.service_client.serializer = serializer

        yield from self.service_client.call('testService5', payload='aaaa')
Beispiel #37
0
 def test_wait_for_100_2(self):
     response = ClientResponse(
         'get', 'http://python.org')
     response._post_init(self.loop)
     self.assertFalse(response.waiting_for_continue())
     response.close()
def test_repr_non_ascii_url():
    response = ClientResponse('get', URL('http://fake-host.org/\u03bb'))
    assert "<ClientResponse(http://fake-host.org/%CE%BB) [None None]>"\
        in repr(response)
def test_repr_non_ascii_reason():
    response = ClientResponse('get', URL('http://fake-host.org/path'))
    response.reason = '\u03bb'
    assert "<ClientResponse(http://fake-host.org/path) [None \\u03bb]>"\
        in repr(response)
def test_wait_for_100_2(loop):
    response = ClientResponse('get', URL('http://python.org'))
    response._post_init(loop)
    assert response._continue is None
    response.close()
def test_charset_no_charset():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.headers = {'Content-Type': 'application/json'}

    assert response.charset is None
Beispiel #42
0
def test_redirect_history_in_exception() -> None:
    hist_url = "http://def-cl-resp.org"
    url = "http://def-cl-resp.org/index.htm"
    hist_headers = {
        "Content-Type": "application/json;charset=cp1251",
        "Location": url
    }
    headers = {"Content-Type": "application/json;charset=cp1251"}
    response = ClientResponse(
        "get",
        URL(url),
        request_info=RequestInfo(url, "get", headers),
        writer=mock.Mock(),
        continue100=None,
        timer=TimerNoop(),
        traces=[],
        loop=mock.Mock(),
        session=mock.Mock(),
    )
    response.status = 409
    response.reason = "CONFLICT"

    hist_response = ClientResponse(
        "get",
        URL(hist_url),
        request_info=RequestInfo(url, "get", headers),
        writer=mock.Mock(),
        continue100=None,
        timer=TimerNoop(),
        traces=[],
        loop=mock.Mock(),
        session=mock.Mock(),
    )

    hist_response._headers = hist_headers
    hist_response.status = 301
    hist_response.reason = "REDIRECT"

    response._history = [hist_response]
    with pytest.raises(aiohttp.ClientResponseError) as cm:
        response.raise_for_status()
    assert [hist_response] == cm.value.history
Beispiel #43
0
 def request(*args, **kwargs):
     self.request = {'args': args, 'kwargs': kwargs}
     self.response = ClientResponse('get', 'http://test.test')
     self.response._post_init(self.loop)
     self.response._content = b'bbbb'
     return self.response
Beispiel #44
0
def test_response_request_info_empty():
    url = 'http://def-cl-resp.org'
    response = ClientResponse(
        'get', URL(url),
    )
    assert response.request_info is None
Beispiel #45
0
def test_url_obj_deprecated():
    response = ClientResponse('get', URL('http://fake-host.org/'))
    with pytest.warns(DeprecationWarning):
        response.url_obj
Beispiel #46
0
 def __init__(self, *args, **kwargs):
     self._impl = ClientResponse(*args, **kwargs)
     self._body = None
Beispiel #47
0
def test_wait_for_100_1(loop, session):
    response = ClientResponse(
        'get', URL('http://python.org'), continue100=object())
    response._post_init(loop, session)
    assert response._continue is not None
    response.close()
Beispiel #48
0
 def run(conn):
     response = ClientResponse('get', URL('http://def-cl-resp.org'))
     response._post_init(loop)
     response._closed = False
     response._connection = conn
Beispiel #49
0
def test_content_disposition_cache():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.headers = {'Content-Disposition': 'attachment'}
    cd = response.content_disposition
    ClientResponse.headers = {'Content-Disposition': 'spam'}
    assert cd is response.content_disposition
Beispiel #50
0
 def __init__(self, *args, **kwargs):
     super().__init__(ClientResponse(*args, **kwargs))
def test_redirect_history_in_exception():
    hist_url = 'http://def-cl-resp.org'
    url = 'http://def-cl-resp.org/index.htm'
    hist_headers = {
        'Content-Type': 'application/json;charset=cp1251',
        'Location': url
    }
    headers = {'Content-Type': 'application/json;charset=cp1251'}
    response = ClientResponse('get',
                              URL(url),
                              request_info=RequestInfo(url, 'get', headers),
                              writer=mock.Mock(),
                              continue100=None,
                              timer=TimerNoop(),
                              traces=[],
                              loop=mock.Mock(),
                              session=mock.Mock())
    response.status = 409
    response.reason = 'CONFLICT'

    hist_response = ClientResponse('get',
                                   URL(hist_url),
                                   request_info=RequestInfo(
                                       url, 'get', headers),
                                   writer=mock.Mock(),
                                   continue100=None,
                                   timer=TimerNoop(),
                                   traces=[],
                                   loop=mock.Mock(),
                                   session=mock.Mock())

    hist_response._headers = hist_headers
    hist_response.status = 301
    hist_response.reason = 'REDIRECT'

    response._history = [hist_response]
    with pytest.raises(aiohttp.ClientResponseError) as cm:
        response.raise_for_status()
    assert [hist_response] == cm.value.history
Beispiel #52
0
 def test_repr_non_ascii_url(self):
     response = ClientResponse('get', 'http://fake-host.org/\u03bb')
     self.assertIn(
         "<ClientResponse(http://fake-host.org/\\u03bb) [None None]>",
         repr(response))
     response.close()
Beispiel #53
0
 def _handle_error(self, resp: ClientResponse, data: bytes):
     if resp.status != 200:
         LOG.error("%s: Status code %d for URL %s", self.id, resp.status, resp.url)
         LOG.error("%s: Headers: %s", self.id, resp.headers)
         LOG.error("%s: Resp: %s", self.id, data)
         resp.raise_for_status()
Beispiel #54
0
 def test_wait_for_100_1(self):
     response = ClientResponse(
         'get', 'http://python.org', continue100=object())
     response._post_init(self.loop)
     self.assertTrue(response.waiting_for_continue())
     response.close()
def test_content_type_no_header():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.headers = {}

    assert 'application/octet-stream' == response.content_type
def test_raise_for_status_2xx():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.status = 200
    response.reason = 'OK'
    response.raise_for_status()  # should not raise
Beispiel #57
0
    def test_https_auth(self, ClientRequestMock) -> None:
        proxy_req = ClientRequest('GET',
                                  URL('http://proxy.example.com'),
                                  auth=aiohttp.helpers.BasicAuth(
                                      'user', 'pass'),
                                  loop=self.loop)
        ClientRequestMock.return_value = proxy_req

        proxy_resp = ClientResponse('get',
                                    URL('http://proxy.example.com'),
                                    request_info=mock.Mock(),
                                    writer=mock.Mock(),
                                    continue100=None,
                                    timer=TimerNoop(),
                                    traces=[],
                                    loop=self.loop,
                                    session=mock.Mock())
        proxy_req.send = make_mocked_coro(proxy_resp)
        proxy_resp.start = make_mocked_coro(mock.Mock(status=200))

        async def make_conn():
            return aiohttp.TCPConnector()

        connector = self.loop.run_until_complete(make_conn())
        connector._resolve_host = make_mocked_coro([{
            'hostname': 'hostname',
            'host': '127.0.0.1',
            'port': 80,
            'family': socket.AF_INET,
            'proto': 0,
            'flags': 0
        }])

        tr, proto = mock.Mock(), mock.Mock()
        self.loop.create_connection = make_mocked_coro((tr, proto))

        self.assertIn('AUTHORIZATION', proxy_req.headers)
        self.assertNotIn('PROXY-AUTHORIZATION', proxy_req.headers)

        req = ClientRequest('GET',
                            URL('https://www.python.org'),
                            proxy=URL('http://proxy.example.com'),
                            loop=self.loop)
        self.assertNotIn('AUTHORIZATION', req.headers)
        self.assertNotIn('PROXY-AUTHORIZATION', req.headers)
        self.loop.run_until_complete(
            connector._create_connection(req, None, aiohttp.ClientTimeout()))

        self.assertEqual(req.url.path, '/')
        self.assertNotIn('AUTHORIZATION', req.headers)
        self.assertNotIn('PROXY-AUTHORIZATION', req.headers)
        self.assertNotIn('AUTHORIZATION', proxy_req.headers)
        self.assertIn('PROXY-AUTHORIZATION', proxy_req.headers)

        connector._resolve_host.assert_called_with('proxy.example.com',
                                                   80,
                                                   traces=mock.ANY)

        self.loop.run_until_complete(proxy_req.close())
        proxy_resp.close()
        self.loop.run_until_complete(req.close())
def test_charset_no_header():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.headers = {}

    assert response.charset is None
def test_charset():
    response = ClientResponse('get', URL('http://def-cl-resp.org'))
    response.headers = {'Content-Type': 'application/json;charset=cp1251'}

    assert 'cp1251' == response.charset
Beispiel #60
0
 def __init__(self, *args, **kwargs):
     self._response = ClientResponse(*args, **kwargs)