From 22bd3585e8fc0873420c93dc671e8fc6b15f283c Mon Sep 17 00:00:00 2001 From: Athul Raj Kollareth Date: Sun, 1 Sep 2024 02:03:28 +0530 Subject: [PATCH] Revise the test --- CMakeLists.txt | 18 +++++-- examples/example1.cpp | 4 +- test/CMakeLists.txt | 14 +++++ {images => test/data/images}/gato.jpg | Bin {images => test/data/images}/gato.png | Bin test/test1.cpp | 75 ++++++++++++++++++++++++++ 6 files changed, 106 insertions(+), 5 deletions(-) create mode 100644 test/CMakeLists.txt rename {images => test/data/images}/gato.jpg (100%) rename {images => test/data/images}/gato.png (100%) create mode 100644 test/test1.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f1bd5db..a9f51ab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,10 +5,22 @@ project(TinyAPI VERSION "0.0.1") set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) -add_library(TinyApi - src/tinyapi.cpp +include(FetchContent) +FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG b514bdc898e2951020cbdca1304b75f5950d1f59 # v1.15.2 ) +FetchContent_MakeAvailable(googletest) -target_include_directories(TinyApi PRIVATE +enable_testing() + +include_directories( ${CMAKE_SOURCE_DIR}/include ) + +add_subdirectory(test) + +add_library(TinyApi + src/tinyapi.cpp +) diff --git a/examples/example1.cpp b/examples/example1.cpp index cf208c7..1fe3eeb 100644 --- a/examples/example1.cpp +++ b/examples/example1.cpp @@ -1,5 +1,5 @@ -#include "helper.h" -#include "tinyapi.h" +#include "../include/helper.h" +#include "../include/tinyapi.h" #include #include #include diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt new file mode 100644 index 0000000..dcaa42b --- /dev/null +++ b/test/CMakeLists.txt @@ -0,0 +1,14 @@ +find_package(CURL REQUIRED) + +add_executable( + hello_test + test1.cpp +) +target_link_libraries(hello_test + TinyApi + GTest::gtest_main + CURL::libcurl +) + +include(GoogleTest) +gtest_discover_tests(hello_test) diff --git a/images/gato.jpg b/test/data/images/gato.jpg similarity index 100% rename from images/gato.jpg rename to test/data/images/gato.jpg diff --git a/images/gato.png b/test/data/images/gato.png similarity index 100% rename from images/gato.png rename to test/data/images/gato.png diff --git a/test/test1.cpp b/test/test1.cpp new file mode 100644 index 0000000..ca525a3 --- /dev/null +++ b/test/test1.cpp @@ -0,0 +1,75 @@ +#include +#include +#include +#include +#include + +#include "helper.h" +#include "tinyapi.h" + +// route = /home +static std::tuple HomePage() { + std::map responseMap; + responseMap["Greet"] = "Welcome Home!"; + auto response = Helper::MapToString(responseMap); + auto responseTup = std::make_tuple(response, "text/html"); + return responseTup; +} + +// connector +static std::tuple connector(std::string endpoint) { + if (endpoint == "/home") + return HomePage(); + + std::string err404 = "404 - Page Not Found!"; + return std::make_tuple(err404, "text/html"); +} + +class FooServer { +public: + std::string local_host = "127.0.0.1"; + bool status_Running = false; + TinyAPI *new_api; + + int init_server(); + void start_server(); +}; + +class FooTest : public testing::Test { +protected: + FooServer m_serverObj; + std::unique_ptr m_server_thread; +}; + +int FooServer::init_server() { + new_api = new TinyAPI(8000, 1024, 5, local_host, 2000, TinyAPI::HOST_OS::WIN); + return new_api->initialize_server(); +} + +void FooServer::start_server() { + status_Running = true; + new_api->HttpRequestHandler(&connector); +} + +TEST_F(FooTest, fooTest) { + ASSERT_EQ(m_serverObj.init_server(), 0); + // launch server in a different thread + m_server_thread = + std::make_unique(&FooServer::start_server, &m_serverObj); + while (!m_serverObj.status_Running) { + // stay still + } + // forward a GET request to the server + CURL *curl; + curl = curl_easy_init(); + if (curl) { + curl_easy_setopt(curl, CURLOPT_URL, "http://127.0.0.1:8000/home"); + CURLcode res = curl_easy_perform(curl); + ASSERT_EQ(res, CURLE_OK); + curl_easy_cleanup(curl); + } else { + FAIL(); + } + // wait for server timeout + m_server_thread->join(); +}