-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from mlibrary/http_client
http client ping pong
- Loading branch information
Showing
14 changed files
with
228 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef __LAUTH_HTTP_CLIENT_HPP__ | ||
#define __LAUTH_HTTP_CLIENT_HPP__ | ||
|
||
#include "lauth/request.hpp" | ||
|
||
namespace mlibrary::lauth { | ||
class HttpClient { | ||
public: | ||
HttpClient(const std::string& baseUrl) : baseUrl(baseUrl) {}; | ||
virtual ~HttpClient() = default; | ||
|
||
virtual bool isAllowed(Request req); | ||
virtual std::string get(const std::string& path); | ||
|
||
|
||
protected: | ||
const std::string baseUrl; | ||
}; | ||
} | ||
|
||
#endif // __LAUTH_HTTP_CLIENT_HPP__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
#include "lauth/api_client.hpp" | ||
|
||
// #include <string> | ||
|
||
namespace mlibrary::lauth { | ||
bool ApiClient::isAllowed(Request req) { | ||
return req.user.size() > 0; | ||
// bool result = http_client(req.uri, req.user); | ||
return client->isAllowed(req); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +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); | ||
|
||
auto res = client.Get(path); | ||
|
||
return res->body; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#include <gtest/gtest.h> | ||
#include <gmock/gmock.h> | ||
|
||
using testing::_; | ||
|
||
#include <httplib.h> | ||
|
||
#include "lauth/http_client.hpp" | ||
#include "lauth/request.hpp" | ||
|
||
using namespace mlibrary::lauth; | ||
|
||
const std::string api_url = "http://localhost:9000"; | ||
|
||
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"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#include <lauth/request.hpp> | ||
#include <gtest/gtest.h> | ||
|
||
TEST(Request, is_ok) { | ||
TEST(RequestTest, is_ok) { | ||
EXPECT_EQ(1, 1); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <httplib.h> | ||
|
||
int main(int argc, char **argv) { | ||
using namespace httplib; | ||
|
||
int port; | ||
const std::string address = "0.0.0.0"; | ||
Server server; | ||
|
||
if (argc == 2) { | ||
port = std::atoi(argv[1]); | ||
server.bind_to_port(address, port); | ||
} else { | ||
port = server.bind_to_any_port(address); | ||
} | ||
|
||
server.Get("/", [](const Request &req, Response &res) { | ||
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(); | ||
}); | ||
|
||
server.Get("/users/:id/is_allowed", [](const Request &req, Response &res) { | ||
auto user_id = req.path_params.at("id"); | ||
std::cout << "Got request for /users/:id/is_allowed" << std::endl; | ||
res.set_content("no", "text/plain"); | ||
}); | ||
|
||
std::cout << "Listening on http://" << address << ":" << port << std::endl; | ||
std::cout << "Stop URL: http://" << address << ":" << port << "/stop" | ||
<< std::endl; | ||
server.listen_after_bind(); | ||
std::cout << "Server has stopped... Bye!" << std::endl; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters