Dash Core  0.12.2.1
P2P Digital Currency
httpbasics.py
Go to the documentation of this file.
1 #!/usr/bin/env python2
2 # Copyright (c) 2014-2015 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 rpc http basics
8 #
9 
10 from test_framework.test_framework import BitcoinTestFramework
11 from test_framework.util import *
12 
13 try:
14  import http.client as httplib
15 except ImportError:
16  import httplib
17 try:
18  import urllib.parse as urlparse
19 except ImportError:
20  import urlparse
21 
23  def setup_nodes(self):
24  return start_nodes(4, self.options.tmpdir)
25 
26  def run_test(self):
27 
28 
31  url = urlparse.urlparse(self.nodes[0].url)
32  authpair = url.username + ':' + url.password
33  headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
34 
35  conn = httplib.HTTPConnection(url.hostname, url.port)
36  conn.connect()
37  conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
38  out1 = conn.getresponse().read()
39  assert(b'"error":null' in out1)
40  assert(conn.sock!=None) #according to http/1.1 connection must still be open!
41 
42  #send 2nd request without closing connection
43  conn.request('POST', '/', '{"method": "getchaintips"}', headers)
44  out1 = conn.getresponse().read()
45  assert(b'"error":null' in out1) #must also response with a correct json-rpc message
46  assert(conn.sock!=None) #according to http/1.1 connection must still be open!
47  conn.close()
48 
49  #same should be if we add keep-alive because this should be the std. behaviour
50  headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection": "keep-alive"}
51 
52  conn = httplib.HTTPConnection(url.hostname, url.port)
53  conn.connect()
54  conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
55  out1 = conn.getresponse().read()
56  assert(b'"error":null' in out1)
57  assert(conn.sock!=None) #according to http/1.1 connection must still be open!
58 
59  #send 2nd request without closing connection
60  conn.request('POST', '/', '{"method": "getchaintips"}', headers)
61  out1 = conn.getresponse().read()
62  assert(b'"error":null' in out1) #must also response with a correct json-rpc message
63  assert(conn.sock!=None) #according to http/1.1 connection must still be open!
64  conn.close()
65 
66  #now do the same with "Connection: close"
67  headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection":"close"}
68 
69  conn = httplib.HTTPConnection(url.hostname, url.port)
70  conn.connect()
71  conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
72  out1 = conn.getresponse().read()
73  assert(b'"error":null' in out1)
74  assert(conn.sock==None) #now the connection must be closed after the response
75 
76  #node1 (2nd node) is running with disabled keep-alive option
77  urlNode1 = urlparse.urlparse(self.nodes[1].url)
78  authpair = urlNode1.username + ':' + urlNode1.password
79  headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
80 
81  conn = httplib.HTTPConnection(urlNode1.hostname, urlNode1.port)
82  conn.connect()
83  conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
84  out1 = conn.getresponse().read()
85  assert(b'"error":null' in out1)
86 
87  #node2 (third node) is running with standard keep-alive parameters which means keep-alive is on
88  urlNode2 = urlparse.urlparse(self.nodes[2].url)
89  authpair = urlNode2.username + ':' + urlNode2.password
90  headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
91 
92  conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
93  conn.connect()
94  conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
95  out1 = conn.getresponse().read()
96  assert(b'"error":null' in out1)
97  assert(conn.sock!=None) #connection must be closed because bitcoind should use keep-alive by default
98 
99  # Check excessive request size
100  conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
101  conn.connect()
102  conn.request('GET', '/' + ('x'*1000), '', headers)
103  out1 = conn.getresponse()
104  assert_equal(out1.status, httplib.NOT_FOUND)
105 
106  conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
107  conn.connect()
108  conn.request('GET', '/' + ('x'*10000), '', headers)
109  out1 = conn.getresponse()
110  assert_equal(out1.status, httplib.BAD_REQUEST)
111 
112 
113 if __name__ == '__main__':
114  HTTPBasicsTest ().main ()
def start_nodes(num_nodes, dirname, extra_args=None, rpchost=None, binary=None)
Definition: util.py:305
def str_to_b64str(string)
Definition: util.py:114
def assert_equal(thing1, thing2)
Definition: util.py:461