|
23 | 23 | def http_get_call(host, port, path, response_object = 0): |
24 | 24 | conn = httplib.HTTPConnection(host, port) |
25 | 25 | conn.request('GET', path) |
26 | | - |
| 26 | + |
27 | 27 | if response_object: |
28 | 28 | return conn.getresponse() |
29 | | - |
| 29 | + |
30 | 30 | return conn.getresponse().read() |
31 | 31 |
|
32 | 32 |
|
33 | 33 | class RESTTest (BitcoinTestFramework): |
34 | 34 | FORMAT_SEPARATOR = "." |
35 | | - |
| 35 | + |
36 | 36 | def run_test(self): |
37 | 37 | url = urlparse.urlparse(self.nodes[0].url) |
38 | 38 | bb_hash = self.nodes[0].getbestblockhash() |
39 | | - |
| 39 | + |
40 | 40 | # check binary format |
41 | 41 | response = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True) |
42 | 42 | assert_equal(response.status, 200) |
43 | | - assert_greater_than(int(response.getheader('content-length')), 10) |
44 | | - |
| 43 | + assert_greater_than(int(response.getheader('content-length')), 80) |
| 44 | + response_str = response.read() |
| 45 | + |
| 46 | + # compare with block header |
| 47 | + response_header = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True) |
| 48 | + assert_equal(response_header.status, 200) |
| 49 | + assert_equal(int(response_header.getheader('content-length')), 80) |
| 50 | + response_header_str = response_header.read() |
| 51 | + assert_equal(response_str[0:80], response_header_str) |
| 52 | + |
| 53 | + # check block hex format |
| 54 | + response_hex = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True) |
| 55 | + assert_equal(response_hex.status, 200) |
| 56 | + assert_greater_than(int(response_hex.getheader('content-length')), 160) |
| 57 | + response_hex_str = response_hex.read() |
| 58 | + assert_equal(response_str.encode("hex")[0:160], response_hex_str[0:160]) |
| 59 | + |
| 60 | + # compare with hex block header |
| 61 | + response_header_hex = http_get_call(url.hostname, url.port, '/rest/headers/1/'+bb_hash+self.FORMAT_SEPARATOR+"hex", True) |
| 62 | + assert_equal(response_header_hex.status, 200) |
| 63 | + assert_greater_than(int(response_header_hex.getheader('content-length')), 160) |
| 64 | + response_header_hex_str = response_header_hex.read() |
| 65 | + assert_equal(response_hex_str[0:160], response_header_hex_str[0:160]) |
| 66 | + assert_equal(response_header_str.encode("hex")[0:160], response_header_hex_str[0:160]) |
| 67 | + |
45 | 68 | # check json format |
46 | 69 | json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json') |
47 | 70 | json_obj = json.loads(json_string) |
48 | 71 | assert_equal(json_obj['hash'], bb_hash) |
49 | | - |
| 72 | + |
50 | 73 | # do tx test |
51 | 74 | tx_hash = json_obj['tx'][0]['txid']; |
52 | 75 | json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"json") |
53 | 76 | json_obj = json.loads(json_string) |
54 | 77 | assert_equal(json_obj['txid'], tx_hash) |
55 | | - |
| 78 | + |
56 | 79 | # check hex format response |
57 | 80 | hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True) |
58 | 81 | assert_equal(response.status, 200) |
59 | 82 | assert_greater_than(int(response.getheader('content-length')), 10) |
60 | | - |
| 83 | + |
61 | 84 | # check block tx details |
62 | 85 | # let's make 3 tx and mine them on node 1 |
63 | 86 | txs = [] |
64 | 87 | txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) |
65 | 88 | txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) |
66 | 89 | txs.append(self.nodes[0].sendtoaddress(self.nodes[2].getnewaddress(), 11)) |
67 | 90 | self.sync_all() |
68 | | - |
| 91 | + |
69 | 92 | # now mine the transactions |
70 | 93 | newblockhash = self.nodes[1].setgenerate(True, 1) |
71 | 94 | self.sync_all() |
72 | | - |
| 95 | + |
73 | 96 | #check if the 3 tx show up in the new block |
74 | 97 | json_string = http_get_call(url.hostname, url.port, '/rest/block/'+newblockhash[0]+self.FORMAT_SEPARATOR+'json') |
75 | 98 | json_obj = json.loads(json_string) |
76 | 99 | for tx in json_obj['tx']: |
77 | 100 | if not 'coinbase' in tx['vin'][0]: #exclude coinbase |
78 | 101 | assert_equal(tx['txid'] in txs, True) |
79 | | - |
| 102 | + |
80 | 103 | #check the same but without tx details |
81 | 104 | json_string = http_get_call(url.hostname, url.port, '/rest/block/notxdetails/'+newblockhash[0]+self.FORMAT_SEPARATOR+'json') |
82 | 105 | json_obj = json.loads(json_string) |
83 | 106 | for tx in txs: |
84 | 107 | assert_equal(tx in json_obj['tx'], True) |
85 | | - |
86 | | - |
87 | 108 |
|
88 | 109 | if __name__ == '__main__': |
89 | 110 | RESTTest ().main () |
0 commit comments