diff --git a/.gitignore b/.gitignore index b1b0b534ea..c36bdd3aa3 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,5 @@ exclude.txt tools/sdk/lib/liblwip_src.a tools/sdk/lwip/src/build tools/sdk/lwip/src/liblwip_src.a + +*.pyc diff --git a/tests/device/Makefile b/tests/device/Makefile index e779996c7a..cf7810fcda 100644 --- a/tests/device/Makefile +++ b/tests/device/Makefile @@ -96,4 +96,4 @@ $(TEST_CONFIG): @echo "****** " false -.PHONY: tests all count venv $(BUILD_DIR) $(TEST_LIST) +.PHONY: tests all count venv $(BUILD_DIR) $(TEST_LIST) diff --git a/tests/device/libraries/BSTest/requirements.txt b/tests/device/libraries/BSTest/requirements.txt index bc7339b49f..7fce0ce8ee 100644 --- a/tests/device/libraries/BSTest/requirements.txt +++ b/tests/device/libraries/BSTest/requirements.txt @@ -10,3 +10,4 @@ PyYAML==3.11 six==1.10.0 Werkzeug==0.11.9 wheel==0.24.0 +poster==0.8.1 diff --git a/tests/device/test_http_server/test_http_server.ino b/tests/device/test_http_server/test_http_server.ino new file mode 100644 index 0000000000..9b5fe43eea --- /dev/null +++ b/tests/device/test_http_server/test_http_server.ino @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include +#include + +BS_ENV_DECLARE(); + +static ESP8266WebServer server(80); +static uint32_t siteHits = 0; +static String siteData = ""; + +void setup() +{ + Serial.begin(115200); + Serial.setDebugOutput(true); + WiFi.persistent(false); + WiFi.begin(STA_SSID, STA_PASS); + while (WiFi.status() != WL_CONNECTED) { + delay(500); + } + MDNS.begin("etd"); + server.onNotFound([](){ server.send(404); }); + server.begin(); + BS_RUN(Serial); +} + + +TEST_CASE("HTTP GET Parameters", "[HTTPServer]") +{ + { + siteHits = 0; + server.on("/get", HTTP_GET, [](){ + siteData = ""; + for (uint8_t i=0; i 0) + siteData += "&"; + siteData += server.argName(i) + "=" + server.arg(i); + } + siteHits++; + server.send(200, "text/plain", siteData); + }); + uint32_t startTime = millis(); + while(siteHits == 0 && (millis() - startTime) < 10000) + server.handleClient(); + REQUIRE(siteHits > 0 && siteData.equals("var1=val with spaces&var+=some%")); + } +} + +TEST_CASE("HTTP POST Parameters", "[HTTPServer]") +{ + { + siteHits = 0; + server.on("/post", HTTP_POST, [](){ + siteData = ""; + for (uint8_t i=0; i 0) + siteData += "&"; + siteData += server.argName(i) + "=" + server.arg(i); + } + siteHits++; + server.send(200, "text/plain", siteData); + }); + uint32_t startTime = millis(); + while(siteHits == 0 && (millis() - startTime) < 10000) + server.handleClient(); + REQUIRE(siteHits > 0 && siteData.equals("var2=val with spaces")); + } +} + +TEST_CASE("HTTP GET+POST Parameters", "[HTTPServer]") +{ + { + siteHits = 0; + server.on("/get_and_post", HTTP_POST, [](){ + siteData = ""; + for (uint8_t i=0; i 0) + siteData += "&"; + siteData += server.argName(i) + "=" + server.arg(i); + } + siteHits++; + server.send(200, "text/plain", siteData); + }); + uint32_t startTime = millis(); + while(siteHits == 0 && (millis() - startTime) < 10000) + server.handleClient(); + REQUIRE(siteHits > 0 && siteData.equals("var3=val with spaces&var+=some%")); + } +} + +TEST_CASE("HTTP Upload", "[HTTPServer]") +{ + { + siteHits = 0; + server.on("/upload", HTTP_POST, [](){ + for (uint8_t i=0; i 0) + siteData += "&"; + siteData += server.argName(i) + "=" + server.arg(i); + } + siteHits++; + server.send(200, "text/plain", siteData); + }, [](){ + HTTPUpload& upload = server.upload(); + if(upload.status == UPLOAD_FILE_START){ + siteData = upload.filename; + } else if(upload.status == UPLOAD_FILE_END){ + siteData.concat(":"); + siteData.concat(String(upload.totalSize)); + siteData.concat("&"); + } + }); + uint32_t startTime = millis(); + while(siteHits == 0 && (millis() - startTime) < 10000) + server.handleClient(); + REQUIRE(siteHits > 0 && siteData.equals("test.txt:16&var4=val with spaces")); + } +} + +void loop() +{ +} diff --git a/tests/device/test_http_server/test_http_server.py b/tests/device/test_http_server/test_http_server.py new file mode 100644 index 0000000000..c43b2b251a --- /dev/null +++ b/tests/device/test_http_server/test_http_server.py @@ -0,0 +1,73 @@ +from mock_decorators import setup, teardown +from threading import Thread +from poster.encode import MultipartParam +from poster.encode import multipart_encode +from poster.streaminghttp import register_openers +import urllib2 +import urllib + +def http_test(res, url, get=None, post=None): + response = '' + try: + if get: + url += '?' + urllib.urlencode(get) + if post: + post = urllib.urlencode(post) + request = urllib2.urlopen(url, post, 2) + response = request.read() + except: + return 1 + if response != res: + return 1 + return 0 + +@setup('HTTP GET Parameters') +def setup_http_get_params(e): + def testRun(): + return http_test('var1=val with spaces&var+=some%', 'http://etd.local/get', {'var1' : 'val with spaces', 'var+' : 'some%'}) + Thread(target=testRun).start() + +@teardown('HTTP GET Parameters') +def teardown_http_get_params(e): + return 0 + +@setup('HTTP POST Parameters') +def setup_http_post_params(e): + def testRun(): + return http_test('var2=val with spaces', 'http://etd.local/post', None, {'var2' : 'val with spaces'}) + Thread(target=testRun).start() + +@teardown('HTTP POST Parameters') +def teardown_http_post_params(e): + return 0 + +@setup('HTTP GET+POST Parameters') +def setup_http_getpost_params(e): + def testRun(): + return http_test('var3=val with spaces&var+=some%', 'http://etd.local/get_and_post', {'var3' : 'val with spaces'}, {'var+' : 'some%'}) + Thread(target=testRun).start() + +@teardown('HTTP GET+POST Parameters') +def teardown_http_getpost_params(e): + return 0 + +@setup('HTTP Upload') +def setup_http_upload(e): + def testRun(): + response = '' + try: + register_openers() + p = MultipartParam("file", "0123456789abcdef", "test.txt", "text/plain; charset=utf8") + datagen, headers = multipart_encode( [("var4", "val with spaces"), p] ) + request = urllib2.Request('http://etd.local/upload', datagen, headers) + response = urllib2.urlopen(request, None, 2).read() + except: + return 1 + if response != 'test.txt:16&var4=val with spaces': + return 1 + return 0 + Thread(target=testRun).start() + +@teardown('HTTP Upload') +def teardown_http_upload(e): + return 0