1
+ #!/usr/bin/env python
2
+ # Copyright (c) 2014 The Bitcoin Core developers
3
+ # Distributed under the MIT software license, see the accompanying
4
+ # file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
+
6
+ #
7
+ # Test REST interface
8
+ #
9
+
10
+ from test_framework import BitcoinTestFramework
11
+ from util import *
12
+ import json
13
+
14
+ try :
15
+ import http .client as httplib
16
+ except ImportError :
17
+ import httplib
18
+ try :
19
+ import urllib .parse as urlparse
20
+ except ImportError :
21
+ import urlparse
22
+
23
+ def http_get_call (host , port , path , response_object = 0 ):
24
+ conn = httplib .HTTPConnection (host , port )
25
+ conn .request ('GET' , path )
26
+
27
+ if response_object :
28
+ return conn .getresponse ()
29
+
30
+ return conn .getresponse ().read ()
31
+
32
+
33
+ class RESTTest (BitcoinTestFramework ):
34
+ FORMAT_SEPARATOR = "/"
35
+
36
+ def run_test (self ):
37
+ url = urlparse .urlparse (self .nodes [0 ].url )
38
+ bb_hash = self .nodes [0 ].getbestblockhash ()
39
+
40
+ # check binary format
41
+ response = http_get_call (url .hostname , url .port , '/rest/block/' + bb_hash + self .FORMAT_SEPARATOR + "bin" , True )
42
+ assert_equal (response .status , 200 )
43
+ assert_greater_than (int (response .getheader ('content-length' )), 10 )
44
+
45
+ # check json format
46
+ json_string = http_get_call (url .hostname , url .port , '/rest/block/' + bb_hash + self .FORMAT_SEPARATOR + 'json' )
47
+ json_obj = json .loads (json_string )
48
+ assert_equal (json_obj ['hash' ], bb_hash )
49
+
50
+ # do tx test
51
+ tx_hash = json_obj ['tx' ][0 ];
52
+ json_string = http_get_call (url .hostname , url .port , '/rest/tx/' + tx_hash + self .FORMAT_SEPARATOR + "json" )
53
+ json_obj = json .loads (json_string )
54
+ assert_equal (json_obj ['txid' ], tx_hash )
55
+
56
+ # check hex format response
57
+ hex_string = http_get_call (url .hostname , url .port , '/rest/tx/' + tx_hash + self .FORMAT_SEPARATOR + "hex" , True )
58
+ assert_equal (response .status , 200 )
59
+ assert_greater_than (int (response .getheader ('content-length' )), 10 )
60
+
61
+ if __name__ == '__main__' :
62
+ RESTTest ().main ()
0 commit comments