Skip to content

Add Test Case #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 2 additions & 2 deletions examples/example1.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include "helper.h"
#include "tinyapi.h"
#include "../include/helper.h"
#include "../include/tinyapi.h"
#include <cstddef>
#include <iostream>
#include <ostream>
Expand Down
14 changes: 14 additions & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
File renamed without changes
File renamed without changes
75 changes: 75 additions & 0 deletions test/test1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
#include <curl/curl.h>
#include <gtest/gtest.h>
#include <memory>
#include <thread>
#include <unistd.h>

#include "helper.h"
#include "tinyapi.h"

// route = /home
static std::tuple<std::string, std::string> HomePage() {
std::map<std::string, std::string> responseMap;
responseMap["Greet"] = "Welcome Home!";
auto response = Helper::MapToString(responseMap);
auto responseTup = std::make_tuple(response, "text/html");
return responseTup;
}

// connector
static std::tuple<std::string, std::string> 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<std::thread> 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<std::thread>(&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();
}