client.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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 builtins import str
  19. import http.client
  20. import argparse
  21. import Utility
  22. def verbose_print(verbosity, *args):
  23. if (verbosity):
  24. Utility.console_log(''.join(str(elems) for elems in args))
  25. def test_get_handler(ip, port, verbosity=False):
  26. verbose_print(verbosity, "======== GET HANDLER TEST =============")
  27. # Establish HTTP connection
  28. verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
  29. sess = http.client.HTTPConnection(ip + ":" + port, timeout=15)
  30. uri = "/hello?query1=value1&query2=value2&query3=value3"
  31. # GET hello response
  32. test_headers = {"Test-Header-1":"Test-Value-1", "Test-Header-2":"Test-Value-2"}
  33. verbose_print(verbosity, "Sending GET to URI : ", uri)
  34. verbose_print(verbosity, "Sending additional headers : ")
  35. for k, v in test_headers.items():
  36. verbose_print(verbosity, "\t", k, ": ", v)
  37. sess.request("GET", url=uri, headers=test_headers)
  38. resp = sess.getresponse()
  39. resp_hdrs = resp.getheaders()
  40. resp_data = resp.read().decode()
  41. try:
  42. if resp.getheader("Custom-Header-1") != "Custom-Value-1":
  43. return False
  44. if resp.getheader("Custom-Header-2") != "Custom-Value-2":
  45. return False
  46. except Exception:
  47. return False
  48. verbose_print(verbosity, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
  49. verbose_print(verbosity, "Server response to GET /hello")
  50. verbose_print(verbosity, "Response Headers : ")
  51. for k, v in resp_hdrs:
  52. verbose_print(verbosity, "\t", k, ": ", v)
  53. verbose_print(verbosity, "Response Data : " + resp_data)
  54. verbose_print(verbosity, "========================================\n")
  55. # Close HTTP connection
  56. sess.close()
  57. return (resp_data == "Hello World!")
  58. def test_post_handler(ip, port, msg, verbosity=False):
  59. verbose_print(verbosity, "======== POST HANDLER TEST ============")
  60. # Establish HTTP connection
  61. verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
  62. sess = http.client.HTTPConnection(ip + ":" + port, timeout=15)
  63. # POST message to /echo and get back response
  64. sess.request("POST", url="/echo", body=msg)
  65. resp = sess.getresponse()
  66. resp_data = resp.read().decode()
  67. verbose_print(verbosity, "Server response to POST /echo (" + msg + ")")
  68. verbose_print(verbosity, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
  69. verbose_print(verbosity, resp_data)
  70. verbose_print(verbosity, "========================================\n")
  71. # Close HTTP connection
  72. sess.close()
  73. return (resp_data == msg)
  74. def test_put_handler(ip, port, verbosity=False):
  75. verbose_print(verbosity, "======== PUT HANDLER TEST =============")
  76. # Establish HTTP connection
  77. verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
  78. sess = http.client.HTTPConnection(ip + ":" + port, timeout=15)
  79. # PUT message to /ctrl to disable /hello URI handler
  80. verbose_print(verbosity, "Disabling /hello handler")
  81. sess.request("PUT", url="/ctrl", body="0")
  82. resp = sess.getresponse()
  83. resp.read()
  84. sess.request("GET", url="/hello")
  85. resp = sess.getresponse()
  86. resp_data1 = resp.read().decode()
  87. verbose_print(verbosity, "Response on GET /hello : " + resp_data1)
  88. # PUT message to /ctrl to enable /hello URI handler
  89. verbose_print(verbosity, "Enabling /hello handler")
  90. sess.request("PUT", url="/ctrl", body="1")
  91. resp = sess.getresponse()
  92. resp.read()
  93. sess.request("GET", url="/hello")
  94. resp = sess.getresponse()
  95. resp_data2 = resp.read().decode()
  96. verbose_print(verbosity, "Response on GET /hello : " + resp_data2)
  97. # Close HTTP connection
  98. sess.close()
  99. return ((resp_data2 == "Hello World!") and (resp_data1 == "This URI doesn't exist"))
  100. def test_custom_uri_query(ip, port, query, verbosity=False):
  101. verbose_print(verbosity, "======== GET HANDLER TEST =============")
  102. # Establish HTTP connection
  103. verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
  104. sess = http.client.HTTPConnection(ip + ":" + port, timeout=15)
  105. uri = "/hello?" + query
  106. # GET hello response
  107. verbose_print(verbosity, "Sending GET to URI : ", uri)
  108. sess.request("GET", url=uri, headers={})
  109. resp = sess.getresponse()
  110. resp_data = resp.read().decode()
  111. verbose_print(verbosity, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
  112. verbose_print(verbosity, "Server response to GET /hello")
  113. verbose_print(verbosity, "Response Data : " + resp_data)
  114. verbose_print(verbosity, "========================================\n")
  115. # Close HTTP connection
  116. sess.close()
  117. return (resp_data == "Hello World!")
  118. if __name__ == '__main__':
  119. # Configure argument parser
  120. parser = argparse.ArgumentParser(description='Run HTTPd Test')
  121. parser.add_argument('IP', metavar='IP', type=str, help='Server IP')
  122. parser.add_argument('port', metavar='port', type=str, help='Server port')
  123. parser.add_argument('msg', metavar='message', type=str, help='Message to be sent to server')
  124. args = vars(parser.parse_args())
  125. # Get arguments
  126. ip = args['IP']
  127. port = args['port']
  128. msg = args['msg']
  129. if not test_get_handler(ip, port, True):
  130. Utility.console_log("Failed!")
  131. if not test_post_handler(ip, port, msg, True):
  132. Utility.console_log("Failed!")
  133. if not test_put_handler(ip, port, True):
  134. Utility.console_log("Failed!")