Skip to content

Commit 7026cbd

Browse files
committed
Merge pull request #5379
01dc2d8 [REST] add REST interface tests in rpc-test section (Jonas Schnelli)
2 parents f0877f8 + 01dc2d8 commit 7026cbd

File tree

3 files changed

+68
-1
lines changed

3 files changed

+68
-1
lines changed

qa/pull-tester/rpc-tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ if [ "x${ENABLE_BITCOIND}${ENABLE_UTILS}${ENABLE_WALLET}" = "x111" ]; then
2121
${BUILDDIR}/qa/rpc-tests/txn_doublespend.py --srcdir "${BUILDDIR}/src"
2222
${BUILDDIR}/qa/rpc-tests/txn_doublespend.py --mineblock --srcdir "${BUILDDIR}/src"
2323
${BUILDDIR}/qa/rpc-tests/getchaintips.py --srcdir "${BUILDDIR}/src"
24+
${BUILDDIR}/qa/rpc-tests/rest.py --srcdir "${BUILDDIR}/src"
2425
#${BUILDDIR}/qa/rpc-tests/forknotify.py --srcdir "${BUILDDIR}/src"
2526
else
2627
echo "No rpc tests to run. Wallet, utils, and bitcoind must all be enabled"

qa/rpc-tests/rest.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#!/usr/bin/env python
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 json
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+
def http_get_call(host, port, path, response_object = 0):
24+
conn = httplib.HTTPConnection(host, port)
25+
conn.request('GET', path)
26+
27+
if response_object:
28+
return conn.getresponse()
29+
30+
return conn.getresponse().read()
31+
32+
33+
class RESTTest (BitcoinTestFramework):
34+
FORMAT_SEPARATOR = "/"
35+
36+
def run_test(self):
37+
url = urlparse.urlparse(self.nodes[0].url)
38+
bb_hash = self.nodes[0].getbestblockhash()
39+
40+
# check binary format
41+
response = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True)
42+
assert_equal(response.status, 200)
43+
assert_greater_than(int(response.getheader('content-length')), 10)
44+
45+
# check json format
46+
json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json')
47+
json_obj = json.loads(json_string)
48+
assert_equal(json_obj['hash'], bb_hash)
49+
50+
# do tx test
51+
tx_hash = json_obj['tx'][0];
52+
json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"json")
53+
json_obj = json.loads(json_string)
54+
assert_equal(json_obj['txid'], tx_hash)
55+
56+
# check hex format response
57+
hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True)
58+
assert_equal(response.status, 200)
59+
assert_greater_than(int(response.getheader('content-length')), 10)
60+
61+
if __name__ == '__main__':
62+
RESTTest ().main ()

qa/rpc-tests/util.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ def start_node(i, dirname, extra_args=None, rpchost=None):
163163
Start a bitcoind and return RPC connection to it
164164
"""
165165
datadir = os.path.join(dirname, "node"+str(i))
166-
args = [ os.getenv("BITCOIND", "bitcoind"), "-datadir="+datadir, "-keypool=1", "-discover=0" ]
166+
args = [ os.getenv("BITCOIND", "bitcoind"), "-datadir="+datadir, "-keypool=1", "-discover=0", "-rest" ]
167167
if extra_args is not None: args.extend(extra_args)
168168
bitcoind_processes[i] = subprocess.Popen(args)
169169
devnull = open("/dev/null", "w+")
@@ -327,3 +327,7 @@ def random_transaction(nodes, amount, min_fee, fee_increment, fee_variants):
327327
def assert_equal(thing1, thing2):
328328
if thing1 != thing2:
329329
raise AssertionError("%s != %s"%(str(thing1),str(thing2)))
330+
331+
def assert_greater_than(thing1, thing2):
332+
if thing1 <= thing2:
333+
raise AssertionError("%s <= %s"%(str(thing1),str(thing2)))

0 commit comments

Comments
 (0)