|
@@ -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");
|
|
|
+}
|