def test_handles_transport_closed_unexpectedly(self): set_exception_logging(False) transport = MockTransport(raise_error) settings = MockSettings() client = Client(transport, settings) errors = [] client.set_transport_failure_handler(lambda: errors.append("")) self.assertTrue(transport.has_started) transport.close() self.assertGreater(len(errors), 0)
def test_survives_handler_error(self): set_exception_logging(False) transport = MockTransport(return_empty_dict_result) settings = MockSettings() client = Client(transport, settings) self.assertIsNotNone(client) self.assertTrue(transport.has_started) req = Request.initialize(dict()) client.send_request(req, lambda resp: raise_error('handler failed')) # exception would fail test if not handled in client self.assertEqual(len(client._response_handlers), 0)
def test_server_request_non_integer_request(self): transport = MockTransport() settings = MockSettings() client = Client(transport, settings) self.assertIsNotNone(client) self.assertTrue(transport.has_started) pings = [] # type: List[Tuple[Any, Dict[str, Any]]] client.on_request( "ping", lambda params, request_id: pings.append( (request_id, params))) transport.receive('{ "id": "abcd-1234-efgh-5678", "method": "ping"}') self.assertEqual(len(pings), 1) self.assertIsInstance(pings[0][0], str) self.assertEqual(pings[0][0], "abcd-1234-efgh-5678")
def do_error_display_handler(self, method): transport = MockTransport(return_error) settings = MockSettings() client = Client(transport, settings) self.assertIsNotNone(client) self.assertTrue(transport.has_started) req = Request.initialize(dict()) errors = [] client.set_error_display_handler(lambda err: errors.append(err)) responses = [] do_request = method.__get__(client, Client) do_request(req, lambda resp: responses.append(resp)) self.assertEqual(len(responses), 0) self.assertGreater(len(errors), 0) self.assertEqual(len(client._response_handlers), 0)
def test_server_request_send_error(self): transport = MockTransport() settings = MockSettings() client = Client(transport, settings) self.assertIsNotNone(client) self.assertTrue(transport.has_started) def always_raise_exception(params: Any, request_id: Any) -> None: raise Error(ErrorCode.InvalidParams, "expected dict, got list") client.on_request("ping", always_raise_exception) transport.receive('{ "id": "abcd-1234-efgh-5678", "method": "ping"}') self.assertEqual(len(transport.messages), 1) self.assertEqual( json.loads(transport.messages[0]), { "error": { "message": "expected dict, got list", "code": -32602 }, "jsonrpc": "2.0", "id": "abcd-1234-efgh-5678" })
def test_server_request_exception_during_handler(self): transport = MockTransport() settings = MockSettings() client = Client(transport, settings) self.assertIsNotNone(client) self.assertTrue(transport.has_started) def always_raise_exception(params: Any, request_id: Any) -> None: raise AttributeError("whoops") client.on_request("ping", always_raise_exception) transport.receive('{ "id": "abcd-1234-efgh-5678", "method": "ping"}') self.assertEqual(len(transport.messages), 1) self.assertEqual( json.loads(transport.messages[0]), { "error": { "message": "whoops", "code": -32603 }, "jsonrpc": "2.0", "id": "abcd-1234-efgh-5678" })
def do_client_request_response(self, method): transport = MockTransport(return_empty_dict_result) settings = MockSettings() client = Client(transport, settings) self.assertIsNotNone(client) self.assertTrue(transport.has_started) req = Request.initialize(dict()) responses = [] do_request = method.__get__(client, Client) do_request(req, lambda resp: responses.append(resp)) self.assertGreater(len(responses), 0) # Make sure the response handler dict does not grow. self.assertEqual(len(client._response_handlers), 0)
def do_client_should_reject_response_when_both_result_and_error_keys_are_not_present( self, method): transport = MockTransport(lambda x: '{"id": 1}') settings = MockSettings() client = Client(transport, settings) req = Request.initialize(dict()) responses = [] errors = [] do_request = method.__get__(client, Client) do_request(req, lambda resp: responses.append(resp), lambda err: errors.append(err)) self.assertEqual(len(responses), 0) self.assertEqual(len(errors), 1) self.assertEqual(errors[0]["code"], ErrorCode.InvalidParams)
def do_client_request_with_none_response(self, method): transport = MockTransport(return_null_result) settings = MockSettings() client = Client(transport, settings) self.assertIsNotNone(client) self.assertTrue(transport.has_started) req = Request.shutdown() responses = [] errors = [] # https://stackoverflow.com/questions/1015307/python-bind-an-unbound-method do_request = method.__get__(client, Client) do_request(req, lambda resp: responses.append(resp), lambda err: errors.append(err)) self.assertEqual(len(responses), 1) self.assertEqual(len(errors), 0)
def test_server_request_unknown(self): transport = MockTransport() settings = MockSettings() client = Client(transport, settings) self.assertIsNotNone(client) self.assertTrue(transport.has_started) transport.receive('{ "id": "abcd-1234-efgh-5678", "method": "ping"}') self.assertEqual(len(transport.messages), 1) self.assertEqual( json.loads(transport.messages[0]), { "error": { "message": "ping", "code": -32601 }, "jsonrpc": "2.0", "id": "abcd-1234-efgh-5678" })
def test_client_notification(self): transport = MockTransport(notify_pong) settings = MockSettings() client = Client(transport, settings) self.assertIsNotNone(client) self.assertTrue(transport.has_started) pongs = [] client.on_notification("pong", lambda params: pongs.append(params)) req = Notification("ping", dict()) client.send_notification(req) self.assertGreater(len(transport.messages), 0) self.assertEqual(len(pongs), 1)
def test_can_create_client(self): transport = MockTransport() client = Client(transport, Settings()) self.assertIsNotNone(client) self.assertTrue(transport.has_started)