Skip to content

Commit 2364ad4

Browse files
me-no-devigrr
authored andcommitted
Web Server Test (#1) (#2231)
* Initial WebServer Test * ignore .pyc files * add poster as requirement to virtualenv
1 parent 79ce122 commit 2364ad4

File tree

5 files changed

+202
-1
lines changed

5 files changed

+202
-1
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ exclude.txt
88
tools/sdk/lib/liblwip_src.a
99
tools/sdk/lwip/src/build
1010
tools/sdk/lwip/src/liblwip_src.a
11+
12+
*.pyc

Diff for: tests/device/Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,4 @@ $(TEST_CONFIG):
9696
@echo "****** "
9797
false
9898

99-
.PHONY: tests all count venv $(BUILD_DIR) $(TEST_LIST)
99+
.PHONY: tests all count venv $(BUILD_DIR) $(TEST_LIST)

Diff for: tests/device/libraries/BSTest/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ PyYAML==3.11
1010
six==1.10.0
1111
Werkzeug==0.11.9
1212
wheel==0.24.0
13+
poster==0.8.1

Diff for: tests/device/test_http_server/test_http_server.ino

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
#include <Arduino.h>
2+
#include <ESP8266WiFi.h>
3+
#include <ESP8266WebServer.h>
4+
#include <ESP8266mDNS.h>
5+
#include <BSTest.h>
6+
#include <test_config.h>
7+
#include <pgmspace.h>
8+
9+
BS_ENV_DECLARE();
10+
11+
static ESP8266WebServer server(80);
12+
static uint32_t siteHits = 0;
13+
static String siteData = "";
14+
15+
void setup()
16+
{
17+
Serial.begin(115200);
18+
Serial.setDebugOutput(true);
19+
WiFi.persistent(false);
20+
WiFi.begin(STA_SSID, STA_PASS);
21+
while (WiFi.status() != WL_CONNECTED) {
22+
delay(500);
23+
}
24+
MDNS.begin("etd");
25+
server.onNotFound([](){ server.send(404); });
26+
server.begin();
27+
BS_RUN(Serial);
28+
}
29+
30+
31+
TEST_CASE("HTTP GET Parameters", "[HTTPServer]")
32+
{
33+
{
34+
siteHits = 0;
35+
server.on("/get", HTTP_GET, [](){
36+
siteData = "";
37+
for (uint8_t i=0; i<server.args(); i++){
38+
if(i > 0)
39+
siteData += "&";
40+
siteData += server.argName(i) + "=" + server.arg(i);
41+
}
42+
siteHits++;
43+
server.send(200, "text/plain", siteData);
44+
});
45+
uint32_t startTime = millis();
46+
while(siteHits == 0 && (millis() - startTime) < 10000)
47+
server.handleClient();
48+
REQUIRE(siteHits > 0 && siteData.equals("var1=val with spaces&var+=some%"));
49+
}
50+
}
51+
52+
TEST_CASE("HTTP POST Parameters", "[HTTPServer]")
53+
{
54+
{
55+
siteHits = 0;
56+
server.on("/post", HTTP_POST, [](){
57+
siteData = "";
58+
for (uint8_t i=0; i<server.args(); i++){
59+
if(i > 0)
60+
siteData += "&";
61+
siteData += server.argName(i) + "=" + server.arg(i);
62+
}
63+
siteHits++;
64+
server.send(200, "text/plain", siteData);
65+
});
66+
uint32_t startTime = millis();
67+
while(siteHits == 0 && (millis() - startTime) < 10000)
68+
server.handleClient();
69+
REQUIRE(siteHits > 0 && siteData.equals("var2=val with spaces"));
70+
}
71+
}
72+
73+
TEST_CASE("HTTP GET+POST Parameters", "[HTTPServer]")
74+
{
75+
{
76+
siteHits = 0;
77+
server.on("/get_and_post", HTTP_POST, [](){
78+
siteData = "";
79+
for (uint8_t i=0; i<server.args(); i++){
80+
if(i > 0)
81+
siteData += "&";
82+
siteData += server.argName(i) + "=" + server.arg(i);
83+
}
84+
siteHits++;
85+
server.send(200, "text/plain", siteData);
86+
});
87+
uint32_t startTime = millis();
88+
while(siteHits == 0 && (millis() - startTime) < 10000)
89+
server.handleClient();
90+
REQUIRE(siteHits > 0 && siteData.equals("var3=val with spaces&var+=some%"));
91+
}
92+
}
93+
94+
TEST_CASE("HTTP Upload", "[HTTPServer]")
95+
{
96+
{
97+
siteHits = 0;
98+
server.on("/upload", HTTP_POST, [](){
99+
for (uint8_t i=0; i<server.args(); i++){
100+
if(i > 0)
101+
siteData += "&";
102+
siteData += server.argName(i) + "=" + server.arg(i);
103+
}
104+
siteHits++;
105+
server.send(200, "text/plain", siteData);
106+
}, [](){
107+
HTTPUpload& upload = server.upload();
108+
if(upload.status == UPLOAD_FILE_START){
109+
siteData = upload.filename;
110+
} else if(upload.status == UPLOAD_FILE_END){
111+
siteData.concat(":");
112+
siteData.concat(String(upload.totalSize));
113+
siteData.concat("&");
114+
}
115+
});
116+
uint32_t startTime = millis();
117+
while(siteHits == 0 && (millis() - startTime) < 10000)
118+
server.handleClient();
119+
REQUIRE(siteHits > 0 && siteData.equals("test.txt:16&var4=val with spaces"));
120+
}
121+
}
122+
123+
void loop()
124+
{
125+
}

Diff for: tests/device/test_http_server/test_http_server.py

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from mock_decorators import setup, teardown
2+
from threading import Thread
3+
from poster.encode import MultipartParam
4+
from poster.encode import multipart_encode
5+
from poster.streaminghttp import register_openers
6+
import urllib2
7+
import urllib
8+
9+
def http_test(res, url, get=None, post=None):
10+
response = ''
11+
try:
12+
if get:
13+
url += '?' + urllib.urlencode(get)
14+
if post:
15+
post = urllib.urlencode(post)
16+
request = urllib2.urlopen(url, post, 2)
17+
response = request.read()
18+
except:
19+
return 1
20+
if response != res:
21+
return 1
22+
return 0
23+
24+
@setup('HTTP GET Parameters')
25+
def setup_http_get_params(e):
26+
def testRun():
27+
return http_test('var1=val with spaces&var+=some%', 'http://etd.local/get', {'var1' : 'val with spaces', 'var+' : 'some%'})
28+
Thread(target=testRun).start()
29+
30+
@teardown('HTTP GET Parameters')
31+
def teardown_http_get_params(e):
32+
return 0
33+
34+
@setup('HTTP POST Parameters')
35+
def setup_http_post_params(e):
36+
def testRun():
37+
return http_test('var2=val with spaces', 'http://etd.local/post', None, {'var2' : 'val with spaces'})
38+
Thread(target=testRun).start()
39+
40+
@teardown('HTTP POST Parameters')
41+
def teardown_http_post_params(e):
42+
return 0
43+
44+
@setup('HTTP GET+POST Parameters')
45+
def setup_http_getpost_params(e):
46+
def testRun():
47+
return http_test('var3=val with spaces&var+=some%', 'http://etd.local/get_and_post', {'var3' : 'val with spaces'}, {'var+' : 'some%'})
48+
Thread(target=testRun).start()
49+
50+
@teardown('HTTP GET+POST Parameters')
51+
def teardown_http_getpost_params(e):
52+
return 0
53+
54+
@setup('HTTP Upload')
55+
def setup_http_upload(e):
56+
def testRun():
57+
response = ''
58+
try:
59+
register_openers()
60+
p = MultipartParam("file", "0123456789abcdef", "test.txt", "text/plain; charset=utf8")
61+
datagen, headers = multipart_encode( [("var4", "val with spaces"), p] )
62+
request = urllib2.Request('http://etd.local/upload', datagen, headers)
63+
response = urllib2.urlopen(request, None, 2).read()
64+
except:
65+
return 1
66+
if response != 'test.txt:16&var4=val with spaces':
67+
return 1
68+
return 0
69+
Thread(target=testRun).start()
70+
71+
@teardown('HTTP Upload')
72+
def teardown_http_upload(e):
73+
return 0

0 commit comments

Comments
 (0)