From 16afe10a59a8a4602890bd6daa611ea8739df5dc Mon Sep 17 00:00:00 2001 From: Greg Kostin Date: Wed, 8 Nov 2023 12:43:27 -0500 Subject: [PATCH] mock http service --- apache/client/include/lauth/http_client.hpp | 1 + apache/client/meson.build | 17 +++++++++--- apache/client/src/lauth/http_client.cpp | 4 +++ apache/client/test/lauth/http_client_test.cpp | 27 +++++++++++++++---- apache/client/test/lauth/mock_service.cpp | 16 +++++++++++ 5 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 apache/client/test/lauth/mock_service.cpp diff --git a/apache/client/include/lauth/http_client.hpp b/apache/client/include/lauth/http_client.hpp index 64149791..4f843dcf 100644 --- a/apache/client/include/lauth/http_client.hpp +++ b/apache/client/include/lauth/http_client.hpp @@ -6,6 +6,7 @@ namespace mlibrary::lauth { class HttpClient { public: + HttpClient(std::string host = "", uint16_t port = 0); virtual ~HttpClient() = default; virtual bool isAllowed(Request req); diff --git a/apache/client/meson.build b/apache/client/meson.build index e2d98739..bbba6f7d 100644 --- a/apache/client/meson.build +++ b/apache/client/meson.build @@ -62,16 +62,25 @@ endif os = host_machine.system() if os == 'darwin' - http_check_links = [] + httplib_links = [] else - http_check_links = ['-lssl', '-lcrypto'] + httplib_links = ['-lssl', '-lcrypto'] endif -http_check = executable( +executable( + 'http-service', + files(['test/lauth/mock_service.cpp']), + dependencies: [ + cpp_httplib, + ], + link_args: httplib_links +) + +executable( 'http-check', files(['src/http_check.cpp']), dependencies: [ cpp_httplib, ], - link_args: http_check_links + link_args: httplib_links ) diff --git a/apache/client/src/lauth/http_client.cpp b/apache/client/src/lauth/http_client.cpp index 66552431..0ce47188 100644 --- a/apache/client/src/lauth/http_client.cpp +++ b/apache/client/src/lauth/http_client.cpp @@ -1,6 +1,10 @@ #include "lauth/http_client.hpp" namespace mlibrary::lauth { + HttpClient::HttpClient(std::string host, uint16_t port) { + + } + bool HttpClient::isAllowed(Request req) { return (req.user == "authorized"); } diff --git a/apache/client/test/lauth/http_client_test.cpp b/apache/client/test/lauth/http_client_test.cpp index 0816683d..d89466f7 100644 --- a/apache/client/test/lauth/http_client_test.cpp +++ b/apache/client/test/lauth/http_client_test.cpp @@ -3,16 +3,33 @@ using testing::_; +#include + #include "lauth/http_client.hpp" #include "lauth/request.hpp" using namespace mlibrary::lauth; -TEST(HttpClientTest, a_request_with_authorized_user_is_allowed) { - HttpClient client; - Request request; +TEST(HttpClientTest, mock_service_response_with_is_allowed) { + std::string host = ""; + uint16_t port = 0; + HttpClient client(host, port); + + Request req; + req.user = "authorized"; - request.user = "authorized"; - bool result = client.isAllowed(request); + bool result = client.isAllowed(req); EXPECT_THAT(result, true); } + +TEST(HttpClientTest, mock_service_response_with_is_not_allowed) { + std::string host = ""; + uint16_t port = 0; + HttpClient client(host, port); + + Request req; + req.user = "authorized"; + + bool result = client.isAllowed(req); + EXPECT_THAT(result, false); +} diff --git a/apache/client/test/lauth/mock_service.cpp b/apache/client/test/lauth/mock_service.cpp new file mode 100644 index 00000000..4529299a --- /dev/null +++ b/apache/client/test/lauth/mock_service.cpp @@ -0,0 +1,16 @@ + + #include + + int main(int argc, char** argv) + { + using namespace httplib; + + Server micro_service; + + micro_service.Get("/user/:id/is_allowed", [](const Request& req, Response& res) { + auto user_id = req.path_params.at("id"); + res.set_content("no", "text/plain"); + }); + + micro_service.listen ("0.0.0.0", 8080); + } \ No newline at end of file