|
@@ -2,18 +2,18 @@
|
|
|
|
|
|
#include <atomic>
|
|
|
|
|
|
+#include "kbf.h"
|
|
|
#include "kbf_wifi.h"
|
|
|
-#include "kbf_http_server.h"
|
|
|
-#include "kbf_http_client.h"
|
|
|
+#include "kbf_http.h"
|
|
|
+#include "http/server.h"
|
|
|
+#include "http/client.h"
|
|
|
|
|
|
using namespace kbf;
|
|
|
-using std::atomic;
|
|
|
|
|
|
-atomic<bool> finished;
|
|
|
+std::atomic<bool> asyncFinished = {false};
|
|
|
|
|
|
TEST_CASE("HTTP server <--> client / GET, POST, 404, 405", "[kbf_http]") {
|
|
|
- wifi::start(wifi::STA::create());
|
|
|
-
|
|
|
+ wifi::start();
|
|
|
auto server = http::Server();
|
|
|
|
|
|
http::Response (*handleGet)(const http::Request &) = {[](const http::Request &request) {
|
|
@@ -28,7 +28,7 @@ TEST_CASE("HTTP server <--> client / GET, POST, 404, 405", "[kbf_http]") {
|
|
|
}};
|
|
|
server.route({http::POST, "/post-only", handlePost});
|
|
|
|
|
|
- http::Response (*handleBoth)(const http::Request &) = {[](const http::Request &request) {
|
|
|
+ http::Response (*handleGetAndPost)(const http::Request &) = {[](const http::Request &request) {
|
|
|
if (request.method == http::GET) {
|
|
|
return http::Response("GET");
|
|
|
} else if (request.method == http::POST) {
|
|
@@ -38,73 +38,100 @@ TEST_CASE("HTTP server <--> client / GET, POST, 404, 405", "[kbf_http]") {
|
|
|
return http::Response("fail"); // unreachable but the compiler moans otherwise
|
|
|
}
|
|
|
}};
|
|
|
- server.route({http::GET, "/both", handleBoth});
|
|
|
- server.route({http::POST, "/both", handleBoth});
|
|
|
+ server.route({http::GET, "/get-and-post", handleGetAndPost});
|
|
|
+ server.route({http::POST, "/get-and-post", handleGetAndPost});
|
|
|
|
|
|
server.start();
|
|
|
|
|
|
+ auto client = http::Client();
|
|
|
+ auto response = client.get("http://localhost/get-only");
|
|
|
+ TEST_ASSERT_EQUAL(200, response->status);
|
|
|
+ TEST_ASSERT_EQUAL_STRING("OK", response->body.c_str());
|
|
|
|
|
|
- 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;
|
|
|
+ response = client.get("http://localhost/non-existent");
|
|
|
+ TEST_ASSERT_EQUAL(404, response->status);
|
|
|
+ TEST_ASSERT_EQUAL_STRING("This URI does not exist", response->body.c_str());
|
|
|
|
|
|
+ response = client.get("http://localhost/post-only");
|
|
|
+ TEST_ASSERT_EQUAL(405, response->status);
|
|
|
+ TEST_ASSERT_EQUAL_STRING("Request method for this URI is not handled by server", response->body.c_str());
|
|
|
|
|
|
- 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);
|
|
|
+ response = client.get("http://localhost/get-and-post");
|
|
|
+ TEST_ASSERT_EQUAL(200, response->status);
|
|
|
+ TEST_ASSERT_EQUAL_STRING("GET", response->body.c_str());
|
|
|
|
|
|
// TODO test POST once the client can do POST
|
|
|
+
|
|
|
+ wifi::stop();
|
|
|
}
|
|
|
|
|
|
TEST_CASE("HTTP server <--> client / custom headers", "[kbf_http]") {
|
|
|
+ wifi::start();
|
|
|
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") {
|
|
|
+ if (!request.query.empty() && request.query.at("type") == "txt") {
|
|
|
response.contentType = "text/plain";
|
|
|
}
|
|
|
return response;
|
|
|
}};
|
|
|
- server.route({http::GET, "/test-content-type", handleContentTypeTest});
|
|
|
+ server.route({http::GET, "/content-type-test", 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});
|
|
|
+ server.route({http::GET, "/custom-header-test", handleCustomHeaderTest});
|
|
|
+
|
|
|
+ server.start();
|
|
|
+ auto client = http::Client();
|
|
|
+
|
|
|
+ auto response = client.get("http://localhost/content-type-test");
|
|
|
+ TEST_ASSERT_EQUAL_STRING("text/html", response->headers.at("Content-Type").c_str());
|
|
|
+ TEST_ASSERT_EQUAL_STRING("OK", response->body.data());
|
|
|
+
|
|
|
+ response = client.get("http://localhost/content-type-test?type=txt");
|
|
|
+ TEST_ASSERT_EQUAL_STRING("text/plain", response->headers.at("Content-Type").c_str());
|
|
|
+ TEST_ASSERT_EQUAL_STRING("OK", response->body.data());
|
|
|
+
|
|
|
+ response = client.get("http://localhost/custom-header-test?secret=THISis1337");
|
|
|
+ TEST_ASSERT_EQUAL_STRING("THISis1337", response->headers.at("X-Secret").c_str());
|
|
|
+ TEST_ASSERT_EQUAL_STRING("OK", response->body.data());
|
|
|
|
|
|
- // TODO finish test after adding header support to Client
|
|
|
+ wifi::stop();
|
|
|
+}
|
|
|
+
|
|
|
+TEST_CASE("HTTP server <--> client / async", "[kbf_http]") {
|
|
|
+ wifi::start();
|
|
|
+
|
|
|
+ http::Response (*handleRequest)(const http::Request &) = {[](const http::Request &request) {
|
|
|
+ auto response = http::Response("OK");
|
|
|
+ kbf::sleep(100);
|
|
|
+ return response;
|
|
|
+ }};
|
|
|
+ auto server = http::Server()
|
|
|
+ .route({http::GET, "/", handleRequest})
|
|
|
+ .start();
|
|
|
+
|
|
|
+ auto client = http::Client(true);
|
|
|
+ client.onSuccess = {[](http::Client &client, const http::Response &response) {
|
|
|
+ TEST_ASSERT_EQUAL_STRING("OK", response.body.data());
|
|
|
+ asyncFinished = true;
|
|
|
+ }};
|
|
|
+
|
|
|
+ auto response = client.get("http://localhost/");
|
|
|
+ TEST_ASSERT_NULL(response);
|
|
|
+ TEST_ASSERT_EQUAL(false, asyncFinished);
|
|
|
+ kbf::sleep(200);
|
|
|
+ TEST_ASSERT_EQUAL(true, asyncFinished);
|
|
|
+
|
|
|
+ wifi::stop();
|
|
|
}
|
|
|
|
|
|
TEST_CASE("HTTP server <--> client / CORS", "[kbf_http]") {
|
|
|
- // TODO finish test after adding header support to Client
|
|
|
+ TEST_FAIL_MESSAGE("not yet implemented");
|
|
|
}
|
|
|
|
|
|
TEST_CASE("HTTP server <--> client / SPIFFS static route", "[kbf_http]") {
|