Skip to content

Commit

Permalink
sending auth header in api_client
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Apr 29, 2024
1 parent def8844 commit 2225e8e
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 8 deletions.
1 change: 1 addition & 0 deletions apache/client/include/lauth/api_client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace mlibrary::lauth {
public:
ApiClient(const std::string& url, const std::string& basicAuth) : client(std::make_unique<HttpClient>(url)), basicAuth(basicAuth) {};
ApiClient(std::unique_ptr<HttpClient>&& client) : client(std::move(client)) {};
ApiClient(std::unique_ptr<HttpClient>&& client, const std::string& basicAuth) : client(std::move(client)), basicAuth(basicAuth) {};
ApiClient(const ApiClient&) = delete;
ApiClient& operator=(const ApiClient&) = delete;
ApiClient(ApiClient&&) = delete;
Expand Down
5 changes: 4 additions & 1 deletion apache/client/src/lauth/api_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,11 @@ namespace mlibrary::lauth {
{"uri", req.uri},
{"user", req.user}
};
HttpHeaders headers {

std::string authValue = "Basic " + basicAuth;

HttpHeaders headers {
{"Authorization", authValue}
};

auto result = client->get("/authorized", params, headers);
Expand Down
18 changes: 11 additions & 7 deletions apache/client/test/lauth/api_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ TEST(ApiClient, HttpRequestByApiClientIsCorrect) {
{"user", req.user}
};

HttpHeaders headers {
{"Authorization", "Basic dGVzdA=="}
};

auto body = R"({"determination":"allowed"})";

EXPECT_CALL(*client, get("/authorized", params)).WillOnce(Return(body));
ApiClient api_client(std::move(client));
EXPECT_CALL(*client, get("/authorized", params, headers)).WillOnce(Return(body));
ApiClient api_client(std::move(client), "dGVzdA==");

api_client.authorize(req);
}
Expand All @@ -40,7 +44,7 @@ TEST(ApiClient, DeterminationAllowedReturnsTrue) {
auto client = std::make_unique<MockHttpClient>();
auto body = R"({"determination":"allowed"})";

EXPECT_CALL(*client, get(_, _)).WillOnce(Return(body));
EXPECT_CALL(*client, get(_, _, _)).WillOnce(Return(body));
ApiClient api_client(std::move(client));

auto result = api_client.authorize(Request());
Expand All @@ -51,7 +55,7 @@ TEST(ApiClient, DeterminationDeniedReturnsFalse) {
auto client = std::make_unique<MockHttpClient>();
auto body = R"({"determination":"denied"})";

EXPECT_CALL(*client, get(_, _)).WillOnce(Return(body));
EXPECT_CALL(*client, get(_, _, _)).WillOnce(Return(body));
ApiClient api_client(std::move(client));

auto result = api_client.authorize(Request());
Expand All @@ -62,7 +66,7 @@ TEST(ApiClient, MismatchedJsonReturnsFalse) {
auto client = std::make_unique<MockHttpClient>();
auto body = R"({"should_ignore_this_key":"allowed"})";

EXPECT_CALL(*client, get(_, _)).WillOnce(Return(body));
EXPECT_CALL(*client, get(_, _, _)).WillOnce(Return(body));
ApiClient api_client(std::move(client));

auto result = api_client.authorize(Request());
Expand All @@ -73,7 +77,7 @@ TEST(ApiClient, MalformedJsonReturnsFalse) {
auto client = std::make_unique<MockHttpClient>();
auto body = R"({"should_ignore_this_key":"allowed",)";

EXPECT_CALL(*client, get(_, _)).WillOnce(Return(body));
EXPECT_CALL(*client, get(_, _, _)).WillOnce(Return(body));
ApiClient api_client(std::move(client));

auto result = api_client.authorize(Request());
Expand All @@ -84,7 +88,7 @@ TEST(ApiClient, EmptyBodyReturnsFalse) {
auto client = std::make_unique<MockHttpClient>();
auto body = "";

EXPECT_CALL(*client, get(_, _)).WillOnce(Return(body));
EXPECT_CALL(*client, get(_, _, _)).WillOnce(Return(body));
ApiClient api_client(std::move(client));

auto result = api_client.authorize(Request());
Expand Down
3 changes: 3 additions & 0 deletions apache/client/test/lauth/mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "lauth/authorization_result.hpp"
#include "lauth/http_client.hpp"
#include "lauth/http_params.hpp"
#include "lauth/http_headers.hpp"

#include <optional>

Expand All @@ -15,6 +16,8 @@ class MockHttpClient : public HttpClient {
MockHttpClient() : HttpClient("http://api.invalid") {};
MOCK_METHOD(std::optional<std::string>, get, (const std::string&), (override));
MOCK_METHOD(std::optional<std::string>, get, (const std::string&, const HttpParams&), (override));
MOCK_METHOD(std::optional<std::string>, get, (const std::string&, const HttpHeaders&), (override));
MOCK_METHOD(std::optional<std::string>, get, (const std::string&, const HttpParams&, const HttpHeaders&), (override));
};

class MockApiClient : public ApiClient {
Expand Down

0 comments on commit 2225e8e

Please sign in to comment.