diff --git a/beemgrapheneapi/graphenerpc.py b/beemgrapheneapi/graphenerpc.py index 7359daf2d3558c4b3db648a3f82c13c17b4d83f2..fd2147054ee9dd0cb595d64253c250b08d7cd252 100644 --- a/beemgrapheneapi/graphenerpc.py +++ b/beemgrapheneapi/graphenerpc.py @@ -200,6 +200,35 @@ class GrapheneRPC(object): reply = self.ws.recv() return reply + def _check_for_server_error(self, reply): + """Checks for server error message in reply""" + if re.search("Internal Server Error", reply) or re.search("500", reply): + raise RPCErrorDoRetry("Internal Server Error") + elif re.search("Not Implemented", reply) or re.search("501", reply): + raise RPCError("Not Implemented") + elif re.search("Bad Gateway", reply) or re.search("502", reply): + raise RPCErrorDoRetry("Bad Gateway") + elif re.search("Service Temporarily Unavailable", reply) or re.search("Service Unavailable", reply) or re.search("503", reply): + raise RPCErrorDoRetry("Service Temporarily Unavailable") + elif re.search("Gateway Time-out", reply) or re.search("Gateway Timeout", reply) or re.search("504", reply): + raise RPCErrorDoRetry("Gateway Time-out") + elif re.search("HTTP Version not supported", reply) or re.search("505", reply): + raise RPCError("HTTP Version not supported") + elif re.search("Variant Also Negotiates", reply) or re.search("506", reply): + raise RPCError("Variant Also Negotiates") + elif re.search("Insufficient Storage", reply) or re.search("507", reply): + raise RPCError("Insufficient Storage") + elif re.search("Loop Detected", reply) or re.search("508", reply): + raise RPCError("Loop Detected") + elif re.search("Bandwidth Limit Exceeded", reply) or re.search("509", reply): + raise RPCError("Bandwidth Limit Exceeded") + elif re.search("Not Extended", reply) or re.search("510", reply): + raise RPCError("Not Extended") + elif re.search("Network Authentication Required", reply) or re.search("511", reply): + raise RPCError("Network Authentication Required") + else: + raise ValueError("Client returned invalid format. Expected JSON!") + def rpcexec(self, payload): """ Execute a call by sending the payload. @@ -232,32 +261,7 @@ class GrapheneRPC(object): try: ret = json.loads(reply, strict=False) except ValueError: - if re.search("Internal Server Error", reply) or re.search("500", reply): - raise RPCErrorDoRetry("Internal Server Error") - elif re.search("Not Implemented", reply) or re.search("501", reply): - raise RPCError("Not Implemented") - elif re.search("Bad Gateway", reply) or re.search("502", reply): - raise RPCErrorDoRetry("Bad Gateway") - elif re.search("Service Temporarily Unavailable", reply) or re.search("Service Unavailable", reply) or re.search("503", reply): - raise RPCErrorDoRetry("Service Temporarily Unavailable") - elif re.search("Gateway Time-out", reply) or re.search("Gateway Timeout", reply) or re.search("504", reply): - raise RPCErrorDoRetry("Gateway Time-out") - elif re.search("HTTP Version not supported", reply) or re.search("505", reply): - raise RPCError("HTTP Version not supported") - elif re.search("Variant Also Negotiates", reply) or re.search("506", reply): - raise RPCError("Variant Also Negotiates") - elif re.search("Insufficient Storage", reply) or re.search("507", reply): - raise RPCError("Insufficient Storage") - elif re.search("Loop Detected", reply) or re.search("508", reply): - raise RPCError("Loop Detected") - elif re.search("Bandwidth Limit Exceeded", reply) or re.search("509", reply): - raise RPCError("Bandwidth Limit Exceeded") - elif re.search("Not Extended", reply) or re.search("510", reply): - raise RPCError("Not Extended") - elif re.search("Network Authentication Required", reply) or re.search("511", reply): - raise RPCError("Network Authentication Required") - else: - raise ValueError("Client returned invalid format. Expected JSON!") + self._check_for_server_error(reply) log.debug(json.dumps(reply)) diff --git a/tests/beemapi/test_steemnoderpc.py b/tests/beemapi/test_steemnoderpc.py index 4ee8c62797dc2483e006ad650556ded304bcf855..b9ad584e0313b8d43716999ffb3a85df7caf8367 100644 --- a/tests/beemapi/test_steemnoderpc.py +++ b/tests/beemapi/test_steemnoderpc.py @@ -50,11 +50,17 @@ class Testcases(unittest.TestCase): keys={"active": wif, "owner": wif, "memo": wif}, num_retries=10 ) + self.rpc = SteemNodeRPC(urls=test_list) # from getpass import getpass # self.bts.wallet.unlock(getpass()) set_shared_steem_instance(self.bts) self.bts.set_default_account("test") + def get_reply(self, msg): + reply = '<html> <head><title>403 Forbidden</title></head><body bgcolor="white"><center><h1>' \ + '%s</h1></center><hr><center>nginx</center></body> </html>' % (msg) + return reply + def test_non_appbase(self): bts = self.bts self.assertTrue(bts.chain_params['min_version'] == '0.0.0') @@ -80,14 +86,87 @@ class Testcases(unittest.TestCase): bts.rpc.get_config_abc() def test_connect_test_node(self): - rpc = SteemNodeRPC(urls=test_list) + rpc = self.rpc self.assertIn(rpc.url, nodes + nodes_appbase + nodes_https) rpc.rpcclose() rpc.rpcconnect() self.assertIn(rpc.url, nodes + nodes_appbase + nodes_https) def test_connect_test_node2(self): - rpc = SteemNodeRPC(urls=test_list) + rpc = self.rpc + self.assertIn(rpc.url, nodes + nodes_appbase + nodes_https) + rpc.next() + self.assertIn(rpc.url, nodes + nodes_appbase + nodes_https) + + def test_connect_test_str_list(self): + str_list = "wss://steemd.pevo.science;wss://gtg.steem.house:8090;wss://rpc.steemliberator.com;wss://rpc.buildteam.io" + rpc = SteemNodeRPC(urls=str_list) self.assertIn(rpc.url, nodes + nodes_appbase + nodes_https) rpc.next() self.assertIn(rpc.url, nodes + nodes_appbase + nodes_https) + + def test_connect_test_str_list2(self): + str_list = "wss://steemd.pevo.science, wss://gtg.steem.house:8090, wss://rpc.steemliberator.com, wss://rpc.buildteam.io" + rpc = SteemNodeRPC(urls=str_list) + self.assertIn(rpc.url, nodes + nodes_appbase + nodes_https) + rpc.next() + self.assertIn(rpc.url, nodes + nodes_appbase + nodes_https) + + def test_server_error(self): + rpc = self.rpc + with self.assertRaises( + exceptions.RPCErrorDoRetry + ): + rpc._check_for_server_error(self.get_reply("500 Internal Server Error")) + with self.assertRaises( + exceptions.RPCError + ): + rpc._check_for_server_error(self.get_reply("501 Not Implemented")) + + with self.assertRaises( + exceptions.RPCErrorDoRetry + ): + rpc._check_for_server_error(self.get_reply("502 Bad Gateway")) + + with self.assertRaises( + exceptions.RPCErrorDoRetry + ): + rpc._check_for_server_error(self.get_reply("503 Service Temporarily Unavailable")) + + with self.assertRaises( + exceptions.RPCErrorDoRetry + ): + rpc._check_for_server_error(self.get_reply("504 Gateway Time-out")) + + with self.assertRaises( + exceptions.RPCError + ): + rpc._check_for_server_error(self.get_reply("505 HTTP Version not supported")) + with self.assertRaises( + exceptions.RPCError + ): + rpc._check_for_server_error(self.get_reply("506 Variant Also Negotiates")) + + with self.assertRaises( + exceptions.RPCError + ): + rpc._check_for_server_error(self.get_reply("507 Insufficient Storage")) + + with self.assertRaises( + exceptions.RPCError + ): + rpc._check_for_server_error(self.get_reply("508 Loop Detected")) + + with self.assertRaises( + exceptions.RPCError + ): + rpc._check_for_server_error(self.get_reply("509 Bandwidth Limit Exceeded")) + with self.assertRaises( + exceptions.RPCError + ): + rpc._check_for_server_error(self.get_reply("510 Not Extended")) + + with self.assertRaises( + exceptions.RPCError + ): + rpc._check_for_server_error(self.get_reply("511 Network Authentication Required"))