Skip to content

Commit 11d7a7d

Browse files
committed
[RPC] add rpc-test for http keep-alive (persistent connections)
1 parent 4383319 commit 11d7a7d

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

qa/pull-tester/rpc-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ if [ "x${ENABLE_BITCOIND}${ENABLE_UTILS}${ENABLE_WALLET}" = "x111" ]; then
2424
${BUILDDIR}/qa/rpc-tests/getchaintips.py --srcdir "${BUILDDIR}/src"
2525
${BUILDDIR}/qa/rpc-tests/rest.py --srcdir "${BUILDDIR}/src"
2626
${BUILDDIR}/qa/rpc-tests/mempool_spendcoinbase.py --srcdir "${BUILDDIR}/src"
27+
${BUILDDIR}/qa/rpc-tests/httpbasics.py --srcdir "${BUILDDIR}/src"
2728
#${BUILDDIR}/qa/rpc-tests/forknotify.py --srcdir "${BUILDDIR}/src"
2829
else
2930
echo "No rpc tests to run. Wallet, utils, and bitcoind must all be enabled"

qa/rpc-tests/httpbasics.py

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#!/usr/bin/env python2
2+
# Copyright (c) 2014 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 REST interface
8+
#
9+
10+
from test_framework import BitcoinTestFramework
11+
from util import *
12+
import base64
13+
14+
try:
15+
import http.client as httplib
16+
except ImportError:
17+
import httplib
18+
try:
19+
import urllib.parse as urlparse
20+
except ImportError:
21+
import urlparse
22+
23+
class RESTTest (BitcoinTestFramework):
24+
def run_test(self):
25+
26+
#################################################
27+
# lowlevel check for http persistent connection #
28+
#################################################
29+
url = urlparse.urlparse(self.nodes[0].url)
30+
authpair = url.username + ':' + url.password
31+
headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
32+
33+
conn = httplib.HTTPConnection(url.hostname, url.port)
34+
conn.connect()
35+
conn.request('GET', '/', '{"method": "getbestblockhash"}', headers)
36+
out1 = conn.getresponse().read();
37+
assert_equal('"error":null' in out1, True)
38+
assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
39+
40+
#send 2nd request without closing connection
41+
conn.request('GET', '/', '{"method": "getchaintips"}', headers)
42+
out2 = conn.getresponse().read();
43+
assert_equal('"error":null' in out1, True) #must also response with a correct json-rpc message
44+
assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
45+
conn.close()
46+
47+
#same should be if we add keep-alive because this should be the std. behaviour
48+
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection": "keep-alive"}
49+
50+
conn = httplib.HTTPConnection(url.hostname, url.port)
51+
conn.connect()
52+
conn.request('GET', '/', '{"method": "getbestblockhash"}', headers)
53+
out1 = conn.getresponse().read();
54+
assert_equal('"error":null' in out1, True)
55+
assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
56+
57+
#send 2nd request without closing connection
58+
conn.request('GET', '/', '{"method": "getchaintips"}', headers)
59+
out2 = conn.getresponse().read();
60+
assert_equal('"error":null' in out1, True) #must also response with a correct json-rpc message
61+
assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
62+
conn.close()
63+
64+
#now do the same with "Connection: close"
65+
headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection":"close"}
66+
67+
conn = httplib.HTTPConnection(url.hostname, url.port)
68+
conn.connect()
69+
conn.request('GET', '/', '{"method": "getbestblockhash"}', headers)
70+
out1 = conn.getresponse().read();
71+
assert_equal('"error":null' in out1, True)
72+
assert_equal(conn.sock!=None, False) #now the connection must be closed after the response
73+
74+
75+
if __name__ == '__main__':
76+
RESTTest ().main ()

0 commit comments

Comments
 (0)