Merge #11713: Fix for mismatched extern definition in wallet tests
[bitcoinplatinum.git] / test / functional / httpbasics.py
blobc7682cb49df422df050939a9cb729ecf528f5811
1 #!/usr/bin/env python3
2 # Copyright (c) 2014-2016 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 """Test the RPC HTTP basics."""
7 from test_framework.test_framework import BitcoinTestFramework
8 from test_framework.util import *
10 import http.client
11 import urllib.parse
13 class HTTPBasicsTest (BitcoinTestFramework):
14 def set_test_params(self):
15 self.num_nodes = 3
17 def setup_network(self):
18 self.setup_nodes()
20 def run_test(self):
22 #################################################
23 # lowlevel check for http persistent connection #
24 #################################################
25 url = urllib.parse.urlparse(self.nodes[0].url)
26 authpair = url.username + ':' + url.password
27 headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
29 conn = http.client.HTTPConnection(url.hostname, url.port)
30 conn.connect()
31 conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
32 out1 = conn.getresponse().read()
33 assert(b'"error":null' in out1)
34 assert(conn.sock!=None) #according to http/1.1 connection must still be open!
36 #send 2nd request without closing connection
37 conn.request('POST', '/', '{"method": "getchaintips"}', headers)
38 out1 = conn.getresponse().read()
39 assert(b'"error":null' in out1) #must also response with a correct json-rpc message
40 assert(conn.sock!=None) #according to http/1.1 connection must still be open!
41 conn.close()
43 #same should be if we add keep-alive because this should be the std. behaviour
44 headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection": "keep-alive"}
46 conn = http.client.HTTPConnection(url.hostname, url.port)
47 conn.connect()
48 conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
49 out1 = conn.getresponse().read()
50 assert(b'"error":null' in out1)
51 assert(conn.sock!=None) #according to http/1.1 connection must still be open!
53 #send 2nd request without closing connection
54 conn.request('POST', '/', '{"method": "getchaintips"}', headers)
55 out1 = conn.getresponse().read()
56 assert(b'"error":null' in out1) #must also response with a correct json-rpc message
57 assert(conn.sock!=None) #according to http/1.1 connection must still be open!
58 conn.close()
60 #now do the same with "Connection: close"
61 headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection":"close"}
63 conn = http.client.HTTPConnection(url.hostname, url.port)
64 conn.connect()
65 conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
66 out1 = conn.getresponse().read()
67 assert(b'"error":null' in out1)
68 assert(conn.sock==None) #now the connection must be closed after the response
70 #node1 (2nd node) is running with disabled keep-alive option
71 urlNode1 = urllib.parse.urlparse(self.nodes[1].url)
72 authpair = urlNode1.username + ':' + urlNode1.password
73 headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
75 conn = http.client.HTTPConnection(urlNode1.hostname, urlNode1.port)
76 conn.connect()
77 conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
78 out1 = conn.getresponse().read()
79 assert(b'"error":null' in out1)
81 #node2 (third node) is running with standard keep-alive parameters which means keep-alive is on
82 urlNode2 = urllib.parse.urlparse(self.nodes[2].url)
83 authpair = urlNode2.username + ':' + urlNode2.password
84 headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
86 conn = http.client.HTTPConnection(urlNode2.hostname, urlNode2.port)
87 conn.connect()
88 conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
89 out1 = conn.getresponse().read()
90 assert(b'"error":null' in out1)
91 assert(conn.sock!=None) #connection must be closed because bitcoind should use keep-alive by default
93 # Check excessive request size
94 conn = http.client.HTTPConnection(urlNode2.hostname, urlNode2.port)
95 conn.connect()
96 conn.request('GET', '/' + ('x'*1000), '', headers)
97 out1 = conn.getresponse()
98 assert_equal(out1.status, http.client.NOT_FOUND)
100 conn = http.client.HTTPConnection(urlNode2.hostname, urlNode2.port)
101 conn.connect()
102 conn.request('GET', '/' + ('x'*10000), '', headers)
103 out1 = conn.getresponse()
104 assert_equal(out1.status, http.client.BAD_REQUEST)
107 if __name__ == '__main__':
108 HTTPBasicsTest ().main ()