Skip to content

Commit

Permalink
all test green
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Nov 8, 2023
1 parent 1ac3c15 commit 947b9f3
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 7 deletions.
2 changes: 1 addition & 1 deletion apache/client/include/lauth/http_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace mlibrary::lauth {
virtual ~HttpClient() = default;

virtual bool isAllowed(Request req);
virtual std::string get(const std::string& url);
virtual std::string get(const std::string& path);


protected:
Expand Down
11 changes: 6 additions & 5 deletions apache/client/src/lauth/http_client.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#include "lauth/http_client.hpp"

#include <httplib.h>

namespace mlibrary::lauth {
std::string HttpClient::get(const std::string& path) {
httplib::Client client(baseUrl);

bool HttpClient::isAllowed(Request req) {
return (req.user == "authorized");
}
auto res = client.Get(path);

std::string HttpClient::get(const std::string& url) {
return "Root";
return res->body;
}
}

8 changes: 7 additions & 1 deletion apache/client/test/lauth/http_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,16 @@ using namespace mlibrary::lauth;

const std::string api_url = "http://localhost:9000";

TEST(HttpClientTest, mock_service_response) {
TEST(HttpClient, get_request_returns_body) {
HttpClient client(api_url);

std::string response = client.get("/");
EXPECT_THAT(response, "Root");
}

TEST(HttpClient, get_request_with_path_returns_body) {
HttpClient client(api_url);

std::string response = client.get("/ping");
EXPECT_THAT(response, "pong");
}
1 change: 1 addition & 0 deletions apache/client/test/lauth/mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class MockHttpClient : public HttpClient {
public:
MockHttpClient() : HttpClient("http://localhost:9000") {};
MOCK_METHOD(bool, isAllowed, (Request), (override));
MOCK_METHOD(std::string, get, (const std::string&), (override));
};

#endif
4 changes: 4 additions & 0 deletions apache/client/test/mock_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ int main(int argc, char **argv) {
res.set_content("Root", "text/plain");
});

server.Get("/ping", [](const Request &req, Response &res) {
res.set_content("pong", "text/plain");
});

server.Get("/stop", [&](const Request &req, Response &res) {
res.set_content("Shutting down server...", "text/plain");
server.stop();
Expand Down

0 comments on commit 947b9f3

Please sign in to comment.