diff --git a/src/server.cpp b/src/server.cpp index a1a77d6..e6c3fc5 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -77,10 +77,10 @@ namespace network #ifdef ENABLE_SSL void server::load_certificate(const std::string &cert_file, const std::string &key_file) { - LOG_DEBUG("Loading certificate: " + cert_file); - ctx.use_certificate_chain_file(cert_file); - LOG_DEBUG("Loading private key: " + key_file); - ctx.use_private_key_file(key_file, asio::ssl::context::pem); + LOG_DEBUG("Loading certificate: " + cert_file); + ctx.use_certificate_chain_file(cert_file); + LOG_DEBUG("Loading private key: " + key_file); + ctx.use_private_key_file(key_file, asio::ssl::context::pem); } #endif diff --git a/tests/test_server.cpp b/tests/test_server.cpp index 80a1dbc..b77f172 100644 --- a/tests/test_server.cpp +++ b/tests/test_server.cpp @@ -1,6 +1,22 @@ #include "server.hpp" #include +/** + * @brief Test function to set up and run a REST server with various routes. + * + * This function initializes a network server and sets up several routes: + * - A root route ("/") that returns an HTML response with "Hello, World!". + * - A JSON route ("/json") that returns a JSON response with a message "Hello, World!". + * - A WebSocket route ("/ws") that returns an HTML page with a WebSocket client script. + * + * If SSL is enabled (ENABLE_SSL), the server loads the SSL certificate and key. + * + * The WebSocket route ("/ws") also sets up handlers for WebSocket events: + * - on_open: Sends "Hello, World!" when a WebSocket connection is opened. + * - on_message: Echoes back any received message. + * + * The server runs in a separate thread for 100 seconds before stopping. + */ void test_rest_server() { network::server server; @@ -34,9 +50,51 @@ void test_rest_server() t.join(); } +/** + * @brief Tests the CORS (Cross-Origin Resource Sharing) functionality of the server. + * + * This function sets up a server with three routes: + * - A GET route at "/json" that returns a JSON response with a "Hello, World!" message and allows CORS from any origin. + * - A POST route at "/json" that returns a JSON response with a "Hello, World!" message and allows CORS from any origin. + * - An OPTIONS route at "/json" that returns the allowed methods (GET, POST, OPTIONS) and allowed headers (Content-Type) for CORS. + * + * The server runs in a separate thread for 100 seconds before stopping. + */ +void test_cors_server() +{ + network::server server; + server.add_route(network::verb::Get, "/json", [](network::request &req) + { + std::map headers; + headers["Access-Control-Allow-Origin"] = "*"; + return std::make_unique(json::json{{"message", "Hello, World!"}}, network::ok, std::move(headers)); }); + + server.add_route(network::verb::Post, "/json", [](network::request &req) + { + std::map headers; + headers["Access-Control-Allow-Origin"] = "*"; + return std::make_unique(json::json{{"message", "Hello, World!"}}, network::ok, std::move(headers)); }); + + server.add_route(network::verb::Options, "/json", [](network::request &req) + { + std::map headers; + headers["Access-Control-Allow-Origin"] = "*"; + headers["Access-Control-Allow-Methods"] = "GET, POST, OPTIONS"; + headers["Access-Control-Allow-Headers"] = "Content-Type"; + return std::make_unique(network::ok, std::move(headers)); }); + + std::thread t{[&server] + { server.start(); }}; + std::this_thread::sleep_for(std::chrono::seconds(100)); + server.stop(); + t.join(); +} + int main(int argc, char const *argv[]) { - test_rest_server(); + // test_rest_server(); + + test_cors_server(); return 0; }