client.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #!/usr/bin/env python
  2. #
  3. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. from __future__ import print_function
  17. from __future__ import unicode_literals
  18. from future import standard_library
  19. standard_library.install_aliases()
  20. from builtins import str
  21. import http.client
  22. import argparse
  23. def verbose_print(verbosity, *args):
  24. if (verbosity):
  25. Utility.console_log(''.join(str(elems) for elems in args))
  26. def test_get_handler(ip, port, verbosity = False):
  27. verbose_print(verbosity, "======== GET HANDLER TEST =============")
  28. # Establish HTTP connection
  29. verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
  30. sess = http.client.HTTPConnection(ip + ":" + port, timeout = 15)
  31. uri = "/hello?query1=value1&query2=value2&query3=value3"
  32. # GET hello response
  33. test_headers = {"Test-Header-1":"Test-Value-1", "Test-Header-2":"Test-Value-2"}
  34. verbose_print(verbosity, "Sending GET to URI : ", uri)
  35. verbose_print(verbosity, "Sending additional headers : ")
  36. for k, v in test_headers.items():
  37. verbose_print(verbosity, "\t", k, ": ", v)
  38. sess.request("GET", url=uri, headers=test_headers)
  39. resp = sess.getresponse()
  40. resp_hdrs = resp.getheaders()
  41. resp_data = resp.read().decode()
  42. try:
  43. if resp.getheader("Custom-Header-1") != "Custom-Value-1":
  44. return False
  45. if resp.getheader("Custom-Header-2") != "Custom-Value-2":
  46. return False
  47. except:
  48. return False
  49. verbose_print(verbosity, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
  50. verbose_print(verbosity, "Server response to GET /hello")
  51. verbose_print(verbosity, "Response Headers : ")
  52. for k, v in resp_hdrs:
  53. verbose_print(verbosity, "\t", k, ": ", v)
  54. verbose_print(verbosity, "Response Data : " + resp_data)
  55. verbose_print(verbosity, "========================================\n")
  56. # Close HTTP connection
  57. sess.close()
  58. return (resp_data == "Hello World!")
  59. def test_post_handler(ip, port, msg, verbosity = False):
  60. verbose_print(verbosity, "======== POST HANDLER TEST ============")
  61. # Establish HTTP connection
  62. verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
  63. sess = http.client.HTTPConnection(ip + ":" + port, timeout = 15)
  64. # POST message to /echo and get back response
  65. sess.request("POST", url="/echo", body=msg)
  66. resp = sess.getresponse()
  67. resp_data = resp.read().decode()
  68. verbose_print(verbosity, "Server response to POST /echo (" + msg + ")")
  69. verbose_print(verbosity, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
  70. verbose_print(verbosity, resp_data)
  71. verbose_print(verbosity, "========================================\n")
  72. # Close HTTP connection
  73. sess.close()
  74. return (resp_data == msg)
  75. def test_put_handler(ip, port, verbosity = False):
  76. verbose_print(verbosity, "======== PUT HANDLER TEST =============")
  77. # Establish HTTP connection
  78. verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
  79. sess = http.client.HTTPConnection(ip + ":" + port, timeout = 15)
  80. # PUT message to /ctrl to disable /hello URI handler
  81. verbose_print(verbosity, "Disabling /hello handler")
  82. sess.request("PUT", url="/ctrl", body="0")
  83. resp = sess.getresponse()
  84. resp.read()
  85. sess.request("GET", url="/hello")
  86. resp = sess.getresponse()
  87. resp_data1 = resp.read().decode()
  88. verbose_print(verbosity, "Response on GET /hello : " + resp_data1)
  89. # PUT message to /ctrl to enable /hello URI handler
  90. verbose_print(verbosity, "Enabling /hello handler")
  91. sess.request("PUT", url="/ctrl", body="1")
  92. resp = sess.getresponse()
  93. resp.read()
  94. sess.request("GET", url="/hello")
  95. resp = sess.getresponse()
  96. resp_data2 = resp.read().decode()
  97. verbose_print(verbosity, "Response on GET /hello : " + resp_data2)
  98. # Close HTTP connection
  99. sess.close()
  100. return ((resp_data2 == "Hello World!") and (resp_data1 == "This URI doesn't exist"))
  101. def test_custom_uri_query(ip, port, query, verbosity = False):
  102. verbose_print(verbosity, "======== GET HANDLER TEST =============")
  103. # Establish HTTP connection
  104. verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
  105. sess = http.client.HTTPConnection(ip + ":" + port, timeout = 15)
  106. uri = "/hello?" + query
  107. # GET hello response
  108. verbose_print(verbosity, "Sending GET to URI : ", uri)
  109. sess.request("GET", url=uri, headers={})
  110. resp = sess.getresponse()
  111. resp_data = resp.read().decode()
  112. verbose_print(verbosity, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
  113. verbose_print(verbosity, "Server response to GET /hello")
  114. verbose_print(verbosity, "Response Data : " + resp_data)
  115. verbose_print(verbosity, "========================================\n")
  116. # Close HTTP connection
  117. sess.close()
  118. return (resp_data == "Hello World!")
  119. if __name__ == '__main__':
  120. # Configure argument parser
  121. parser = argparse.ArgumentParser(description='Run HTTPd Test')
  122. parser.add_argument('IP' , metavar='IP' , type=str, help='Server IP')
  123. parser.add_argument('port', metavar='port', type=str, help='Server port')
  124. parser.add_argument('msg', metavar='message', type=str, help='Message to be sent to server')
  125. args = vars(parser.parse_args())
  126. # Get arguments
  127. ip = args['IP']
  128. port = args['port']
  129. msg = args['msg']
  130. if not test_get_handler (ip, port, True):
  131. Utility.console_log("Failed!")
  132. if not test_post_handler(ip, port, msg, True):
  133. Utility.console_log("Failed!")
  134. if not test_put_handler (ip, port, True):
  135. Utility.console_log("Failed!")