|
4 | 4 | # file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | 5 |
|
6 | 6 | # |
7 | | -# Test REST interface |
| 7 | +# Test rpc http basics |
8 | 8 | # |
9 | 9 |
|
10 | 10 | from test_framework.test_framework import BitcoinTestFramework |
|
20 | 20 | except ImportError: |
21 | 21 | import urlparse |
22 | 22 |
|
23 | | -class HTTPBasicsTest (BitcoinTestFramework): |
| 23 | +class HTTPBasicsTest (BitcoinTestFramework): |
24 | 24 | def setup_nodes(self): |
25 | 25 | return start_nodes(4, self.options.tmpdir, extra_args=[['-rpckeepalive=1'], ['-rpckeepalive=0'], [], []]) |
26 | 26 |
|
27 | | - def run_test(self): |
28 | | - |
| 27 | + def run_test(self): |
| 28 | + |
29 | 29 | ################################################# |
30 | 30 | # lowlevel check for http persistent connection # |
31 | 31 | ################################################# |
32 | 32 | url = urlparse.urlparse(self.nodes[0].url) |
33 | 33 | authpair = url.username + ':' + url.password |
34 | 34 | headers = {"Authorization": "Basic " + base64.b64encode(authpair)} |
35 | | - |
| 35 | + |
36 | 36 | conn = httplib.HTTPConnection(url.hostname, url.port) |
37 | 37 | conn.connect() |
38 | 38 | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
39 | 39 | out1 = conn.getresponse().read(); |
40 | 40 | assert_equal('"error":null' in out1, True) |
41 | 41 | assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open! |
42 | | - |
| 42 | + |
43 | 43 | #send 2nd request without closing connection |
44 | 44 | conn.request('POST', '/', '{"method": "getchaintips"}', headers) |
45 | 45 | out2 = conn.getresponse().read(); |
46 | 46 | assert_equal('"error":null' in out1, True) #must also response with a correct json-rpc message |
47 | 47 | assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open! |
48 | 48 | conn.close() |
49 | | - |
| 49 | + |
50 | 50 | #same should be if we add keep-alive because this should be the std. behaviour |
51 | 51 | headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection": "keep-alive"} |
52 | | - |
| 52 | + |
53 | 53 | conn = httplib.HTTPConnection(url.hostname, url.port) |
54 | 54 | conn.connect() |
55 | 55 | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
56 | 56 | out1 = conn.getresponse().read(); |
57 | 57 | assert_equal('"error":null' in out1, True) |
58 | 58 | assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open! |
59 | | - |
| 59 | + |
60 | 60 | #send 2nd request without closing connection |
61 | 61 | conn.request('POST', '/', '{"method": "getchaintips"}', headers) |
62 | 62 | out2 = conn.getresponse().read(); |
63 | 63 | assert_equal('"error":null' in out1, True) #must also response with a correct json-rpc message |
64 | 64 | assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open! |
65 | 65 | conn.close() |
66 | | - |
| 66 | + |
67 | 67 | #now do the same with "Connection: close" |
68 | 68 | headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection":"close"} |
69 | | - |
| 69 | + |
70 | 70 | conn = httplib.HTTPConnection(url.hostname, url.port) |
71 | 71 | conn.connect() |
72 | 72 | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
73 | 73 | out1 = conn.getresponse().read(); |
74 | 74 | assert_equal('"error":null' in out1, True) |
75 | | - assert_equal(conn.sock!=None, False) #now the connection must be closed after the response |
76 | | - |
| 75 | + assert_equal(conn.sock!=None, False) #now the connection must be closed after the response |
| 76 | + |
77 | 77 | #node1 (2nd node) is running with disabled keep-alive option |
78 | 78 | urlNode1 = urlparse.urlparse(self.nodes[1].url) |
79 | 79 | authpair = urlNode1.username + ':' + urlNode1.password |
80 | 80 | headers = {"Authorization": "Basic " + base64.b64encode(authpair)} |
81 | | - |
| 81 | + |
82 | 82 | conn = httplib.HTTPConnection(urlNode1.hostname, urlNode1.port) |
83 | 83 | conn.connect() |
84 | 84 | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
85 | 85 | out1 = conn.getresponse().read(); |
86 | 86 | assert_equal('"error":null' in out1, True) |
87 | 87 | assert_equal(conn.sock!=None, False) #connection must be closed because keep-alive was set to false |
88 | | - |
| 88 | + |
89 | 89 | #node2 (third node) is running with standard keep-alive parameters which means keep-alive is off |
90 | 90 | urlNode2 = urlparse.urlparse(self.nodes[2].url) |
91 | 91 | authpair = urlNode2.username + ':' + urlNode2.password |
92 | 92 | headers = {"Authorization": "Basic " + base64.b64encode(authpair)} |
93 | | - |
| 93 | + |
94 | 94 | conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port) |
95 | 95 | conn.connect() |
96 | 96 | conn.request('POST', '/', '{"method": "getbestblockhash"}', headers) |
97 | 97 | out1 = conn.getresponse().read(); |
98 | 98 | assert_equal('"error":null' in out1, True) |
99 | 99 | assert_equal(conn.sock!=None, True) #connection must be closed because bitcoind should use keep-alive by default |
100 | | - |
| 100 | + |
101 | 101 | if __name__ == '__main__': |
102 | 102 | HTTPBasicsTest ().main () |
0 commit comments