def test_http_post_broken_data(self): client = socket.create_connection(('localhost', self.port), 1) data = 'POST / HTTP/1.1\r\nContent-Length: 1024\r\n\r\nTest broken data' client.send(data) base.loop(count=5) client.close() self.assertEqual(self.server.resp_code, 408)
def test_notify_method_list_params(self): params = ['abc', 123, {'a': 1, 'b': 2, 'c': 3}] self.client.foo_list.notify(params) base.loop() self._assert_message(self._request, base.JsonRpcNotification) self.assertEqual(self._request.method, 'foo_list') self.assertEqual(self._request.params, params)
def test_call_method_few_times(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 85\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "test_result", "params": [123, "abc"]}''' client.send(data) client.send(data) client.send(data) base.loop(count=3) # First resp = http_client.HTTPResponse(client) resp.begin() data = resp.read() self.assertEqual(self.server.resp_code, 200) response = base.loads(data, [base.JsonRpcResponse]) self.assertTrue(isinstance(response, base.JsonRpcResponse)) self.assertEqual(response.id, '12345abc') self.assertEqual(response.result, { 'status': 'OK', 'params': { 'a': 123, 'b': 'abc' } }) # Second... resp = http_client.HTTPResponse(client) try: resp.begin() except http_client.BadStatusLine as err: self.assertEqual(str(err), "''") else: self.assertFalse(True) finally: client.close()
def test_call_method_few_times(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 85\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "test_result", "params": [123, "abc"]}''' client.send(data) client.send(data) client.send(data) base.loop() # First resp = httplib.HTTPResponse(client) resp.begin() data = resp.read() self.assertEqual(self.server.resp_code, 200) response = base.loads(data, [base.JsonRpcResponse]) self.assertTrue(isinstance(response, base.JsonRpcResponse)) self.assertEqual(response.id, '12345abc') self.assertEqual(response.result, {'status': 'OK', 'params': {'a': 123, 'b': 'abc'}}) # Second... resp = httplib.HTTPResponse(client) try: resp.begin() except httplib.BadStatusLine, err: self.assertEqual(str(err), '')
def test_notify_method_dict_params(self): params = {'a': 1, 'b': 'def', 'c': [3, 6, 9]} self.client.foo_dict.notify(params) base.loop() self._assert_message(self._request, base.JsonRpcNotification) self.assertEqual(self._request.method, 'foo_dict') self.assertEqual(self._request.params, params)
def test_http_post_broken_data(self): client = socket.create_connection(('localhost', self.port), 1) data = 'POST / HTTP/1.1\r\nContent-Length: 1024\r\n\r\nTest broken data' client.send(data) base.loop() client.close() self.assertEqual(self.server.resp_code, 408)
def test_call_invalid_dict_params(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 106\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "test_result", "params": {"a": 123, "b": "abc", "c": true}}''' client.send(data) base.loop(count=3) resp = http_client.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) try: base.loads(data, [base.JsonRpcResponse]) except errors.JsonRpcError as err: self.assertEqual(err.code, -32602) self.assertEqual(err.message, 'Invalid params.') self.assertEqual( err.data, { 'method': 'test_result', 'params': { 'a': 123, 'b': 'abc', 'c': True } })
def test_notify_method(self): context = self.client.foo.notify() self.assertTrue(isinstance(context, client.JsonRpcContext)) self.assertTrue(context.closed()) base.loop() self._assert_message(self._request, base.JsonRpcNotification) self.assertEqual(self._request.method, 'foo') self.assertEqual(self._request.params, None)
def test_request_method(self): context = self.client.foo() self.assertTrue(isinstance(context, client.JsonRpcContext)) self.assertFalse(context.closed()) base.loop() self._assert_message(self._request, base.JsonRpcRequest) self.assertEqual(self._request.id, context.request.id) self.assertEqual(self._request.method, 'foo') self.assertEqual(self._request.params, None)
def test_notify_method_error(self): params = [1] self.client.notifier = True self.client.test_exception(params) base.loop() self.assertTrue(isinstance(self.server.request, base.JsonRpcNotification)) self.assertEqual(self.server.request.method, 'test_exception') self.assertEqual(self.server.request.params, params) self.assertEqual(self._result, None) self.assertEqual(self._error, None)
def test_notify_method_list_params(self): params = ['abc', {'a': 1, 'b': 2, 'c': 3}] self.client.notifier = True self.client.test_result(params) base.loop() self.assertTrue(isinstance(self.server.request, base.JsonRpcNotification)) self.assertEqual(self.server.request.method, 'test_result') self.assertEqual(self.server.request.params, params) self.assertEqual(self._result, None) self.assertEqual(self._error, None)
def test_notify_method_dict_params(self): params = {'b': 'def', 'a': [3, 6, 9]} self.client.notifier = True self.client.test_result(params) base.loop() self.assertTrue(isinstance(self.server.request, base.JsonRpcNotification)) self.assertEqual(self.server.request.method, 'test_result') self.assertEqual(self.server.request.params, params) self.assertEqual(self._result, None) self.assertEqual(self._error, None)
def test_request_method_dict_params(self): params = {'b': 'def', 'a': [3, 6, 9]} self.client.test_result(params, on_result=self._on_result, on_error=self._on_error) base.loop() self.assertTrue(isinstance(self.server.request, base.JsonRpcRequest)) self.assertEqual(self.server.request.method, 'test_result') self.assertEqual(self.server.request.params, params) self.assertEqual(self._result, {'status': 'OK', 'params': params}) self.assertEqual(self._error, None)
def test_notification(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 67\r \r {"jsonrpc": "2.0", "method": "test_result", "params": [123, "abc"]}''' client.send(data) base.loop(count=3) resp = http_client.HTTPResponse(client) self.assertRaises(http_client.BadStatusLine, resp.begin) self.assertEqual(self.server.resp_code, None) client.close()
def test_request_method_list_params(self): params = ['abc', {'a': 1, 'b': 2, 'c': 3}] self.client.test_result(params, on_result=self._on_result, on_error=self._on_error) base.loop() self.assertTrue(isinstance(self.server.request, base.JsonRpcRequest)) self.assertEqual(self.server.request.method, 'test_result') self.assertEqual(self.server.request.params, params) self.assertEqual(self._result, {'status': 'OK', 'params': {'a': params[0], 'b': params[1]}}) self.assertEqual(self._error, None)
def test_notification(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 67\r \r {"jsonrpc": "2.0", "method": "test_result", "params": [123, "abc"]}''' client.send(data) base.loop(count=1) resp = httplib.HTTPResponse(client) self.assertRaises(httplib.BadStatusLine, resp.begin) self.assertEqual(self.server.resp_code, None) client.close()
def test_request_method_error(self): params = [1] self.client.test_exception(params, on_result=self._on_result, on_error=self._on_error) base.loop() self.assertTrue(isinstance(self.server.request, base.JsonRpcRequest)) self.assertEqual(self.server.request.method, 'test_exception') self.assertEqual(self.server.request.params, params) self.assertTrue(isinstance(self._error, errors.JsonRpcError)) self.assertEqual(self.server.request.id, self._error.id) self.assertEqual(self._error.marshal()['error'], {'code': -32603, 'message': 'Internal error.', 'data': {'exception': '1'}}) self.assertEqual(self._result, None)
def test_response_timeout(self): def on_error(error): self._result = error self.client.timeout = 0.1 self.server.del_channel() context = self.client.foo(on_error=on_error) self.assertTrue(isinstance(context, client.JsonRpcContext)) base.loop() self._assert_message(context.request, base.JsonRpcRequest) self._assert_message(self._result, errors.JsonRpcProtocolError) self.assertEqual(self._result.id, context.request.id) self.assertEqual(self._result.code, 110)
def test_http_post_data(self): client = socket.create_connection(('localhost', self.port), 1) data = 'POST / HTTP/1.1\r\nContent-Length: 14\r\n\r\nTest text data' client.send(data) base.loop() resp = httplib.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) try: base.loads(data) except errors.JsonRpcError, err: self.assertEqual(err.code, -32700) self.assertEqual(err.message, 'Parse error.')
def test_request_method_result(self): def callback(data): self._request_callback(data) msg = base.JsonRpcResponse(self._request.id, result) return self._format_response(msg) def on_result(result): self._result = result result = {'status': 'OK'} self.server.connect_callback(callback) self.client.foo(on_result=on_result) base.loop() self._assert_message(self._request, base.JsonRpcRequest) self.assertEqual(self._result, result)
def test_http_post_data(self): client = socket.create_connection(('localhost', self.port), 1) data = 'POST / HTTP/1.1\r\nContent-Length: 14\r\n\r\nTest text data' client.send(data) base.loop(count=3) resp = http_client.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) try: base.loads(data) except errors.JsonRpcError as err: self.assertEqual(err.code, -32700) self.assertEqual(err.message, 'Parse error.')
def test_http_get_response(self): def callback(data): self._request_callback(data) return 'GET / HTTP/1.1\r\n\r\n' def on_error(error): self._result = error self.server.connect_callback(callback) self.client.foo(on_error=on_error) base.loop() self._assert_message(self._request, base.JsonRpcRequest) self._assert_message(self._result, errors.JsonRpcParseError) self.assertEqual(self._result.id, self._request.id) self.assertEqual(self._result.data, {'exception': 'No JSON object could be decoded'})
def test_empty_tcp_response(self): def callback(data): self._request_callback(data) return '' def on_error(error): self._result = error self.server.connect_callback(callback) self.client.foo(on_error=on_error) base.loop() self._assert_message(self._request, base.JsonRpcRequest) self._assert_message(self._result, errors.JsonRpcResponseError) self.assertEqual(self._result.id, self._request.id) self.assertEqual(self._result.code, -32650) self.assertEqual(self._result.message, 'Invalid response.') self.assertEqual(self._result.data, {'exception': "''"})
def test_request_method_invalid_response_id(self): def callback(data): self._request_callback(data) msg = base.JsonRpcResponse('a1b2c3d4', None) return self._format_response(msg) def on_error(error): self._result = error self.server.connect_callback(callback) self.client.foo(on_error=on_error) base.loop() self._assert_message(self._request, base.JsonRpcRequest) self._assert_message(self._result, errors.JsonRpcResponseError) self.assertEqual(self._result.id, self._request.id) self.assertEqual(self._result.code, -32650) self.assertEqual(self._result.message, 'Invalid response.') self.assertEqual(self._result.data, {'id': 'a1b2c3d4'})
def test_interface_on_result(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 88\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "test_on_result", "params": [123, "abc"]}''' client.send(data) base.loop() resp = httplib.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) response = base.loads(data, [base.JsonRpcResponse]) self.assertTrue(isinstance(response, base.JsonRpcResponse)) self.assertEqual(response.id, '12345abc') self.assertEqual(response.result, {'status': 'OK', 'params': {'a': 123, 'b': 'abc'}})
def test_call_method_not_found(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 102\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "method_not_found", "params": {"a": "abc", "b": "efg"}}''' client.send(data) base.loop() resp = httplib.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) try: base.loads(data, [base.JsonRpcResponse]) except errors.JsonRpcError, err: self.assertEqual(err.code, -32601) self.assertEqual(err.message, 'Method not found.') self.assertEqual(err.data, {'method': 'method_not_found'})
def test_interface_on_error(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 82\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "test_on_error", "params": ["efg"]}''' client.send(data) base.loop(count=3) resp = http_client.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) try: base.loads(data, [base.JsonRpcResponse]) except errors.JsonRpcError as err: self.assertEqual(err.code, -32603) self.assertEqual(err.message, 'Internal error.') self.assertEqual(err.data, {'exception': 'efg'})
def test_interface_on_error(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 82\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "test_on_error", "params": ["efg"]}''' client.send(data) base.loop() resp = httplib.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) try: base.loads(data, [base.JsonRpcResponse]) except errors.JsonRpcError, err: self.assertEqual(err.code, -32603) self.assertEqual(err.message, 'Internal error.') self.assertEqual(err.data, {'exception': 'efg'})
def test_call_private_method_not_found(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 96\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "_on_result", "params": {"a": "abc", "b": "efg"}}''' client.send(data) base.loop(count=3) resp = http_client.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) try: base.loads(data, [base.JsonRpcResponse]) except errors.JsonRpcError as err: self.assertEqual(err.code, -32601) self.assertEqual(err.message, 'Method not found.') self.assertEqual(err.data, {'method': '_on_result'})
def test_call_invalid_dict_params(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 106\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "test_result", "params": {"a": 123, "b": "abc", "c": true}}''' client.send(data) base.loop() resp = httplib.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) try: base.loads(data, [base.JsonRpcResponse]) except errors.JsonRpcError, err: self.assertEqual(err.code, -32602) self.assertEqual(err.message, 'Invalid params.') self.assertEqual(err.data, {'method': 'test_result', 'params': {'a': 123, 'b': 'abc', 'c': True}})
def test_request_method_error(self): def callback(data): self._request_callback(data) msg = errors.JsonRpcError(id=self._request.id, **error) return self._format_response(msg.marshal()) def on_error(error): self._result = error error = { 'code': -12345, 'message': 'Test error message', 'data': {'exception': 'Test exception'} } self.server.connect_callback(callback) self.client.foo(on_error=on_error) base.loop() self._assert_message(self._request, base.JsonRpcRequest) self._assert_message(self._result, errors.JsonRpcError) self.assertEqual(self._result.id, self._request.id) self.assertEqual(self._result.marshal()['error'], error)
def test_request_method_http_error(self): def callback(data): self._request_callback(data) return '''HTTP/1.1 501 Unsupported method ('GET')\r Content-Type: application/json-rpc\r Content-Length: 15\r \r <html />''' def on_error(error): self._result = error self.server.connect_callback(callback) self.client.foo(on_error=on_error) base.loop() self._assert_message(self._request, base.JsonRpcRequest) self._assert_message(self._result, errors.JsonRpcProtocolError) self.assertEqual(self._result.id, self._request.id) self.assertEqual(self._result.code, 501) self.assertEqual(self._result.message, "Unsupported method ('GET')") self.assertEqual(self._result.data, {'exception': '<html />'})
def test_interface_on_result(self): client = socket.create_connection(('localhost', self.port), 1) data = '''POST / HTTP/1.1\r Content-Length: 88\r \r {"jsonrpc": "2.0", "id": "12345abc", "method": "test_on_result", "params": [123, "abc"]}''' client.send(data) base.loop(count=3) resp = http_client.HTTPResponse(client) resp.begin() data = resp.read() client.close() self.assertEqual(self.server.resp_code, 200) response = base.loads(data, [base.JsonRpcResponse]) self.assertTrue(isinstance(response, base.JsonRpcResponse)) self.assertEqual(response.id, '12345abc') self.assertEqual(response.result, { 'status': 'OK', 'params': { 'a': 123, 'b': 'abc' } })
def test_tcp_data(self): client = socket.create_connection(('localhost', self.port), 1) client.send('Test tcp data\n') base.loop() client.close() self.assertEqual(self.server.resp_code, 400)
def test_tcp_empty(self): client = socket.create_connection(('localhost', self.port), 1) base.loop(count=3) client.close() self.assertEqual(self.server.resp_code, 408)
def test_tcp_empty(self): client = socket.create_connection(('localhost', self.port), 1) base.loop() client.close() self.assertEqual(self.server.resp_code, 408)
def test_tcp_data(self): client = socket.create_connection(('localhost', self.port), 1) client.send('Test tcp data\n') base.loop(count=3) client.close() self.assertEqual(self.server.resp_code, 400)
def test_http_get(self): client = socket.create_connection(('localhost', self.port), 1) client.send('GET / HTTP/1.1\r\n\r\n') base.loop() client.close() self.assertEqual(self.server.resp_code, 501)
def test_http_get(self): client = socket.create_connection(('localhost', self.port), 1) client.send('GET / HTTP/1.1\r\n\r\n') base.loop(count=3) client.close() self.assertEqual(self.server.resp_code, 501)