diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 62031fb50a..ecac309384 100644 --- a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp +++ b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp @@ -1036,7 +1036,7 @@ bool HTTPClient::connect(void) } #ifdef HTTPCLIENT_1_1_COMPATIBLE - if(!_client) { + if(!_client && _transportTraits) { _tcpDeprecated = _transportTraits->create(); _client = _tcpDeprecated.get(); } diff --git a/tests/device/test_http_client/test_http_client.ino b/tests/device/test_http_client/test_http_client.ino index db217ba56d..12aefd2b54 100644 --- a/tests/device/test_http_client/test_http_client.ino +++ b/tests/device/test_http_client/test_http_client.ino @@ -24,8 +24,9 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") { { // small request + WiFiClient client; HTTPClient http; - http.begin(getenv("SERVER_IP"), 8088, "/"); + http.begin(client, getenv("SERVER_IP"), 8088, "/"); auto httpCode = http.GET(); REQUIRE(httpCode == HTTP_CODE_OK); String payload = http.getString(); @@ -33,8 +34,9 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") } { // request which returns 8000 bytes + WiFiClient client; HTTPClient http; - http.begin(getenv("SERVER_IP"), 8088, "/data?size=8000"); + http.begin(client, getenv("SERVER_IP"), 8088, "/data?size=8000"); auto httpCode = http.GET(); REQUIRE(httpCode == HTTP_CODE_OK); String payload = http.getString(); @@ -48,8 +50,9 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") } { // can do two POST requests with one HTTPClient object (#1902) + WiFiClient client; HTTPClient http; - http.begin(getenv("SERVER_IP"), 8088, "/"); + http.begin(client, getenv("SERVER_IP"), 8088, "/"); http.addHeader("Content-Type", "text/plain"); auto httpCode = http.POST("foo"); Serial.println(httpCode); @@ -57,7 +60,8 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") http.end(); httpCode = http.POST("bar"); - REQUIRE(httpCode == HTTP_CODE_OK); + // its not expected to work but should not crash + REQUIRE(httpCode == HTTPC_ERROR_CONNECTION_REFUSED); http.end(); } } @@ -65,10 +69,15 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") TEST_CASE("HTTPS GET request", "[HTTPClient]") { + // + // Tests with BearSSL + // { // small request + BearSSL::WiFiClientSecure client; + client.setFingerprint(fp); HTTPClient http; - http.begin(getenv("SERVER_IP"), 8088, "/", fp); + http.begin(client, getenv("SERVER_IP"), 8088, "/", fp); auto httpCode = http.GET(); REQUIRE(httpCode == HTTP_CODE_OK); String payload = http.getString(); @@ -76,8 +85,39 @@ TEST_CASE("HTTPS GET request", "[HTTPClient]") } { // request which returns 4000 bytes + BearSSL::WiFiClientSecure client; + client.setFingerprint(fp); HTTPClient http; - http.begin(getenv("SERVER_IP"), 8088, "/data?size=4000", fp); + 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(); @@ -89,7 +129,6 @@ TEST_CASE("HTTPS GET request", "[HTTPClient]") } } } - } void loop() diff --git a/tests/device/test_http_client/test_http_client.py b/tests/device/test_http_client/test_http_client.py index bfb518164f..a78d4108df 100644 --- a/tests/device/test_http_client/test_http_client.py +++ b/tests/device/test_http_client/test_http_client.py @@ -4,6 +4,7 @@ import urllib2 import os import ssl +import time @setup('HTTP GET & POST requests') def setup_http_get(e): @@ -34,6 +35,7 @@ def flaskThread(): def teardown_http_get(e): response = urllib2.urlopen('http://localhost:8088/shutdown') html = response.read() + time.sleep(30) @setup('HTTPS GET request')