1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

Web Server Test (#1) (#2231)

* Initial WebServer Test

* ignore .pyc files

* add poster as requirement to virtualenv
This commit is contained in:
Me No Dev 2016-07-05 11:18:53 +03:00 committed by Ivan Grokhotkov
parent 79ce12247e
commit 2364ad4dd0
5 changed files with 202 additions and 1 deletions

2
.gitignore vendored
View File

@ -8,3 +8,5 @@ exclude.txt
tools/sdk/lib/liblwip_src.a tools/sdk/lib/liblwip_src.a
tools/sdk/lwip/src/build tools/sdk/lwip/src/build
tools/sdk/lwip/src/liblwip_src.a tools/sdk/lwip/src/liblwip_src.a
*.pyc

View File

@ -10,3 +10,4 @@ PyYAML==3.11
six==1.10.0 six==1.10.0
Werkzeug==0.11.9 Werkzeug==0.11.9
wheel==0.24.0 wheel==0.24.0
poster==0.8.1

View File

@ -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()
{
}

View File

@ -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