forked from esp8266/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_http_client.ino
138 lines (129 loc) · 3.78 KB
/
test_http_client.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
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClientSecureAxTLS.h>
#include <BSTest.h>
#include <pgmspace.h>
BS_ENV_DECLARE();
void setup()
{
Serial.begin(115200);
Serial.setDebugOutput(true);
WiFi.persistent(false);
WiFi.begin(getenv("STA_SSID"), getenv("STA_PASS"));
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
BS_RUN(Serial);
}
const char* fp = "44 40 9E 34 92 2D E4 61 A4 89 A8 D5 7F 71 B7 62 B3 FD DD E1";
TEST_CASE("HTTP GET & POST requests", "[HTTPClient]")
{
{
// small request
WiFiClient client;
HTTPClient http;
http.begin(client, getenv("SERVER_IP"), 8088, "/");
auto httpCode = http.GET();
REQUIRE(httpCode == HTTP_CODE_OK);
String payload = http.getString();
REQUIRE(payload == "hello!!!");
}
{
// request which returns 8000 bytes
WiFiClient client;
HTTPClient http;
http.begin(client, getenv("SERVER_IP"), 8088, "/data?size=8000");
auto httpCode = http.GET();
REQUIRE(httpCode == HTTP_CODE_OK);
String payload = http.getString();
auto len = payload.length();
REQUIRE(len == 8000);
for (int i = 0; i < len; ++i) {
if (payload[i] != 'a') {
REQUIRE(false);
}
}
}
{
// can do two POST requests with one HTTPClient object (#1902)
WiFiClient client;
HTTPClient http;
http.begin(client, getenv("SERVER_IP"), 8088, "/");
http.addHeader("Content-Type", "text/plain");
auto httpCode = http.POST("foo");
Serial.println(httpCode);
REQUIRE(httpCode == HTTP_CODE_OK);
http.end();
httpCode = http.POST("bar");
// its not expected to work but should not crash
REQUIRE(httpCode == HTTPC_ERROR_CONNECTION_REFUSED);
http.end();
}
}
TEST_CASE("HTTPS GET request", "[HTTPClient]")
{
//
// Tests with BearSSL
//
{
// small request
BearSSL::WiFiClientSecure client;
client.setFingerprint(fp);
HTTPClient http;
http.begin(client, getenv("SERVER_IP"), 8088, "/", fp);
auto httpCode = http.GET();
REQUIRE(httpCode == HTTP_CODE_OK);
String payload = http.getString();
REQUIRE(payload == "hello!!!");
}
{
// request which returns 4000 bytes
BearSSL::WiFiClientSecure client;
client.setFingerprint(fp);
HTTPClient http;
http.begin(client, getenv("SERVER_IP"), 8088, "/data?size=4000", fp);
auto httpCode = http.GET();
REQUIRE(httpCode == HTTP_CODE_OK);
String payload = http.getString();
auto len = payload.length();
REQUIRE(len == 4000);
for (int i = 0; i < len; ++i) {
if (payload[i] != 'a') {
REQUIRE(false);
}
}
}
//
// Same tests with axTLS
//
{
// small request
axTLS::WiFiClientSecure client;
HTTPClient http;
http.begin(client, getenv("SERVER_IP"), 8088, "/", fp);
auto httpCode = http.GET();
REQUIRE(httpCode == HTTP_CODE_OK);
String payload = http.getString();
REQUIRE(payload == "hello!!!");
}
{
// request which returns 4000 bytes
axTLS::WiFiClientSecure client;
HTTPClient http;
http.begin(client, getenv("SERVER_IP"), 8088, "/data?size=4000", fp);
auto httpCode = http.GET();
REQUIRE(httpCode == HTTP_CODE_OK);
String payload = http.getString();
auto len = payload.length();
REQUIRE(len == 4000);
for (int i = 0; i < len; ++i) {
if (payload[i] != 'a') {
REQUIRE(false);
}
}
}
}
void loop()
{
}