Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

httpbasics.py 4.7KB

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