123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- #include <unity.h>
- #include <atomic>
- #include "kbf.h"
- #include "kbf/wifi.h"
- #include "kbf/http/server.h"
- #include "kbf/http/client.h"
- using namespace kbf;
- std::atomic<bool> asyncFinished = {false};
- TEST_CASE("HTTP server <--> client / GET, POST, 404, 405", "[kbf_http]") {
- wifi::start();
- 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 (*handleGetAndPost)(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, "/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());
- 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());
- 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.empty() && request.query.at("type") == "txt") {
- response.contentType = "text/plain";
- }
- return response;
- }};
- 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, "/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());
- 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]") {
- TEST_FAIL_MESSAGE("not yet implemented");
- }
- TEST_CASE("HTTP server <--> client / SPIFFS static route", "[kbf_http]") {
- TEST_FAIL_MESSAGE("not yet implemented");
- }
|