def test_initialize(self): req = Request.initialize({"param": 1}) payload = req.to_payload(1) self.assertEqual(payload["jsonrpc"], "2.0") self.assertEqual(payload["id"], 1) self.assertEqual(payload["method"], "initialize") self.assertEqual(payload["params"], {"param": 1})
def test_shutdown(self): req = Request.shutdown() payload = req.to_payload(1) self.assertEqual(payload["jsonrpc"], "2.0") self.assertEqual(payload["id"], 1) self.assertEqual(payload["method"], "shutdown") self.assertEqual(payload["params"], None)
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 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_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 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)