From a42c3c399b2742c7f241b039b2bafd187515f1a8 Mon Sep 17 00:00:00 2001 From: liebman Date: Tue, 6 Nov 2018 17:56:10 -0800 Subject: [PATCH] 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 --- .../src/ESP8266HTTPClient.cpp | 2 +- .../test_http_client/test_http_client.ino | 53 ++++++++++++++++--- .../test_http_client/test_http_client.py | 2 + 3 files changed, 49 insertions(+), 8 deletions(-) diff --git a/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp b/libraries/ESP8266HTTPClient/src/ESP8266HTTPClient.cpp index 62031fb50..ecac30938 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 db217ba56..12aefd2b5 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 bfb518164..a78d4108d 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 @@ from threading import Thread import urllib2 import os import ssl +import time @setup('HTTP GET & POST requests') def setup_http_get(e): @@ -34,6 +35,7 @@ def setup_http_get(e): def teardown_http_get(e): response = urllib2.urlopen('http://localhost:8088/shutdown') html = response.read() + time.sleep(30) @setup('HTTPS GET request')