Skip to content

Commit

Permalink
Web Server Test (#1) (#2231)
Browse files Browse the repository at this point in the history
* Initial WebServer Test

* ignore .pyc files

* add poster as requirement to virtualenv
  • Loading branch information
me-no-dev authored and igrr committed Jul 5, 2016
1 parent 79ce122 commit 2364ad4
Show file tree
Hide file tree
Showing 5 changed files with 202 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
2 changes: 1 addition & 1 deletion tests/device/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 1 addition & 0 deletions tests/device/libraries/BSTest/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ PyYAML==3.11
six==1.10.0
Werkzeug==0.11.9
wheel==0.24.0
poster==0.8.1
125 changes: 125 additions & 0 deletions tests/device/test_http_server/test_http_server.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <BSTest.h>
#include <test_config.h>
#include <pgmspace.h>

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<server.args(); i++){
if(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<server.args(); i++){
if(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<server.args(); i++){
if(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<server.args(); i++){
if(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()
{
}
73 changes: 73 additions & 0 deletions tests/device/test_http_server/test_http_server.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2364ad4

Please sign in to comment.