diff options
author | John Newbery <john@johnnewbery.com> | 2018-03-22 14:26:37 -0400 |
---|---|---|
committer | Roman Zeyde <me@romanzey.de> | 2018-04-03 10:53:37 +0300 |
commit | 7a3181a7676c822b2a369c745e0c45b0347c78e1 (patch) | |
tree | 064f5fa7546c48f93b1eb59645e328cb3c073ca2 /test/functional/interface_rest.py | |
parent | 3fd4490db1381c0a68112edfb2b2e5f5906dab8c (diff) |
[tests] Make json request building more consistent in interface_rest.py
Diffstat (limited to 'test/functional/interface_rest.py')
-rwxr-xr-x | test/functional/interface_rest.py | 92 |
1 files changed, 43 insertions, 49 deletions
diff --git a/test/functional/interface_rest.py b/test/functional/interface_rest.py index 7e0fe6080a..79c1fe1a89 100755 --- a/test/functional/interface_rest.py +++ b/test/functional/interface_rest.py @@ -42,8 +42,6 @@ def http_post_call(host, port, path, requestdata='', response_object=0): return conn.getresponse().read() class RESTTest (BitcoinTestFramework): - FORMAT_SEPARATOR = "." - def set_test_params(self): self.setup_clean_chain = True self.num_nodes = 3 @@ -75,7 +73,8 @@ class RESTTest (BitcoinTestFramework): self.log.info("Load the transaction using the /tx URI") - json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json") + json_request = "/rest/tx/{}.json".format(txid) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) vintx = json_obj['vin'][0]['txid'] # get the vin to later check for utxo (should be spent by then) # get n of 0.1 outpoint @@ -86,8 +85,8 @@ class RESTTest (BitcoinTestFramework): self.log.info("Query an unspent TXO using the /getutxos URI") - json_request = '/' + txid + '-' + str(n) - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = "/rest/getutxos/{}-{}.json".format(txid, str(n)) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) # Check chainTip response @@ -99,8 +98,8 @@ class RESTTest (BitcoinTestFramework): self.log.info("Query a spent TXO using the /getutxos URI") - json_request = '/' + vintx + '-0' - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = "/rest/getutxos/{}-0.json".format(vintx) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) # Check chainTip response @@ -114,8 +113,8 @@ class RESTTest (BitcoinTestFramework): self.log.info("Query two TXOs using the /getutxos URI") - json_request = '/' + txid + '-' + str(n) + '/' + vintx + '-0' - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = "/rest/getutxos/{}-{}/{}-0.json".format(txid, str(n), vintx) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) assert_equal(json_obj['bitmap'], "10") @@ -130,7 +129,7 @@ class RESTTest (BitcoinTestFramework): bin_request += hex_str_to_bytes(vintx) bin_request += pack("i", 0) - bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'bin', bin_request) + bin_response = http_post_call(url.hostname, url.port, '/rest/getutxos.bin', bin_request) output = BytesIO() output.write(bin_response) output.seek(0) @@ -147,7 +146,8 @@ class RESTTest (BitcoinTestFramework): # do a tx and don't sync txid = self.nodes[0].sendtoaddress(self.nodes[1].getnewaddress(), 0.1) - json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + txid + self.FORMAT_SEPARATOR + "json") + json_request = "/rest/tx/{}.json".format(txid) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) # get the spent output to later check for utxo (should be spent by then) spent = '{}-{}'.format(json_obj['vin'][0]['txid'], json_obj['vin'][0]['vout']) @@ -158,64 +158,58 @@ class RESTTest (BitcoinTestFramework): n = vout['n'] spending = '{}-{}'.format(txid, n) - json_request = '/' + spending - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/{}.json'.format(spending) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 0) - json_request = '/checkmempool/' + spending - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) - json_request = '/' + spent - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/{}.json'.format(spent) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) - json_request = '/checkmempool/' + spent - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/checkmempool/{}.json'.format(spent) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 0) self.nodes[0].generate(1) self.sync_all() - json_request = '/' + spending - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/{}.json'.format(spending) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) - json_request = '/checkmempool/' + spending - json_string = http_get_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json') + json_request = '/rest/getutxos/checkmempool/{}.json'.format(spending) + json_string = http_get_call(url.hostname, url.port, json_request) json_obj = json.loads(json_string) assert_equal(len(json_obj['utxos']), 1) # Do some invalid requests json_request = '{"checkmempool' - response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'json', json_request, True) + response = http_post_call(url.hostname, url.port, '/rest/getutxos.json', json_request, True) assert_equal(response.status, 400) # must be a 400 because we send an invalid json request json_request = '{"checkmempool' - response = http_post_call(url.hostname, url.port, '/rest/getutxos' + self.FORMAT_SEPARATOR + 'bin', json_request, True) + response = http_post_call(url.hostname, url.port, '/rest/getutxos.bin', json_request, True) assert_equal(response.status, 400) # must be a 400 because we send an invalid bin request - response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool' + self.FORMAT_SEPARATOR + 'bin', '', True) + response = http_post_call(url.hostname, url.port, '/rest/getutxos/checkmempool.json', '', True) assert_equal(response.status, 400) # must be a 400 because we send an invalid bin request # Test limits - json_request = '/checkmempool/' - for x in range(0, 20): - json_request += txid + '-' + str(n) + '/' - json_request = json_request.rstrip("/") - response = http_post_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True) + json_request = '/rest/getutxos/checkmempool/' + '/'.join(["{}-{}".format(txid, n) for n in range(20)]) + '.json' + response = http_post_call(url.hostname, url.port, json_request, '', True) assert_equal(response.status, 400) # must be a 400 because we exceeding the limits - json_request = '/checkmempool/' - for x in range(0, 15): - json_request += txid + '-' + str(n) + '/' - json_request = json_request.rstrip("/") - response = http_post_call(url.hostname, url.port, '/rest/getutxos' + json_request + self.FORMAT_SEPARATOR + 'json', '', True) + json_request = '/rest/getutxos/checkmempool/' + '/'.join(['{}-{}'.format(txid, n) for n in range(15)]) + '.json' + response = http_post_call(url.hostname, url.port, json_request, '', True) assert_equal(response.status, 200) # must be a 200 because we are within the limits self.nodes[0].generate(1) # generate block to not affect upcoming tests @@ -224,27 +218,27 @@ class RESTTest (BitcoinTestFramework): self.log.info("Test the /block and /headers URIs") # Check binary format - response = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True) + response = http_get_call(url.hostname, url.port, '/rest/block/{}.bin'.format(bb_hash), True) assert_equal(response.status, 200) assert_greater_than(int(response.getheader('content-length')), 80) response_str = response.read() # Compare with block header - response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "bin", True) + response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.bin'.format(bb_hash), True) assert_equal(response_header.status, 200) assert_equal(int(response_header.getheader('content-length')), 80) response_header_str = response_header.read() assert_equal(response_str[0:80], response_header_str) # Check block hex format - response_hex = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + "hex", True) + response_hex = http_get_call(url.hostname, url.port, '/rest/block/{}.hex'.format(bb_hash), True) assert_equal(response_hex.status, 200) assert_greater_than(int(response_hex.getheader('content-length')), 160) response_hex_str = response_hex.read() assert_equal(encode(response_str, "hex_codec")[0:160], response_hex_str[0:160]) # Compare with hex block header - response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "hex", True) + response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.hex'.format(bb_hash), True) assert_equal(response_header_hex.status, 200) assert_greater_than(int(response_header_hex.getheader('content-length')), 160) response_header_hex_str = response_header_hex.read() @@ -252,12 +246,12 @@ class RESTTest (BitcoinTestFramework): assert_equal(encode(response_header_str, "hex_codec")[0:160], response_header_hex_str[0:160]) # Check json format - block_json_string = http_get_call(url.hostname, url.port, '/rest/block/' + bb_hash + self.FORMAT_SEPARATOR + 'json') + block_json_string = http_get_call(url.hostname, url.port, '/rest/block/{}.json'.format(bb_hash)) block_json_obj = json.loads(block_json_string) assert_equal(block_json_obj['hash'], bb_hash) # Compare with json block header - response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/' + bb_hash + self.FORMAT_SEPARATOR + "json", True) + response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/1/{}.json'.format(bb_hash), True) assert_equal(response_header_json.status, 200) response_header_json_str = response_header_json.read().decode('utf-8') json_obj = json.loads(response_header_json_str, parse_float=Decimal) @@ -272,7 +266,7 @@ class RESTTest (BitcoinTestFramework): # See if we can get 5 headers in one response self.nodes[1].generate(5) self.sync_all() - response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/' + bb_hash + self.FORMAT_SEPARATOR + "json", True) + response_header_json = http_get_call(url.hostname, url.port, '/rest/headers/5/{}.json'.format(bb_hash), True) assert_equal(response_header_json.status, 200) response_header_json_str = response_header_json.read().decode('utf-8') json_obj = json.loads(response_header_json_str) @@ -281,12 +275,12 @@ class RESTTest (BitcoinTestFramework): self.log.info("Test the /tx URI") tx_hash = block_json_obj['tx'][0]['txid'] - json_string = http_get_call(url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "json") + json_string = http_get_call(url.hostname, url.port, '/rest/tx/{}.json'.format(tx_hash)) json_obj = json.loads(json_string) assert_equal(json_obj['txid'], tx_hash) # Check hex format response - hex_string = http_get_call(url.hostname, url.port, '/rest/tx/' + tx_hash + self.FORMAT_SEPARATOR + "hex", True) + hex_string = http_get_call(url.hostname, url.port, '/rest/tx/{}.hex'.format(tx_hash), True) assert_equal(hex_string.status, 200) assert_greater_than(int(response.getheader('content-length')), 10) @@ -300,14 +294,14 @@ class RESTTest (BitcoinTestFramework): self.sync_all() # Check that there are exactly 3 transactions in the TX memory pool before generating the block - json_string = http_get_call(url.hostname, url.port, '/rest/mempool/info' + self.FORMAT_SEPARATOR + 'json') + json_string = http_get_call(url.hostname, url.port, '/rest/mempool/info.json') json_obj = json.loads(json_string) assert_equal(json_obj['size'], 3) # the size of the memory pool should be greater than 3x ~100 bytes assert_greater_than(json_obj['bytes'], 300) # Check that there are our submitted transactions in the TX memory pool - json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents' + self.FORMAT_SEPARATOR + 'json') + json_string = http_get_call(url.hostname, url.port, '/rest/mempool/contents.json') json_obj = json.loads(json_string) for i, tx in enumerate(txs): assert_equal(tx in json_obj, True) @@ -319,14 +313,14 @@ class RESTTest (BitcoinTestFramework): self.sync_all() # Check if the 3 tx show up in the new block - json_string = http_get_call(url.hostname, url.port, '/rest/block/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json') + json_string = http_get_call(url.hostname, url.port, '/rest/block/{}.json'.format(newblockhash[0])) json_obj = json.loads(json_string) for tx in json_obj['tx']: if 'coinbase' not in tx['vin'][0]: # exclude coinbase assert_equal(tx['txid'] in txs, True) # Check the same but without tx details - json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/' + newblockhash[0] + self.FORMAT_SEPARATOR + 'json') + json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/{}.json'.format(newblockhash[0])) json_obj = json.loads(json_string) for tx in txs: assert_equal(tx in json_obj['tx'], True) |