Skip to content

Commit

Permalink
mock http service
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Nov 8, 2023
1 parent 8ee58e5 commit 16afe10
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 9 deletions.
1 change: 1 addition & 0 deletions apache/client/include/lauth/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
17 changes: 13 additions & 4 deletions apache/client/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
4 changes: 4 additions & 0 deletions apache/client/src/lauth/http_client.cpp
Original file line number Diff line number Diff line change
@@ -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");
}
Expand Down
27 changes: 22 additions & 5 deletions apache/client/test/lauth/http_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,33 @@

using testing::_;

#include <httplib.h>

#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);
}
16 changes: 16 additions & 0 deletions apache/client/test/lauth/mock_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

#include <httplib.h>

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);
}

0 comments on commit 16afe10

Please sign in to comment.