You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
111 lines
4.7 KiB
111 lines
4.7 KiB
![]()
9 years ago
|
#!/usr/bin/env python3
|
||
|
# Copyright (c) 2014-2016 The Bitcoin Core developers
|
||
![]()
10 years ago
|
# Distributed under the MIT software license, see the accompanying
|
||
|
# file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||
![]()
8 years ago
|
"""Test the RPC HTTP basics."""
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
from test_framework.test_framework import BitcoinTestFramework
|
||
|
from test_framework.util import *
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
import http.client
|
||
|
import urllib.parse
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
class HTTPBasicsTest (BitcoinTestFramework):
|
||
![]()
8 years ago
|
def __init__(self):
|
||
|
super().__init__()
|
||
|
self.num_nodes = 3
|
||
|
self.setup_clean_chain = False
|
||
|
|
||
|
def setup_network(self):
|
||
|
self.nodes = self.setup_nodes()
|
||
![]()
10 years ago
|
|
||
![]()
9 years ago
|
def run_test(self):
|
||
|
|
||
![]()
10 years ago
|
#################################################
|
||
|
# lowlevel check for http persistent connection #
|
||
|
#################################################
|
||
![]()
9 years ago
|
url = urllib.parse.urlparse(self.nodes[0].url)
|
||
![]()
10 years ago
|
authpair = url.username + ':' + url.password
|
||
![]()
9 years ago
|
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
||
![]()
10 years ago
|
conn.connect()
|
||
![]()
10 years ago
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||
![]()
9 years ago
|
out1 = conn.getresponse().read()
|
||
![]()
9 years ago
|
assert(b'"error":null' in out1)
|
||
![]()
9 years ago
|
assert(conn.sock!=None) #according to http/1.1 connection must still be open!
|
||
![]()
9 years ago
|
|
||
![]()
10 years ago
|
#send 2nd request without closing connection
|
||
![]()
10 years ago
|
conn.request('POST', '/', '{"method": "getchaintips"}', headers)
|
||
![]()
9 years ago
|
out1 = conn.getresponse().read()
|
||
|
assert(b'"error":null' in out1) #must also response with a correct json-rpc message
|
||
![]()
9 years ago
|
assert(conn.sock!=None) #according to http/1.1 connection must still be open!
|
||
![]()
10 years ago
|
conn.close()
|
||
![]()
9 years ago
|
|
||
![]()
10 years ago
|
#same should be if we add keep-alive because this should be the std. behaviour
|
||
![]()
9 years ago
|
headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection": "keep-alive"}
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
||
![]()
10 years ago
|
conn.connect()
|
||
![]()
10 years ago
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||
![]()
9 years ago
|
out1 = conn.getresponse().read()
|
||
![]()
9 years ago
|
assert(b'"error":null' in out1)
|
||
![]()
9 years ago
|
assert(conn.sock!=None) #according to http/1.1 connection must still be open!
|
||
![]()
9 years ago
|
|
||
![]()
10 years ago
|
#send 2nd request without closing connection
|
||
![]()
10 years ago
|
conn.request('POST', '/', '{"method": "getchaintips"}', headers)
|
||
![]()
9 years ago
|
out1 = conn.getresponse().read()
|
||
|
assert(b'"error":null' in out1) #must also response with a correct json-rpc message
|
||
![]()
9 years ago
|
assert(conn.sock!=None) #according to http/1.1 connection must still be open!
|
||
![]()
10 years ago
|
conn.close()
|
||
![]()
9 years ago
|
|
||
![]()
10 years ago
|
#now do the same with "Connection: close"
|
||
![]()
9 years ago
|
headers = {"Authorization": "Basic " + str_to_b64str(authpair), "Connection":"close"}
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
conn = http.client.HTTPConnection(url.hostname, url.port)
|
||
![]()
10 years ago
|
conn.connect()
|
||
![]()
10 years ago
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||
![]()
9 years ago
|
out1 = conn.getresponse().read()
|
||
![]()
9 years ago
|
assert(b'"error":null' in out1)
|
||
![]()
9 years ago
|
assert(conn.sock==None) #now the connection must be closed after the response
|
||
![]()
9 years ago
|
|
||
![]()
10 years ago
|
#node1 (2nd node) is running with disabled keep-alive option
|
||
![]()
9 years ago
|
urlNode1 = urllib.parse.urlparse(self.nodes[1].url)
|
||
![]()
10 years ago
|
authpair = urlNode1.username + ':' + urlNode1.password
|
||
![]()
9 years ago
|
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
conn = http.client.HTTPConnection(urlNode1.hostname, urlNode1.port)
|
||
![]()
10 years ago
|
conn.connect()
|
||
![]()
10 years ago
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||
![]()
9 years ago
|
out1 = conn.getresponse().read()
|
||
![]()
9 years ago
|
assert(b'"error":null' in out1)
|
||
![]()
9 years ago
|
|
||
![]()
10 years ago
|
#node2 (third node) is running with standard keep-alive parameters which means keep-alive is on
|
||
![]()
9 years ago
|
urlNode2 = urllib.parse.urlparse(self.nodes[2].url)
|
||
![]()
10 years ago
|
authpair = urlNode2.username + ':' + urlNode2.password
|
||
![]()
9 years ago
|
headers = {"Authorization": "Basic " + str_to_b64str(authpair)}
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
conn = http.client.HTTPConnection(urlNode2.hostname, urlNode2.port)
|
||
![]()
10 years ago
|
conn.connect()
|
||
![]()
10 years ago
|
conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
|
||
![]()
9 years ago
|
out1 = conn.getresponse().read()
|
||
![]()
9 years ago
|
assert(b'"error":null' in out1)
|
||
![]()
9 years ago
|
assert(conn.sock!=None) #connection must be closed because bitcoind should use keep-alive by default
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
# Check excessive request size
|
||
![]()
9 years ago
|
conn = http.client.HTTPConnection(urlNode2.hostname, urlNode2.port)
|
||
![]()
9 years ago
|
conn.connect()
|
||
|
conn.request('GET', '/' + ('x'*1000), '', headers)
|
||
|
out1 = conn.getresponse()
|
||
![]()
9 years ago
|
assert_equal(out1.status, http.client.NOT_FOUND)
|
||
![]()
9 years ago
|
|
||
![]()
9 years ago
|
conn = http.client.HTTPConnection(urlNode2.hostname, urlNode2.port)
|
||
![]()
9 years ago
|
conn.connect()
|
||
|
conn.request('GET', '/' + ('x'*10000), '', headers)
|
||
|
out1 = conn.getresponse()
|
||
![]()
9 years ago
|
assert_equal(out1.status, http.client.BAD_REQUEST)
|
||
![]()
9 years ago
|
|
||
|
|
||
![]()
10 years ago
|
if __name__ == '__main__':
|
||
![]()
10 years ago
|
HTTPBasicsTest ().main ()
|