client.py 5.7 KB

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