From 01dc2d83f80d8890ccb197110bbeb328ab68c285 Mon Sep 17 00:00:00 2001 From: Jonas Schnelli Date: Wed, 26 Nov 2014 16:26:02 +0100 Subject: [PATCH] [REST] add REST interface tests in rpc-test section --- qa/pull-tester/rpc-tests.sh | 1 + qa/rpc-tests/rest.py | 62 +++++++++++++++++++++++++++++++++++++ qa/rpc-tests/util.py | 6 +++- 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100755 qa/rpc-tests/rest.py diff --git a/qa/pull-tester/rpc-tests.sh b/qa/pull-tester/rpc-tests.sh index 2f1e24b78d..a064fb81e9 100755 --- a/qa/pull-tester/rpc-tests.sh +++ b/qa/pull-tester/rpc-tests.sh @@ -21,6 +21,7 @@ if [ "x${ENABLE_BITCOIND}${ENABLE_UTILS}${ENABLE_WALLET}" = "x111" ]; then ${BUILDDIR}/qa/rpc-tests/txn_doublespend.py --srcdir "${BUILDDIR}/src" ${BUILDDIR}/qa/rpc-tests/txn_doublespend.py --mineblock --srcdir "${BUILDDIR}/src" ${BUILDDIR}/qa/rpc-tests/getchaintips.py --srcdir "${BUILDDIR}/src" + ${BUILDDIR}/qa/rpc-tests/rest.py --srcdir "${BUILDDIR}/src" #${BUILDDIR}/qa/rpc-tests/forknotify.py --srcdir "${BUILDDIR}/src" else echo "No rpc tests to run. Wallet, utils, and bitcoind must all be enabled" diff --git a/qa/rpc-tests/rest.py b/qa/rpc-tests/rest.py new file mode 100755 index 0000000000..ca09a5c6d8 --- /dev/null +++ b/qa/rpc-tests/rest.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +# Copyright (c) 2014 The Bitcoin Core developers +# Distributed under the MIT software license, see the accompanying +# file COPYING or http://www.opensource.org/licenses/mit-license.php. + +# +# Test REST interface +# + +from test_framework import BitcoinTestFramework +from util import * +import json + +try: + import http.client as httplib +except ImportError: + import httplib +try: + import urllib.parse as urlparse +except ImportError: + import urlparse + +def http_get_call(host, port, path, response_object = 0): + conn = httplib.HTTPConnection(host, port) + conn.request('GET', path) + + if response_object: + return conn.getresponse() + + return conn.getresponse().read() + + +class RESTTest (BitcoinTestFramework): + FORMAT_SEPARATOR = "/" + + def run_test(self): + url = urlparse.urlparse(self.nodes[0].url) + bb_hash = self.nodes[0].getbestblockhash() + + # check binary format + response = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+"bin", True) + assert_equal(response.status, 200) + assert_greater_than(int(response.getheader('content-length')), 10) + + # check json format + json_string = http_get_call(url.hostname, url.port, '/rest/block/'+bb_hash+self.FORMAT_SEPARATOR+'json') + json_obj = json.loads(json_string) + assert_equal(json_obj['hash'], bb_hash) + + # do tx test + tx_hash = json_obj['tx'][0]; + json_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"json") + json_obj = json.loads(json_string) + assert_equal(json_obj['txid'], tx_hash) + + # check hex format response + hex_string = http_get_call(url.hostname, url.port, '/rest/tx/'+tx_hash+self.FORMAT_SEPARATOR+"hex", True) + assert_equal(response.status, 200) + assert_greater_than(int(response.getheader('content-length')), 10) + +if __name__ == '__main__': + RESTTest ().main () \ No newline at end of file diff --git a/qa/rpc-tests/util.py b/qa/rpc-tests/util.py index bed7fed8ca..6b66bfd8f6 100644 --- a/qa/rpc-tests/util.py +++ b/qa/rpc-tests/util.py @@ -163,7 +163,7 @@ def start_node(i, dirname, extra_args=None, rpchost=None): Start a bitcoind and return RPC connection to it """ datadir = os.path.join(dirname, "node"+str(i)) - args = [ os.getenv("BITCOIND", "bitcoind"), "-datadir="+datadir, "-keypool=1", "-discover=0" ] + args = [ os.getenv("BITCOIND", "bitcoind"), "-datadir="+datadir, "-keypool=1", "-discover=0", "-rest" ] if extra_args is not None: args.extend(extra_args) bitcoind_processes[i] = subprocess.Popen(args) devnull = open("/dev/null", "w+") @@ -327,3 +327,7 @@ def random_transaction(nodes, amount, min_fee, fee_increment, fee_variants): def assert_equal(thing1, thing2): if thing1 != thing2: raise AssertionError("%s != %s"%(str(thing1),str(thing2))) + +def assert_greater_than(thing1, thing2): + if thing1 <= thing2: + raise AssertionError("%s <= %s"%(str(thing1),str(thing2))) \ No newline at end of file