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 *
13 class HTTPBasicsTest (BitcoinTestFramework
):
14 def set_test_params(self
):
17 def setup_network(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
)
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!
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
)
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!
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
)
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
)
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
)
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
)
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
)
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 ()