def connect_client(host, port, http, username, password): client = RpcClient(host, port) if http: client.enable_http() if username and password: client.enable_http_basic_auth(username, password) return client
def connect_client(host, port, http, username, password): client = RpcClient(host, port) if http: client.enable_http() if username and password: client.enable_http_basic_auth(username, password) return client
def test_twisted_server_http(self): server = ServerRunner('../examples/serverhttp.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() try: result = client.rpc_call('echo', 'Hello Server') self.assertEqual(result, 'Hello Server') finally: client.close_connection() server.stop()
def test_twisted_server_http(self): server = ServerRunner('../examples/serverhttp.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() try: result = client.rpc_call('echo', 'Hello Server') self.assertEqual(result, 'Hello Server') finally: client.close_connection() server.stop()
def test_twisted_server_http_basic_auth_no_password(self): server = ServerRunner('../examples/serverhttp_basic_auth.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() try: with self.assertRaises(HttpException) as cm: client.rpc_call('is_authenticated') self.assertEqual(cm.exception.message, "Expected status code '200' but got '401'") self.assertEqual(cm.exception.status, '401') finally: client.close_connection() server.stop()
def test_client_http_invalid_answer(self): server = FakeServer('localhost', 5500) server.add_reply('{"error": null, "result": "Hello Server", "id": 1}') server.run() client = RpcClient('localhost', 5500) client.enable_http() try: with self.assertRaises(HttpException) as cm: client.rpc_call('echo', 'Hello Server') self.assertEqual(cm.exception.message, "Received invalid HTTP response: Couldn't find a HTTP header") server.stop() finally: client.close_connection()
def test_twisted_server_http_basic_auth_no_password(self): server = ServerRunner('../examples/serverhttp_basic_auth.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() try: with self.assertRaises(HttpException) as cm: client.rpc_call('is_authenticated') self.assertEqual(cm.exception.message, "Expected status code '200' but got '401'") self.assertEqual(cm.exception.status, '401') finally: client.close_connection() server.stop()
def test_twisted_server_http_basic_auth(self): server = ServerRunner('../examples/serverhttp_basic_auth.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() client.enable_http_basic_auth('testuser', '123456') try: authenticated = client.rpc_call('is_authenticated') username = client.rpc_call('get_username') self.assertEqual(authenticated, True) self.assertEqual(username, 'testuser') finally: client.close_connection() server.stop()
def test_twisted_server_http_basic_auth(self): server = ServerRunner('../examples/serverhttp_basic_auth.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() client.enable_http_basic_auth('testuser', '123456') try: authenticated = client.rpc_call('is_authenticated') username = client.rpc_call('get_username') self.assertEqual(authenticated, True) self.assertEqual(username, 'testuser') finally: client.close_connection() server.stop()
def test_client_http_invalid_answer(self): server = FakeServer('localhost', 5500) server.add_reply('{"error": null, "result": "Hello Server", "id": 1}') server.run() client = RpcClient('localhost', 5500) client.enable_http() try: with self.assertRaises(HttpException) as cm: client.rpc_call('echo', 'Hello Server') self.assertEqual( cm.exception.message, "Received invalid HTTP response: Couldn't find a HTTP header") server.stop() finally: client.close_connection()
def test_twisted_server_tls_client_auth_username_http(self): server = ServerRunner('../examples/servertls_clientauth_http.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() try: client.enable_tls('../examples/certs/rootCA.crt', False) client.enable_client_auth('../examples/certs/client.crt', '../examples/certs/client.key') authenticated = client.rpc_call('is_authenticated') username = client.rpc_call('get_username') self.assertEqual(authenticated, True) self.assertEqual(username, 'example-username') finally: client.close_connection() server.stop()
def test_concurrency_error_handling_http(self): server = ServerRunner('../examples/concurrency-http.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() try: with self.assertRaises(RpcError) as cm: client.rpc_call('deferred_error') self.assertEqual(cm.exception.json['name'], "JsonRpcError") self.assertEqual(cm.exception.json['message'], "You wanted an error, here you have it!") with self.assertRaises(RpcError) as cm: result = client.rpc_call('deferred_internal_error') self.assertEqual(cm.exception.json['name'], "InternalError") self.assertEqual(cm.exception.json['message'], "Internal error") finally: client.close_connection() server.stop()
def test_twisted_server_tls_client_auth_username_http(self): server = ServerRunner('../examples/servertls_clientauth_http.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() try: client.enable_tls('../examples/certs/rootCA.crt', False) client.enable_client_auth('../examples/certs/client.crt', '../examples/certs/client.key') authenticated = client.rpc_call('is_authenticated') username = client.rpc_call('get_username') self.assertEqual(authenticated, True) self.assertEqual(username, 'example-username') finally: client.close_connection() server.stop()
def test_concurrency_http(self): server = ServerRunner('../examples/concurrency-http.py', 5500) server.run() client1 = RpcClient('localhost', 5500) client1.enable_http() client2 = RpcClient('localhost', 5500) client2.enable_http() results = [] def t1_func(): result = client1.rpc_call('slow_operation') results.append(result) def t2_func(): time.sleep(0.5) result = client2.rpc_call('fast_operation') results.append(result) try: t1 = threading.Thread(target=t1_func, args=()) t1.start() t2 = threading.Thread(target=t2_func, args=()) t2.start() t1.join() if t1.is_alive(): raise RuntimeError("Failed to join on thread 1") t2.join() if t2.is_alive(): raise RuntimeError("Failed to join on thread 2") # slow_operation (value 42) must finish last self.assertEqual(results[0], 41) self.assertEqual(results[1], 42) finally: client1.close_connection() client2.close_connection() server.stop()
def test_concurrency_http(self): server = ServerRunner('../examples/concurrency-http.py', 5500) server.run() client1 = RpcClient('localhost', 5500) client1.enable_http() client2 = RpcClient('localhost', 5500) client2.enable_http() results = [] def t1_func(): result = client1.rpc_call('slow_operation') results.append(result) def t2_func(): time.sleep(0.5) result = client2.rpc_call('fast_operation') results.append(result) try: t1 = threading.Thread(target = t1_func, args = ()) t1.start() t2 = threading.Thread(target = t2_func, args = ()) t2.start() t1.join() if t1.is_alive(): raise RuntimeError("Failed to join on thread 1") t2.join() if t2.is_alive(): raise RuntimeError("Failed to join on thread 2") # slow_operation (value 42) must finish last self.assertEqual(results[0], 41) self.assertEqual(results[1], 42) finally: client1.close_connection() client2.close_connection() server.stop()
def test_concurrency_error_handling_http(self): server = ServerRunner('../examples/concurrency-http.py', 5500) server.run() client = RpcClient('localhost', 5500) client.enable_http() try: with self.assertRaises(RpcError) as cm: client.rpc_call('deferred_error') self.assertEqual(cm.exception.json['name'], "JsonRpcError") self.assertEqual(cm.exception.json['message'], "You wanted an error, here you have it!") with self.assertRaises(RpcError) as cm: result = client.rpc_call('deferred_internal_error') self.assertEqual(cm.exception.json['name'], "InternalError") self.assertEqual(cm.exception.json['message'], "Internal error") finally: client.close_connection() server.stop()