Examples: Add Python 2&3 support

This commit is contained in:
Roland Dobai
2018-09-10 15:13:47 +02:00
parent 96cd3b75cd
commit 17b7959de9
10 changed files with 274 additions and 216 deletions

View File

@@ -14,6 +14,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from builtins import range
import imp
import re
import os
@@ -47,27 +51,27 @@ def test_examples_protocol_http_server_simple(env, extra_data):
# Get binary file
binary_file = os.path.join(dut1.app.binary_path, "simple.bin")
bin_size = os.path.getsize(binary_file)
IDF.log_performance("http_server_bin_size", "{}KB".format(bin_size/1024))
IDF.check_performance("http_server_bin_size", bin_size/1024)
IDF.log_performance("http_server_bin_size", "{}KB".format(bin_size//1024))
IDF.check_performance("http_server_bin_size", bin_size//1024)
# Upload binary and start testing
print "Starting http_server simple test app"
print("Starting http_server simple test app")
dut1.start_app()
# Parse IP address of STA
print "Waiting to connect with AP"
print("Waiting to connect with AP")
got_ip = dut1.expect(re.compile(r"(?:[\s\S]*)Got IP: '(\d+.\d+.\d+.\d+)'"), timeout=120)[0]
got_port = dut1.expect(re.compile(r"(?:[\s\S]*)Starting server on port: '(\d+)'"), timeout=30)[0]
print "Got IP : " + got_ip
print "Got Port : " + got_port
print("Got IP : " + got_ip)
print("Got Port : " + got_port)
# Expected Logs
dut1.expect("Registering URI handlers", timeout=30)
# Run test script
# If failed raise appropriate exception
print "Test /hello GET handler"
print("Test /hello GET handler")
if not client.test_get_handler(got_ip, got_port):
raise RuntimeError
@@ -82,7 +86,7 @@ def test_examples_protocol_http_server_simple(env, extra_data):
dut1.expect("Found URL query parameter => query2=value2", timeout=30)
dut1.expect("Request headers lost", timeout=30)
print "Test /ctrl PUT handler and realtime handler de/registration"
print("Test /ctrl PUT handler and realtime handler de/registration")
if not client.test_put_handler(got_ip, got_port):
raise RuntimeError
dut1.expect("Unregistering /hello and /echo URIs", timeout=30)
@@ -90,24 +94,24 @@ def test_examples_protocol_http_server_simple(env, extra_data):
# Generate random data of 10KB
random_data = ''.join(string.printable[random.randint(0,len(string.printable))-1] for _ in range(10*1024))
print "Test /echo POST handler with random data"
print("Test /echo POST handler with random data")
if not client.test_post_handler(got_ip, got_port, random_data):
raise RuntimeError
query = "http://foobar"
print "Test /hello with custom query : " + query
print("Test /hello with custom query : " + query)
if not client.test_custom_uri_query(got_ip, got_port, query):
raise RuntimeError
dut1.expect("Found URL query => " + query, timeout=30)
query = "abcd+1234%20xyz"
print "Test /hello with custom query : " + query
print("Test /hello with custom query : " + query)
if not client.test_custom_uri_query(got_ip, got_port, query):
raise RuntimeError
dut1.expect("Found URL query => " + query, timeout=30)
query = "abcd\nyz"
print "Test /hello with invalid query"
print("Test /hello with invalid query")
if client.test_custom_uri_query(got_ip, got_port, query):
raise RuntimeError
dut1.expect("400 Bad Request - Server unable to understand request due to invalid syntax", timeout=30)

View File

@@ -14,30 +14,35 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import httplib
from __future__ import print_function
from __future__ import unicode_literals
from future import standard_library
standard_library.install_aliases()
from builtins import str
import http.client
import argparse
def verbose_print(verbosity, *args):
if (verbosity):
print ''.join(str(elems) for elems in args)
print(''.join(str(elems) for elems in args))
def test_get_handler(ip, port, verbosity = False):
verbose_print(verbosity, "======== GET HANDLER TEST =============")
# Establish HTTP connection
verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
sess = httplib.HTTPConnection(ip + ":" + port, timeout = 15)
sess = http.client.HTTPConnection(ip + ":" + port, timeout = 15)
uri = "/hello?query1=value1&query2=value2&query3=value3"
# GET hello response
test_headers = {"Test-Header-1":"Test-Value-1", "Test-Header-2":"Test-Value-2"}
verbose_print(verbosity, "Sending GET to URI : ", uri)
verbose_print(verbosity, "Sending additional headers : ")
for k, v in test_headers.iteritems():
for k, v in test_headers.items():
verbose_print(verbosity, "\t", k, ": ", v)
sess.request("GET", url=uri, headers=test_headers)
resp = sess.getresponse()
resp_hdrs = resp.getheaders()
resp_data = resp.read()
resp_data = resp.read().decode()
try:
if resp.getheader("Custom-Header-1") != "Custom-Value-1":
return False
@@ -62,12 +67,12 @@ def test_post_handler(ip, port, msg, verbosity = False):
verbose_print(verbosity, "======== POST HANDLER TEST ============")
# Establish HTTP connection
verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
sess = httplib.HTTPConnection(ip + ":" + port, timeout = 15)
sess = http.client.HTTPConnection(ip + ":" + port, timeout = 15)
# POST message to /echo and get back response
sess.request("POST", url="/echo", body=msg)
resp = sess.getresponse()
resp_data = resp.read()
resp_data = resp.read().decode()
verbose_print(verbosity, "Server response to POST /echo (" + msg + ")")
verbose_print(verbosity, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
verbose_print(verbosity, resp_data)
@@ -81,7 +86,7 @@ def test_put_handler(ip, port, verbosity = False):
verbose_print(verbosity, "======== PUT HANDLER TEST =============")
# Establish HTTP connection
verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
sess = httplib.HTTPConnection(ip + ":" + port, timeout = 15)
sess = http.client.HTTPConnection(ip + ":" + port, timeout = 15)
# PUT message to /ctrl to disable /hello URI handler
verbose_print(verbosity, "Disabling /hello handler")
@@ -91,7 +96,7 @@ def test_put_handler(ip, port, verbosity = False):
sess.request("GET", url="/hello")
resp = sess.getresponse()
resp_data1 = resp.read()
resp_data1 = resp.read().decode()
verbose_print(verbosity, "Response on GET /hello : " + resp_data1)
# PUT message to /ctrl to enable /hello URI handler
@@ -102,7 +107,7 @@ def test_put_handler(ip, port, verbosity = False):
sess.request("GET", url="/hello")
resp = sess.getresponse()
resp_data2 = resp.read()
resp_data2 = resp.read().decode()
verbose_print(verbosity, "Response on GET /hello : " + resp_data2)
# Close HTTP connection
@@ -113,14 +118,14 @@ def test_custom_uri_query(ip, port, query, verbosity = False):
verbose_print(verbosity, "======== GET HANDLER TEST =============")
# Establish HTTP connection
verbose_print(verbosity, "Connecting to => " + ip + ":" + port)
sess = httplib.HTTPConnection(ip + ":" + port, timeout = 15)
sess = http.client.HTTPConnection(ip + ":" + port, timeout = 15)
uri = "/hello?" + query
# GET hello response
verbose_print(verbosity, "Sending GET to URI : ", uri)
sess.request("GET", url=uri, headers={})
resp = sess.getresponse()
resp_data = resp.read()
resp_data = resp.read().decode()
verbose_print(verbosity, "vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv")
verbose_print(verbosity, "Server response to GET /hello")
@@ -145,8 +150,8 @@ if __name__ == '__main__':
msg = args['msg']
if not test_get_handler (ip, port, True):
print "Failed!"
print("Failed!")
if not test_post_handler(ip, port, msg, True):
print "Failed!"
print("Failed!")
if not test_put_handler (ip, port, True):
print "Failed!"
print("Failed!")