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.

httpbasics.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #!/usr/bin/env python2
  2. # Copyright (c) 2014-2015 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. # Test rpc http basics
  7. #
  8. from test_framework.test_framework import BitcoinTestFramework
  9. from test_framework.util import *
  10. import base64
  11. try:
  12. import http.client as httplib
  13. except ImportError:
  14. import httplib
  15. try:
  16. import urllib.parse as urlparse
  17. except ImportError:
  18. import urlparse
  19. class HTTPBasicsTest (BitcoinTestFramework):
  20. def setup_nodes(self):
  21. return start_nodes(4, self.options.tmpdir)
  22. def run_test(self):
  23. #################################################
  24. # lowlevel check for http persistent connection #
  25. #################################################
  26. url = urlparse.urlparse(self.nodes[0].url)
  27. authpair = url.username + ':' + url.password
  28. headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
  29. conn = httplib.HTTPConnection(url.hostname, url.port)
  30. conn.connect()
  31. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  32. out1 = conn.getresponse().read();
  33. assert_equal('"error":null' in out1, True)
  34. assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
  35. #send 2nd request without closing connection
  36. conn.request('POST', '/', '{"method": "getchaintips"}', headers)
  37. out2 = conn.getresponse().read();
  38. assert_equal('"error":null' in out1, True) #must also response with a correct json-rpc message
  39. assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
  40. conn.close()
  41. #same should be if we add keep-alive because this should be the std. behaviour
  42. headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection": "keep-alive"}
  43. conn = httplib.HTTPConnection(url.hostname, url.port)
  44. conn.connect()
  45. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  46. out1 = conn.getresponse().read();
  47. assert_equal('"error":null' in out1, True)
  48. assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
  49. #send 2nd request without closing connection
  50. conn.request('POST', '/', '{"method": "getchaintips"}', headers)
  51. out2 = conn.getresponse().read();
  52. assert_equal('"error":null' in out1, True) #must also response with a correct json-rpc message
  53. assert_equal(conn.sock!=None, True) #according to http/1.1 connection must still be open!
  54. conn.close()
  55. #now do the same with "Connection: close"
  56. headers = {"Authorization": "Basic " + base64.b64encode(authpair), "Connection":"close"}
  57. conn = httplib.HTTPConnection(url.hostname, url.port)
  58. conn.connect()
  59. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  60. out1 = conn.getresponse().read();
  61. assert_equal('"error":null' in out1, True)
  62. assert_equal(conn.sock!=None, False) #now the connection must be closed after the response
  63. #node1 (2nd node) is running with disabled keep-alive option
  64. urlNode1 = urlparse.urlparse(self.nodes[1].url)
  65. authpair = urlNode1.username + ':' + urlNode1.password
  66. headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
  67. conn = httplib.HTTPConnection(urlNode1.hostname, urlNode1.port)
  68. conn.connect()
  69. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  70. out1 = conn.getresponse().read();
  71. assert_equal('"error":null' in out1, True)
  72. #node2 (third node) is running with standard keep-alive parameters which means keep-alive is on
  73. urlNode2 = urlparse.urlparse(self.nodes[2].url)
  74. authpair = urlNode2.username + ':' + urlNode2.password
  75. headers = {"Authorization": "Basic " + base64.b64encode(authpair)}
  76. conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
  77. conn.connect()
  78. conn.request('POST', '/', '{"method": "getbestblockhash"}', headers)
  79. out1 = conn.getresponse().read();
  80. assert_equal('"error":null' in out1, True)
  81. assert_equal(conn.sock!=None, True) #connection must be closed because bitcoind should use keep-alive by default
  82. # Check excessive request size
  83. conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
  84. conn.connect()
  85. conn.request('GET', '/' + ('x'*1000), '', headers)
  86. out1 = conn.getresponse()
  87. assert_equal(out1.status, httplib.NOT_FOUND)
  88. conn = httplib.HTTPConnection(urlNode2.hostname, urlNode2.port)
  89. conn.connect()
  90. conn.request('GET', '/' + ('x'*10000), '', headers)
  91. out1 = conn.getresponse()
  92. assert_equal(out1.status, httplib.BAD_REQUEST)
  93. if __name__ == '__main__':
  94. HTTPBasicsTest ().main ()