Skip to content

Web Server Test #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 5, 2016
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
7 changes: 5 additions & 2 deletions tests/device/libraries/BSTest/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,18 @@ def __init__(self, spawn_obj, name, mocks):

def get_test_list(self):
self.sp.sendline('-1')
timeout = 10
self.tests = []
timeout = 100
while timeout > 0:
res = self.sp.expect(['>>>>>bs_test_menu_begin', EOF, TIMEOUT])
if res == 0:
break
timeout-=1
time.sleep(0.1)
if timeout <= 0:
debug_print('begin timeout')
return
debug_print('got begin')
self.tests = []
while True:
res = self.sp.expect(['>>>>>bs_test_item id\=(\d+) name\="([^\"]*?)" desc="([^"]*?)"',
'>>>>>bs_test_menu_end',
Expand Down
54 changes: 54 additions & 0 deletions tests/device/test_WiFiServer/test_WiFiServer.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiClient.h>
#include <BSTest.h>
#include <test_config.h>


BS_ENV_DECLARE();

void setup()
{
Serial.begin(115200);
WiFi.persistent(false);
WiFi.begin(STA_SSID, STA_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
MDNS.begin("esp8266-wfs-test");
BS_RUN(Serial);
}

TEST_CASE("Simple echo server", "[WiFiServer]")
{
const uint32_t timeout = 10000;
const uint16_t port = 5000;
const int maxRequests = 5;
const int minRequestLength = 128;
WiFiServer server(port);
server.begin();
auto start = millis();

int replyCount = 0;
while (millis() - start < timeout) {
delay(50);
WiFiClient client = server.available();
if (!client) {
continue;
}
String request = client.readStringUntil('\n');
CHECK(request.length() >= minRequestLength);
client.print(request);
client.print('\n');
if (++replyCount == maxRequests) {
break;
}
}
CHECK(replyCount == maxRequests);
}

void loop()
{
}

42 changes: 42 additions & 0 deletions tests/device/test_WiFiServer/test_WiFiServer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
from mock_decorators import setup, teardown
from threading import Thread
import socket
import time

stop_client_thread = False
client_thread = None

@setup('Simple echo server')
def setup_echo_server(e):
global stop_client_thread
global client_thread
def echo_client_thread():
server_address = socket.gethostbyname('esp8266-wfs-test.local')
count = 0
while count < 5 and not stop_client_thread:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((server_address, 5000))
sock.settimeout(1.0)
buf = 'a' * 1023 + '\n'
sock.sendall(buf)
data = ''
retries = 0
while len(data) < 1024 and retries < 3:
data += sock.recv(1024)
retries += 1
print 'Received {} bytes'.format(len(data))
if len(data) != 1024:
raise RuntimeError('client failed to receive response')
count += 1

stop_client_thread = False
client_thread = Thread(target=echo_client_thread)
client_thread.start()

@teardown('Simple echo server')
def teardown_echo_server(e):
global stop_client_thread
stop_client_thread = True
client_thread.join()


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
13 changes: 10 additions & 3 deletions tools/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,17 @@
def compile(tmp_dir, sketch, tools_dir, hardware_dir, ide_path, f, args):
cmd = ide_path + '/arduino-builder '
cmd += '-compile -logger=human '
cmd += '-build-path "' + tmp_dir + '" '
cmd += '-tools "' + ide_path + '/tools-builder" '
if args.library_path:
for lib_dir in args.library_path:
cmd += '-libraries "' + lib_dir + '" '
cmd += '-build-path "' + tmp_dir + '" '
cmd += '-hardware "' + ide_path + '/hardware" '
cmd += '-hardware ' + hardware_dir + ' '
cmd += '-tools "' + ide_path + '/tools-builder" '
if args.hardware_dir:
for hw_dir in args.hardware_dir:
cmd += '-hardware "' + hw_dir + '" '
else:
cmd += '-hardware "' + hardware_dir + '" '
# Debug=Serial,DebugLevel=Core____
cmd += '-fqbn=esp8266com:esp8266:{board_name}:' \
'CpuFrequency={cpu_freq},' \
Expand Down Expand Up @@ -72,6 +76,8 @@ def parse_args():
parser.add_argument('-p', '--build_path', help='Build directory')
parser.add_argument('-l', '--library_path', help='Additional library path',
action='append')
parser.add_argument('-d', '--hardware_dir', help='Additional hardware path',
action='append')
parser.add_argument('-b', '--board_name', help='Board name', default='generic')
parser.add_argument('-s', '--flash_size', help='Flash size', default='512K64',
choices=['512K0', '512K64', '1M512', '4M1M', '4M3M'])
Expand Down Expand Up @@ -111,6 +117,7 @@ def main():
created_tmp_dir = True

tools_dir = os.path.dirname(os.path.realpath(__file__)) + '/../tools'
# this is not the correct hardware folder to add.
hardware_dir = os.path.dirname(os.path.realpath(__file__)) + '/../cores'

output_name = tmp_dir + '/' + os.path.basename(sketch_path) + '.bin'
Expand Down