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
):
17 self
.setup_clean_chain
= False
19 def setup_network(self
):
24 #################################################
25 # lowlevel check for http persistent connection #
26 #################################################
27 url
= urllib
.parse
.urlparse(self
.nodes
[0].url
)
28 authpair
= url
.username
+ ':' + url
.password
29 headers
= {"Authorization": "Basic " + str_to_b64str(authpair
)}
31 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
33 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
34 out1
= conn
.getresponse().read()
35 assert(b
'"error":null' in out1
)
36 assert(conn
.sock
!=None) #according to http/1.1 connection must still be open!
38 #send 2nd request without closing connection
39 conn
.request('POST', '/', '{"method": "getchaintips"}', headers
)
40 out1
= conn
.getresponse().read()
41 assert(b
'"error":null' in out1
) #must also response with a correct json-rpc message
42 assert(conn
.sock
!=None) #according to http/1.1 connection must still be open!
45 #same should be if we add keep-alive because this should be the std. behaviour
46 headers
= {"Authorization": "Basic " + str_to_b64str(authpair
), "Connection": "keep-alive"}
48 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
50 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
51 out1
= conn
.getresponse().read()
52 assert(b
'"error":null' in out1
)
53 assert(conn
.sock
!=None) #according to http/1.1 connection must still be open!
55 #send 2nd request without closing connection
56 conn
.request('POST', '/', '{"method": "getchaintips"}', headers
)
57 out1
= conn
.getresponse().read()
58 assert(b
'"error":null' in out1
) #must also response with a correct json-rpc message
59 assert(conn
.sock
!=None) #according to http/1.1 connection must still be open!
62 #now do the same with "Connection: close"
63 headers
= {"Authorization": "Basic " + str_to_b64str(authpair
), "Connection":"close"}
65 conn
= http
.client
.HTTPConnection(url
.hostname
, url
.port
)
67 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
68 out1
= conn
.getresponse().read()
69 assert(b
'"error":null' in out1
)
70 assert(conn
.sock
==None) #now the connection must be closed after the response
72 #node1 (2nd node) is running with disabled keep-alive option
73 urlNode1
= urllib
.parse
.urlparse(self
.nodes
[1].url
)
74 authpair
= urlNode1
.username
+ ':' + urlNode1
.password
75 headers
= {"Authorization": "Basic " + str_to_b64str(authpair
)}
77 conn
= http
.client
.HTTPConnection(urlNode1
.hostname
, urlNode1
.port
)
79 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
80 out1
= conn
.getresponse().read()
81 assert(b
'"error":null' in out1
)
83 #node2 (third node) is running with standard keep-alive parameters which means keep-alive is on
84 urlNode2
= urllib
.parse
.urlparse(self
.nodes
[2].url
)
85 authpair
= urlNode2
.username
+ ':' + urlNode2
.password
86 headers
= {"Authorization": "Basic " + str_to_b64str(authpair
)}
88 conn
= http
.client
.HTTPConnection(urlNode2
.hostname
, urlNode2
.port
)
90 conn
.request('POST', '/', '{"method": "getbestblockhash"}', headers
)
91 out1
= conn
.getresponse().read()
92 assert(b
'"error":null' in out1
)
93 assert(conn
.sock
!=None) #connection must be closed because bitcoind should use keep-alive by default
95 # Check excessive request size
96 conn
= http
.client
.HTTPConnection(urlNode2
.hostname
, urlNode2
.port
)
98 conn
.request('GET', '/' + ('x'*1000), '', headers
)
99 out1
= conn
.getresponse()
100 assert_equal(out1
.status
, http
.client
.NOT_FOUND
)
102 conn
= http
.client
.HTTPConnection(urlNode2
.hostname
, urlNode2
.port
)
104 conn
.request('GET', '/' + ('x'*10000), '', headers
)
105 out1
= conn
.getresponse()
106 assert_equal(out1
.status
, http
.client
.BAD_REQUEST
)
109 if __name__
== '__main__':
110 HTTPBasicsTest ().main ()