http_server_simple_test.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 imp
  17. import re
  18. import os
  19. import sys
  20. import string
  21. import random
  22. import socket
  23. # This environment variable is expected on the host machine
  24. test_fw_path = os.getenv("TEST_FW_PATH")
  25. if test_fw_path and test_fw_path not in sys.path:
  26. sys.path.insert(0, test_fw_path)
  27. # When running on local machine execute the following before running this script
  28. # > make app bootloader
  29. # > make print_flash_cmd | tail -n 1 > build/download.config
  30. # > export TEST_FW_PATH=~/esp/esp-idf/tools/tiny-test-fw
  31. import TinyFW
  32. import IDF
  33. # Import client module
  34. expath = os.path.dirname(os.path.realpath(__file__))
  35. client = imp.load_source("client", expath + "/scripts/client.py")
  36. @IDF.idf_example_test(env_tag="Example_WIFI")
  37. def test_examples_protocol_http_server_simple(env, extra_data):
  38. # Acquire DUT
  39. dut1 = env.get_dut("http_server", "examples/protocols/http_server/simple")
  40. # Get binary file
  41. binary_file = os.path.join(dut1.app.binary_path, "simple.bin")
  42. bin_size = os.path.getsize(binary_file)
  43. IDF.log_performance("http_server_bin_size", "{}KB".format(bin_size/1024))
  44. IDF.check_performance("http_server_bin_size", bin_size/1024)
  45. # Upload binary and start testing
  46. print "Starting http_server simple test app"
  47. dut1.start_app()
  48. # Parse IP address of STA
  49. print "Waiting to connect with AP"
  50. got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=120)[0]
  51. got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
  52. print "Got IP : " + got_ip
  53. print "Got Port : " + got_port
  54. # Expected Logs
  55. dut1.expect("Registering URI handlers", timeout=30)
  56. # Run test script
  57. # If failed raise appropriate exception
  58. print "Test /hello GET handler"
  59. if not client.test_get_handler(got_ip, got_port):
  60. raise RuntimeError
  61. # Acquire host IP. Need a way to check it
  62. host_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Found header => Host: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
  63. # Match additional headers sent in the request
  64. dut1.expect("Found header => Test-Header-2: Test-Value-2", timeout=30)
  65. dut1.expect("Found header => Test-Header-1: Test-Value-1", timeout=30)
  66. dut1.expect("Found URL query parameter => query1=value1", timeout=30)
  67. dut1.expect("Found URL query parameter => query3=value3", timeout=30)
  68. dut1.expect("Found URL query parameter => query2=value2", timeout=30)
  69. dut1.expect("Request headers lost", timeout=30)
  70. print "Test /ctrl PUT handler and realtime handler de/registration"
  71. if not client.test_put_handler(got_ip, got_port):
  72. raise RuntimeError
  73. dut1.expect("Unregistering /hello and /echo URIs", timeout=30)
  74. dut1.expect("Registering /hello and /echo URIs", timeout=30)
  75. # Generate random data of 10KB
  76. random_data = ''.join(string.printable[random.randint(0,len(string.printable))-1] for _ in range(10*1024))
  77. print "Test /echo POST handler with random data"
  78. if not client.test_post_handler(got_ip, got_port, random_data):
  79. raise RuntimeError
  80. query = "http://foobar"
  81. print "Test /hello with custom query : " + query
  82. if not client.test_custom_uri_query(got_ip, got_port, query):
  83. raise RuntimeError
  84. dut1.expect("Found URL query => " + query, timeout=30)
  85. query = "abcd+1234%20xyz"
  86. print "Test /hello with custom query : " + query
  87. if not client.test_custom_uri_query(got_ip, got_port, query):
  88. raise RuntimeError
  89. dut1.expect("Found URL query => " + query, timeout=30)
  90. query = "abcd\nyz"
  91. print "Test /hello with invalid query"
  92. if client.test_custom_uri_query(got_ip, got_port, query):
  93. raise RuntimeError
  94. dut1.expect("400 Bad Request - Server unable to understand request due to invalid syntax", timeout=30)
  95. if __name__ == '__main__':
  96. test_examples_protocol_http_server_simple()