def fixture_response_connected(self, response_code): handle = MockCurl({pycurl.RESPONSE_CODE: response_code}) handle.request_obj = Request(RequestTarget(self.host), RequestData(self.request)) handle.output_buffer = io.BytesIO() handle.output_buffer.write(self.data) return Response.connection_successful(handle)
def _create_mock_curl(): counter["counter"] += 1 return ( MockCurl() if counter["counter"] != 2 else MockCurl(error=(pycurl.E_SEND_ERROR, "reason")) )
def _mock_create_request_handle(request, _, __): counter["counter"] += 1 return (MockCurl( request=request) if counter["counter"] > 2 else MockCurl( error=(pycurl.E_SEND_ERROR, "reason"), request=request, ))
def fixture_handle(info, request, data, debug): handle = MockCurl(info) handle.request_obj = request handle.output_buffer = io.BytesIO() handle.output_buffer.write(data.encode("utf-8")) handle.debug_buffer = io.BytesIO() handle.debug_buffer.write(debug.encode("utf-8")) return handle
def test_all_info(self, mock_curl): mock_curl.return_value = MockCurl(None, b"output", [ (pycurl.DEBUG_TEXT, b"debug"), (pycurl.DEBUG_DATA_OUT, b"info\n"), ]) request = lib.Request( lib.RequestTarget( "label", token="token_val", dest_list=_addr_list_to_dest(["host1", "host2"], port=123), ), lib.RequestData("action", [("data", "value")])) cookies = { "name1": "val1", "name2": "val2", } handle = lib._create_request_handle(request, cookies, 1) expected_opts = { pycurl.TIMEOUT: 1, pycurl.URL: request.url.encode("utf-8"), pycurl.COOKIE: "name1=val1;name2=val2;token=token_val".encode("utf-8"), pycurl.COPYPOSTFIELDS: "data=value".encode("utf-8"), } expected_opts.update(self._common_opts) self.assertLessEqual(set(expected_opts.items()), set(handle.opts.items())) self.assertIs(request, handle.request_obj) self.assertEqual("", handle.output_buffer.getvalue().decode("utf-8")) self.assertEqual("", handle.debug_buffer.getvalue().decode("utf-8")) handle.perform() self.assertEqual("output", handle.output_buffer.getvalue().decode("utf-8")) self.assertEqual("* debug\n>> info\n", handle.debug_buffer.getvalue().decode("utf-8"))
def test_failure( self, mock_create_handle, _, mock_con_successful, mock_con_failure ): expected_response_list = [] def _con_failure(handle, errno, err_msg): response = lib.Response(handle, False, errno, err_msg) expected_response_list.append(response) return response mock_con_failure.side_effect = _con_failure com = self.get_multiaddress_communicator() mock_create_handle.side_effect = lambda request, _, __: MockCurl( error=(pycurl.E_SEND_ERROR, "reason"), request=request, ) request = lib.Request( lib.RequestTarget( "label", dest_list=_addr_list_to_dest( ["host{0}".format(i) for i in range(4)] ), ), lib.RequestData("action") ) com.add_requests([request]) response_list = list(com.start_loop()) self.assertEqual(1, len(response_list)) response = response_list[0] self.assertFalse(response.was_connected) self.assertIs(request, response.request) self.assertEqual(Destination("host3", None), request.dest) self.assertEqual(4, mock_create_handle.call_count) mock_con_successful.assert_not_called() self.assertEqual(4, len(expected_response_list)) mock_create_handle.assert_has_calls([ mock.call(request, {}, settings.default_request_timeout) for _ in range(3) ]) logger_calls = ( fixture_logger_request_retry_calls( expected_response_list[0], Destination("host0", None) ) + fixture_logger_request_retry_calls( expected_response_list[1], Destination("host1", None) ) + fixture_logger_request_retry_calls( expected_response_list[2], Destination("host2", None) ) + [ mock.call.log_request_start(request), mock.call.log_response(response), mock.call.log_no_more_addresses(response) ] ) self.assertEqual(logger_calls, self.mock_com_log.mock_calls) # pylint: disable=no-member, protected-access com._multi_handle.assert_no_handle_left()
def test_call_start_loop_multiple_times(self, _, mock_create_handle): com = self.get_communicator() mock_create_handle.side_effect = lambda request, _, __: MockCurl( request=request) com.add_requests([fixture_request(i) for i in range(2)]) next(com.start_loop()) with self.assertRaises(AssertionError): next(com.start_loop())
def test_failure(self, mock_create_handle, _): com = self.get_communicator() expected_reason = "expected reason" errno = pycurl.E_SEND_ERROR response = self.get_response(com, mock_create_handle, MockCurl(error=(errno, expected_reason))) self.assert_common_checks(com, response) self.assertEqual(errno, response.errno) self.assertEqual(expected_reason, response.error_msg)
def test_basic(self, mock_curl): mock_curl.return_value = MockCurl(None) request = lib.Request(lib.RequestTarget("label"), lib.RequestData("action")) handle = lib._create_request_handle(request, {}, 10) expected_opts = { pycurl.TIMEOUT: 10, pycurl.URL: request.url.encode("utf-8"), } expected_opts.update(self._common_opts) self.assertLessEqual(set(expected_opts.items()), set(handle.opts.items())) self.assertFalse(pycurl.COOKIE in handle.opts) self.assertFalse(pycurl.COPYPOSTFIELDS in handle.opts) self.assertIs(request, handle.request_obj) self.assertEqual("", handle.output_buffer.getvalue().decode("utf-8")) self.assertEqual("", handle.debug_buffer.getvalue().decode("utf-8")) handle.perform() self.assertEqual("", handle.output_buffer.getvalue().decode("utf-8")) self.assertEqual("", handle.debug_buffer.getvalue().decode("utf-8"))
def fixture_response_not_connected(self, errno, error_msg): handle = MockCurl() handle.request_obj = Request(RequestTarget(self.host), RequestData(self.request)) return Response.connection_failure(handle, errno, error_msg)
def test_simple(self, mock_create_handle, _): com = self.get_communicator() response = self.get_response(com, mock_create_handle, MockCurl()) self.assert_common_checks(com, response)