-
Notifications
You must be signed in to change notification settings - Fork 13.3k
/
Copy pathtest_http_server.ino
127 lines (115 loc) · 3.12 KB
/
test_http_server.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <BSTest.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);
BS_RUN(Serial);
}
bool pretest()
{
WiFi.persistent(false);
WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS"));
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
MDNS.begin("etd");
server.onNotFound([](){ server.send(404); });
server.begin();
return true;
}
void handle_request()
{
for (uint8_t i=0; i<server.args(); i++){
// skip "plain" which is automatically added during arg parsing for post's
if (server.argName(i) == "plain")
continue;
if(i > 0)
siteData += "\n";
siteData += server.argName(i) + " = " + server.arg(i);
}
siteHits++;
server.send(200, "text/plain", siteData);
}
TEST_CASE("HTTP GET Parameters", "[HTTPServer]")
{
{
siteHits = 0;
siteData = "";
server.on("/get", HTTP_GET, &handle_request);
uint32_t startTime = millis();
while(siteHits == 0 && (millis() - startTime) < 10000)
{
MDNS.update();
server.handleClient();
}
REQUIRE(siteHits > 0 && siteData.equals("var1 = val with spaces\nva=r+ = so&me%"));
}
}
TEST_CASE("HTTP POST Parameters", "[HTTPServer]")
{
{
siteHits = 0;
siteData = "";
server.on("/post", HTTP_POST, &handle_request);
uint32_t startTime = millis();
while(siteHits == 0 && (millis() - startTime) < 10000)
{
MDNS.update();
server.handleClient();
}
REQUIRE(siteHits > 0 && siteData.equals("var2 = val with spaces"));
}
}
TEST_CASE("HTTP GET+POST Parameters", "[HTTPServer]")
{
{
siteHits = 0;
siteData = "";
server.on("/get_and_post", HTTP_POST, &handle_request);
uint32_t startTime = millis();
while(siteHits == 0 && (millis() - startTime) < 10000)
{
MDNS.update();
server.handleClient();
}
REQUIRE(siteHits > 0 && siteData.equals("var3 = val with spaces\nva&r+ = so=me%"));
}
}
#if 0
TEST_CASE("HTTP Upload", "[HTTPServer]")
{
{
siteHits = 0;
siteData = "";
server.on("/upload", HTTP_POST, &handle_request, [](){
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("\n");
}
});
uint32_t startTime = millis();
while(siteHits == 0 && (millis() - startTime) < 10000)
{
MDNS.update();
server.handleClient();
}
REQUIRE(siteHits > 0 && siteData.equals("test.txt:16\nvar4 = val with spaces"));
}
}
#endif
void loop()
{
}