http_server_simple_test.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 division
  17. from __future__ import print_function
  18. from __future__ import unicode_literals
  19. from builtins import range
  20. import re
  21. import os
  22. import string
  23. import random
  24. from tiny_test_fw import Utility
  25. import ttfw_idf
  26. from idf_http_server_test import client
  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. @ttfw_idf.idf_example_test(env_tag="Example_WIFI")
  31. def test_examples_protocol_http_server_simple(env, extra_data):
  32. # Acquire DUT
  33. dut1 = env.get_dut("http_server", "examples/protocols/http_server/simple")
  34. # Get binary file
  35. binary_file = os.path.join(dut1.app.binary_path, "simple.bin")
  36. bin_size = os.path.getsize(binary_file)
  37. ttfw_idf.log_performance("http_server_bin_size", "{}KB".format(bin_size // 1024))
  38. ttfw_idf.check_performance("http_server_bin_size", bin_size // 1024)
  39. # Upload binary and start testing
  40. Utility.console_log("Starting http_server simple test app")
  41. dut1.start_app()
  42. # Parse IP address of STA
  43. Utility.console_log("Waiting to connect with AP")
  44. got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)IPv4 address: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
  45. got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
  46. Utility.console_log("Got IP : " + got_ip)
  47. Utility.console_log("Got Port : " + got_port)
  48. # Expected Logs
  49. dut1.expect("Registering URI handlers", timeout=30)
  50. # Run test script
  51. # If failed raise appropriate exception
  52. Utility.console_log("Test /hello GET handler")
  53. if not client.test_get_handler(got_ip, got_port):
  54. raise RuntimeError
  55. # Acquire host IP. Need a way to check it
  56. dut1.expect(re.compile(r"(?:[\s\S]*)Found header => Host: (\d+.\d+.\d+.\d+)"), timeout=30)[0]
  57. # Match additional headers sent in the request
  58. dut1.expect("Found header => Test-Header-2: Test-Value-2", timeout=30)
  59. dut1.expect("Found header => Test-Header-1: Test-Value-1", timeout=30)
  60. dut1.expect("Found URL query parameter => query1=value1", timeout=30)
  61. dut1.expect("Found URL query parameter => query3=value3", timeout=30)
  62. dut1.expect("Found URL query parameter => query2=value2", timeout=30)
  63. dut1.expect("Request headers lost", timeout=30)
  64. Utility.console_log("Test /ctrl PUT handler and realtime handler de/registration")
  65. if not client.test_put_handler(got_ip, got_port):
  66. raise RuntimeError
  67. dut1.expect("Unregistering /hello and /echo URIs", timeout=30)
  68. dut1.expect("Registering /hello and /echo URIs", timeout=30)
  69. # Generate random data of 10KB
  70. random_data = ''.join(string.printable[random.randint(0,len(string.printable)) - 1] for _ in range(10 * 1024))
  71. Utility.console_log("Test /echo POST handler with random data")
  72. if not client.test_post_handler(got_ip, got_port, random_data):
  73. raise RuntimeError
  74. query = "http://foobar"
  75. Utility.console_log("Test /hello with custom query : " + query)
  76. if not client.test_custom_uri_query(got_ip, got_port, query):
  77. raise RuntimeError
  78. dut1.expect("Found URL query => " + query, timeout=30)
  79. query = "abcd+1234%20xyz"
  80. Utility.console_log("Test /hello with custom query : " + query)
  81. if not client.test_custom_uri_query(got_ip, got_port, query):
  82. raise RuntimeError
  83. dut1.expect("Found URL query => " + query, timeout=30)
  84. if __name__ == '__main__':
  85. test_examples_protocol_http_server_simple()