Skip to content

Commit a42c3c3

Browse files
liebmandevyte
authored andcommitted
Fix device/test_http_client tests (#5309)
* update HTTPClient API usage skip the second POST as end() has different semantics and nulls the client pointer use bearssl in ssl tests add delay in python side when shutting down http web server so MacOS does not complain about address already in use * fix crash if GET/POST was called after end() without a new begin() update double POST test to insure no crash if POST called after end() test now are for both AxTLS and BearSSL * fix small comment typo
1 parent feb86cd commit a42c3c3

File tree

3 files changed

+49
-8
lines changed

3 files changed

+49
-8
lines changed

libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ bool HTTPClient::connect(void)
10361036
}
10371037

10381038
#ifdef HTTPCLIENT_1_1_COMPATIBLE
1039-
if(!_client) {
1039+
if(!_client && _transportTraits) {
10401040
_tcpDeprecated = _transportTraits->create();
10411041
_client = _tcpDeprecated.get();
10421042
}

tests/device/test_http_client/test_http_client.ino

+46-7
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,19 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]")
2424
{
2525
{
2626
// small request
27+
WiFiClient client;
2728
HTTPClient http;
28-
http.begin(getenv("SERVER_IP"), 8088, "/");
29+
http.begin(client, getenv("SERVER_IP"), 8088, "/");
2930
auto httpCode = http.GET();
3031
REQUIRE(httpCode == HTTP_CODE_OK);
3132
String payload = http.getString();
3233
REQUIRE(payload == "hello!!!");
3334
}
3435
{
3536
// request which returns 8000 bytes
37+
WiFiClient client;
3638
HTTPClient http;
37-
http.begin(getenv("SERVER_IP"), 8088, "/data?size=8000");
39+
http.begin(client, getenv("SERVER_IP"), 8088, "/data?size=8000");
3840
auto httpCode = http.GET();
3941
REQUIRE(httpCode == HTTP_CODE_OK);
4042
String payload = http.getString();
@@ -48,36 +50,74 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]")
4850
}
4951
{
5052
// can do two POST requests with one HTTPClient object (#1902)
53+
WiFiClient client;
5154
HTTPClient http;
52-
http.begin(getenv("SERVER_IP"), 8088, "/");
55+
http.begin(client, getenv("SERVER_IP"), 8088, "/");
5356
http.addHeader("Content-Type", "text/plain");
5457
auto httpCode = http.POST("foo");
5558
Serial.println(httpCode);
5659
REQUIRE(httpCode == HTTP_CODE_OK);
5760
http.end();
5861

5962
httpCode = http.POST("bar");
60-
REQUIRE(httpCode == HTTP_CODE_OK);
63+
// its not expected to work but should not crash
64+
REQUIRE(httpCode == HTTPC_ERROR_CONNECTION_REFUSED);
6165
http.end();
6266
}
6367
}
6468

6569

6670
TEST_CASE("HTTPS GET request", "[HTTPClient]")
6771
{
72+
//
73+
// Tests with BearSSL
74+
//
6875
{
6976
// small request
77+
BearSSL::WiFiClientSecure client;
78+
client.setFingerprint(fp);
7079
HTTPClient http;
71-
http.begin(getenv("SERVER_IP"), 8088, "/", fp);
80+
http.begin(client, getenv("SERVER_IP"), 8088, "/", fp);
7281
auto httpCode = http.GET();
7382
REQUIRE(httpCode == HTTP_CODE_OK);
7483
String payload = http.getString();
7584
REQUIRE(payload == "hello!!!");
7685
}
7786
{
7887
// request which returns 4000 bytes
88+
BearSSL::WiFiClientSecure client;
89+
client.setFingerprint(fp);
7990
HTTPClient http;
80-
http.begin(getenv("SERVER_IP"), 8088, "/data?size=4000", fp);
91+
http.begin(client, getenv("SERVER_IP"), 8088, "/data?size=4000", fp);
92+
auto httpCode = http.GET();
93+
REQUIRE(httpCode == HTTP_CODE_OK);
94+
String payload = http.getString();
95+
auto len = payload.length();
96+
REQUIRE(len == 4000);
97+
for (int i = 0; i < len; ++i) {
98+
if (payload[i] != 'a') {
99+
REQUIRE(false);
100+
}
101+
}
102+
}
103+
//
104+
// Same tests with axTLS
105+
//
106+
{
107+
// small request
108+
axTLS::WiFiClientSecure client;
109+
HTTPClient http;
110+
http.begin(client, getenv("SERVER_IP"), 8088, "/", fp);
111+
auto httpCode = http.GET();
112+
REQUIRE(httpCode == HTTP_CODE_OK);
113+
String payload = http.getString();
114+
REQUIRE(payload == "hello!!!");
115+
}
116+
{
117+
// request which returns 4000 bytes
118+
axTLS::WiFiClientSecure client;
119+
HTTPClient http;
120+
http.begin(client, getenv("SERVER_IP"), 8088, "/data?size=4000", fp);
81121
auto httpCode = http.GET();
82122
REQUIRE(httpCode == HTTP_CODE_OK);
83123
String payload = http.getString();
@@ -89,7 +129,6 @@ TEST_CASE("HTTPS GET request", "[HTTPClient]")
89129
}
90130
}
91131
}
92-
93132
}
94133

95134
void loop()

tests/device/test_http_client/test_http_client.py

+2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import urllib2
55
import os
66
import ssl
7+
import time
78

89
@setup('HTTP GET & POST requests')
910
def setup_http_get(e):
@@ -34,6 +35,7 @@ def flaskThread():
3435
def teardown_http_get(e):
3536
response = urllib2.urlopen('http://localhost:8088/shutdown')
3637
html = response.read()
38+
time.sleep(30)
3739

3840

3941
@setup('HTTPS GET request')

0 commit comments

Comments
 (0)