From e68824c49349613f7c603c5012347c0912fea25e Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Sun, 4 Nov 2018 17:05:01 -0800 Subject: [PATCH 1/3] 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 --- .../test_http_client/test_http_client.ino | 20 +++++++++++++------ .../test_http_client/test_http_client.py | 2 ++ 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/tests/device/test_http_client/test_http_client.ino b/tests/device/test_http_client/test_http_client.ino index db217ba56d..61e86c92b1 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,17 +50,20 @@ 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); REQUIRE(httpCode == HTTP_CODE_OK); +/* TBD: This test is broken because end() nulls the client* http.end(); httpCode = http.POST("bar"); REQUIRE(httpCode == HTTP_CODE_OK); http.end(); +*/ } } @@ -67,8 +72,10 @@ TEST_CASE("HTTPS GET request", "[HTTPClient]") { { // small request + BearSSL::WiFiClientSecure client; + client.setInsecure(); 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 +83,10 @@ TEST_CASE("HTTPS GET request", "[HTTPClient]") } { // request which returns 4000 bytes + BearSSL::WiFiClientSecure client; + client.setInsecure(); 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(); @@ -89,7 +98,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') From a2304d4b19ada935012a694bceb26f163b9f0cc8 Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Mon, 5 Nov 2018 17:08:42 -0800 Subject: [PATCH 2/3] 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 --- .../src/ESP8266HTTPClient.cpp | 2 +- .../test_http_client/test_http_client.ino | 41 ++++++++++++++++--- 2 files changed, 37 insertions(+), 6 deletions(-) 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 61e86c92b1..5a43e692d6 100644 --- a/tests/device/test_http_client/test_http_client.ino +++ b/tests/device/test_http_client/test_http_client.ino @@ -57,23 +57,25 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") auto httpCode = http.POST("foo"); Serial.println(httpCode); REQUIRE(httpCode == HTTP_CODE_OK); -/* TBD: This test is broken because end() nulls the client* http.end(); httpCode = http.POST("bar"); - REQUIRE(httpCode == HTTP_CODE_OK); + // its not expected to work but should not ccrash + REQUIRE(httpCode == HTTPC_ERROR_CONNECTION_REFUSED); http.end(); -*/ } } TEST_CASE("HTTPS GET request", "[HTTPClient]") { + // + // Tests with BearSSL + // { // small request BearSSL::WiFiClientSecure client; - client.setInsecure(); + client.setFingerprint(fp); HTTPClient http; http.begin(client, getenv("SERVER_IP"), 8088, "/", fp); auto httpCode = http.GET(); @@ -84,7 +86,36 @@ TEST_CASE("HTTPS GET request", "[HTTPClient]") { // request which returns 4000 bytes BearSSL::WiFiClientSecure client; - client.setInsecure(); + 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(); From 317316e02613480d641e7aae4f9a7e8de3a1845f Mon Sep 17 00:00:00 2001 From: Chris Liebman Date: Mon, 5 Nov 2018 17:12:30 -0800 Subject: [PATCH 3/3] fix small comment typo --- tests/device/test_http_client/test_http_client.ino | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/device/test_http_client/test_http_client.ino b/tests/device/test_http_client/test_http_client.ino index 5a43e692d6..12aefd2b54 100644 --- a/tests/device/test_http_client/test_http_client.ino +++ b/tests/device/test_http_client/test_http_client.ino @@ -60,7 +60,7 @@ TEST_CASE("HTTP GET & POST requests", "[HTTPClient]") http.end(); httpCode = http.POST("bar"); - // its not expected to work but should not ccrash + // its not expected to work but should not crash REQUIRE(httpCode == HTTPC_ERROR_CONNECTION_REFUSED); http.end(); }