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

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