|
| 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