Forráskód Böngészése

add tests for kbf::http

Bence Balint 3 éve
szülő
commit
8edca548b5
5 módosított fájl, 125 hozzáadás és 5 törlés
  1. 7 1
      src/kbf_http_client.cpp
  2. 112 0
      test/test_http.cpp
  3. 2 0
      test_app/CMakeLists.txt
  4. 1 1
      test_app/partitions.csv
  5. 3 3
      test_app/sdkconfig

+ 7 - 1
src/kbf_http_client.cpp

@@ -31,10 +31,16 @@ void kbf::http::Client::task(void *arg) {
     esp_http_client_set_url(client->handle, client->url.c_str());
 
     auto err = esp_http_client_perform(client->handle);
+    if (err == ESP_ERR_HTTP_MAX_REDIRECT) {
+        // TODO this seems get triggered on HTTP 405... is this a bug in IDF?
+        ESP_LOGW(TAG, "reached redirect limit");
+        err = ESP_OK;
+    }
+
     if (err == ESP_OK) {
         Response response{};
         response.status = esp_http_client_get_status_code(client->handle);
-        response.body = std::string_view(client->buffer);
+        response.body   = std::string_view(client->buffer);
 
         ESP_LOGI(TAG, "HTTP status: %d, content-length: %d", response.status, response.contentLength);
         ESP_LOG_BUFFER_HEXDUMP(TAG, response.body.data(), response.body.length(), ESP_LOG_INFO);

+ 112 - 0
test/test_http.cpp

@@ -0,0 +1,112 @@
+#include <unity.h>
+
+#include <atomic>
+
+#include "kbf_wifi.h"
+#include "kbf_http_server.h"
+#include "kbf_http_client.h"
+
+using namespace kbf;
+using std::atomic;
+
+atomic<bool> finished;
+
+TEST_CASE("HTTP server <--> client / GET, POST, 404, 405", "[kbf_http]") {
+    wifi::start(wifi::STA::create());
+
+    auto server = http::Server();
+
+    http::Response (*handleGet)(const http::Request &) = {[](const http::Request &request) {
+        TEST_ASSERT_EQUAL(http::GET, request.method);
+        return http::Response("OK");
+    }};
+    server.route({http::GET, "/get-only", handleGet});
+
+    http::Response (*handlePost)(const http::Request &) = {[](const http::Request &request) {
+        TEST_ASSERT_EQUAL(http::POST, request.method);
+        return http::Response("OK");
+    }};
+    server.route({http::POST, "/post-only", handlePost});
+
+    http::Response (*handleBoth)(const http::Request &) = {[](const http::Request &request) {
+        if (request.method == http::GET) {
+            return http::Response("GET");
+        } else if (request.method == http::POST) {
+            return http::Response("POST");
+        } else {
+            TEST_FAIL();
+            return http::Response("fail"); // unreachable but the compiler moans otherwise
+        }
+    }};
+    server.route({http::GET, "/both", handleBoth});
+    server.route({http::POST, "/both", handleBoth});
+
+    server.start();
+
+
+    auto client = new http::Client();
+    finished = false;
+    client->onFinish = {[](http::Client &, http::Client::Response response) {
+        TEST_ASSERT_EQUAL(response.status, 200);
+        TEST_ASSERT_EQUAL_STRING("OK", response.body.data());
+        finished = true;
+    }};
+    client->get("http://localhost/get-only");
+    while (!finished);
+    delete client;
+
+
+    client = new http::Client();
+    finished = false;
+    client->onFinish = {[](http::Client &, http::Client::Response response) {
+        TEST_ASSERT_EQUAL(response.status, 405);
+        TEST_ASSERT_EQUAL_STRING("", response.body.data());
+        finished = true;
+    }};
+    client->get("http://localhost/post-only");
+    while (!finished);
+    delete client;
+
+
+    client = new http::Client();
+    finished = false;
+    client->onFinish = {[](http::Client &, http::Client::Response response) {
+        TEST_ASSERT_EQUAL(response.status, 200);
+        TEST_ASSERT_EQUAL_STRING("GET", response.body.data());
+        finished = true;
+    }};
+    client->get("http://localhost/both");
+    while (!finished);
+
+    // TODO test POST once the client can do POST
+}
+
+TEST_CASE("HTTP server <--> client / custom headers", "[kbf_http]") {
+    auto server = http::Server();
+
+    http::Response (*handleContentTypeTest)(const http::Request &) = {[](const http::Request &request) {
+        auto response = http::Response("OK");
+        if (request.query.at("type") == "txt") {
+            response.contentType = "text/plain";
+        }
+        return response;
+    }};
+    server.route({http::GET, "/test-content-type", handleContentTypeTest});
+
+    http::Response (*handleCustomHeaderTest)(const http::Request &) = {[](const http::Request &request) {
+        auto response = http::Response("OK");
+        response.headers["X-Secret"] = request.query.at("secret");
+        return response;
+    }};
+    server.route({http::GET, "/test-custom-header", handleCustomHeaderTest});
+
+    // TODO finish test after adding header support to Client
+}
+
+TEST_CASE("HTTP server <--> client / CORS", "[kbf_http]") {
+    // TODO finish test after adding header support to Client
+}
+
+TEST_CASE("HTTP server <--> client / SPIFFS static route", "[kbf_http]") {
+    TEST_FAIL_MESSAGE("not yet implemented");
+}

+ 2 - 0
test_app/CMakeLists.txt

@@ -1,6 +1,8 @@
 # This is the project CMakeLists.txt file for the test subproject
 cmake_minimum_required(VERSION 3.5)
 
+set(CMAKE_CXX_STANDARD 17)
+
 # Include the components directory of the main application:
 #
 set(EXTRA_COMPONENT_DIRS "../..")

+ 1 - 1
test_app/partitions.csv

@@ -2,5 +2,5 @@
 # Note: if you have increased the bootloader size, make sure to update the offsets to avoid overlap
 nvs,      data, nvs,     0x9000,  0x6000,
 phy_init, data, phy,     0xf000,  0x1000,
-factory,  app,  factory, 0x10000, 1M,
+factory,  app,  factory, 0x10000, 2M,
 storage,  data, spiffs,  ,        0xF0000,

+ 3 - 3
test_app/sdkconfig

@@ -87,11 +87,11 @@ CONFIG_ESPTOOLPY_FLASHFREQ_40M=y
 # CONFIG_ESPTOOLPY_FLASHFREQ_20M is not set
 CONFIG_ESPTOOLPY_FLASHFREQ="40m"
 # CONFIG_ESPTOOLPY_FLASHSIZE_1MB is not set
-CONFIG_ESPTOOLPY_FLASHSIZE_2MB=y
-# CONFIG_ESPTOOLPY_FLASHSIZE_4MB is not set
+# CONFIG_ESPTOOLPY_FLASHSIZE_2MB is not set
+CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
 # CONFIG_ESPTOOLPY_FLASHSIZE_8MB is not set
 # CONFIG_ESPTOOLPY_FLASHSIZE_16MB is not set
-CONFIG_ESPTOOLPY_FLASHSIZE="2MB"
+CONFIG_ESPTOOLPY_FLASHSIZE="4MB"
 CONFIG_ESPTOOLPY_FLASHSIZE_DETECT=y
 CONFIG_ESPTOOLPY_BEFORE_RESET=y
 # CONFIG_ESPTOOLPY_BEFORE_NORESET is not set