-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathfull_test_suite.ino
189 lines (150 loc) · 5.35 KB
/
full_test_suite.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/* HTTPClient full test suite
Every REST method is called.
by Chris Continanza (csquared)
modified by Massimo Banzi (mbanzi) to support more network devices
modified by Tom Igoe to match new API
updated 22 Feb 2016
by Tom Igoe
*/
#include <HTTPClient.h>
#include <WiFi101.h>
#include "config.h"
int test_delay = 1000; //so we don't spam the API
boolean describe_tests = true;
char serverAddress[] = "192.168.0.3"; // server address
int port = 8080;
WiFiClient wifi;
HTTPClient client = HTTPClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
void setup() {
Serial.begin(9600);
while(!Serial);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
}
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void test_status(int statusCode) {
delay(test_delay);
if (statusCode == 200) {
Serial.print("TEST RESULT: ok (");
Serial.print(statusCode);
Serial.println(")");
} else {
Serial.print("TEST RESULT: failure (");
Serial.print(statusCode);
Serial.println(")");
}
}
void test_response(String response) {
//Serial.println(response);
if (response == "OK") {
Serial.println("TEST RESULT: ok (response body)");
} else {
Serial.println("TEST RESULT: fail (response body = " + response + ")");
}
}
void describe(String description) {
if (describe_tests) Serial.println(description);
}
//reusable test variables
String post_body = "POSTDATA";
void GET_tests() {
describe("Test GET with path");
test_status(client.get("/get"));
describe("Test GET with path and response");
test_status(client.get("/get"));
test_response(client.readResponse());
describe("Test GET with path and query");
test_status(client.get("/get?name=Bob&age=13"));
describe("Test GET with path and header");
client.setHeader("X-Test-Header: true");
test_status(client.get("/get-header"));
describe("Test GET with path and header and response");
client.setHeader("X-Test-Header: true");
test_status(client.get("/get-header"));
test_response(client.readResponse());
describe("Test GET with 2 headers and response");
client.setHeader("X-Test-Header1: one");
client.setHeader("X-Test-Header2: two");
test_status(client.get("/get-headers"));
test_response(client.readResponse());
}
void POST_tests() {
// POST TESTS
describe("Test POST with path and body");
test_status(client.post("/data", post_body));
describe("Test POST with path and body and response");
test_status(client.post("/data", post_body));
test_response(client.readResponse());
describe("Test POST with path and body and header");
client.setHeader("X-Test-Header: true");
test_status(client.post("/data-header", post_body));
describe("Test POST with path and body and header and response");
client.setHeader("X-Test-Header: true");
test_status(client.post("/data-header", post_body));
test_response(client.readResponse());
describe("Test POST with 2 headers and response");
client.setHeader("X-Test-Header1: one");
client.setHeader("X-Test-Header2: two");
test_status(client.post("/data-headers", post_body));
test_response(client.readResponse());
}
void PUT_tests() {
describe("Test PUT with path and body");
test_status(client.put("/data", post_body));
describe("Test PUT with path and body and response");
test_status(client.put("/data", post_body));
test_response(client.readResponse());
describe("Test PUT with path and body and header");
client.setHeader("X-Test-Header: true");
test_status(client.put("/data-header", post_body));
describe("Test PUT with path and body and header and response");
client.setHeader("X-Test-Header: true");
test_status(client.put("/data-header", post_body));
test_response(client.readResponse());
describe("Test PUT with 2 headers and response");
client.setHeader("X-Test-Header1: one");
client.setHeader("X-Test-Header2: two");
test_status(client.put("/data-headers", post_body));
test_response(client.readResponse());
}
void DELETE_tests() {
describe("Test DELETE with path");
//note: requires a special endpoint
test_status(client.del("/del"));
describe("Test DELETE with path and body");
test_status(client.del("/data", post_body));
describe("Test DELETE with path and body and response");
test_status(client.del("/data", post_body));
test_response(client.readResponse());
describe("Test DELETE with path and body and header");
client.setHeader("X-Test-Header: true");
test_status(client.del("/data-header", post_body));
describe("Test DELETE with path and body and header and response");
client.setHeader("X-Test-Header: true");
test_status(client.del("/data-header", post_body));
test_response(client.readResponse());
describe("Test DELETE with 2 headers and response");
client.setHeader("X-Test-Header1: one");
client.setHeader("X-Test-Header2: two");
test_status(client.del("/data-headers", post_body));
test_response(client.readResponse());
}
// Run the tests!
void loop() {
GET_tests();
POST_tests();
PUT_tests();
DELETE_tests();
while(true);
}