Skip to content

Commit

Permalink
Merge pull request #437 from jupp0r/improve-code-coverage-for-pull
Browse files Browse the repository at this point in the history
Improve code coverage for pull
  • Loading branch information
gjasny committed Dec 27, 2020
2 parents 928a347 + b8db5e4 commit 85e4aff
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 1 deletion.
11 changes: 10 additions & 1 deletion pull/src/endpoint.cc
Expand Up @@ -7,6 +7,14 @@
namespace prometheus {
namespace detail {

namespace {
class AlwaysAllowAccessHandler : public CivetAuthHandler {
bool authorize(CivetServer*, struct mg_connection*) override { return true; }
};

AlwaysAllowAccessHandler alwaysAllowAccessHandler;
} // namespace

Endpoint::Endpoint(CivetServer& server, std::string uri)
: server_(server),
uri_(std::move(uri)),
Expand All @@ -21,7 +29,8 @@ Endpoint::~Endpoint() {
server_.removeHandler(uri_);
if (auth_handler_) {
// work-around https://github.com/civetweb/civetweb/issues/941
server_.removeAuthHandler(uri_);
// server_.removeAuthHandler(uri_);
server_.addAuthHandler(uri_, alwaysAllowAccessHandler);
}
}

Expand Down
63 changes: 63 additions & 0 deletions pull/tests/integration/integration_test.cc
@@ -1,6 +1,7 @@
#include <curl/curl.h>
#include <gmock/gmock.h>

#include <functional>
#include <memory>
#include <string>

Expand All @@ -27,6 +28,8 @@ class IntegrationTest : public testing::Test {
std::string body;
};

std::function<void(CURL*)> fetchPrePerform_;

Resonse FetchMetrics(std::string metrics_path) {
auto curl = std::shared_ptr<CURL>(curl_easy_init(), curl_easy_cleanup);
if (!curl) {
Expand All @@ -40,6 +43,10 @@ class IntegrationTest : public testing::Test {
curl_easy_setopt(curl.get(), CURLOPT_WRITEDATA, &response.body);
curl_easy_setopt(curl.get(), CURLOPT_WRITEFUNCTION, WriteCallback);

if (fetchPrePerform_) {
fetchPrePerform_(curl.get());
}

CURLcode curl_error = curl_easy_perform(curl.get());
if (curl_error != CURLE_OK) {
throw std::runtime_error("failed to perform HTTP request");
Expand Down Expand Up @@ -134,5 +141,61 @@ TEST_F(IntegrationTest, unexposeRegistry) {
EXPECT_THAT(metrics.body, Not(HasSubstr(counter_name)));
}

TEST_F(IntegrationTest, acceptOptionalCompression) {
const std::string counter_name = "example_total";
auto registry = RegisterSomeCounter(counter_name, default_metrics_path_);

fetchPrePerform_ = [](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_ACCEPT_ENCODING, "");
};
const auto metrics = FetchMetrics(default_metrics_path_);

ASSERT_EQ(metrics.code, 200);
EXPECT_THAT(metrics.body, HasSubstr(counter_name));
}

#if 0 // https://github.com/civetweb/civetweb/issues/954
TEST_F(IntegrationTest, shouldRejectRequestWithoutAuthorization) {
const std::string counter_name = "example_total";
auto registry = RegisterSomeCounter(counter_name, default_metrics_path_);

exposer_->RegisterAuth(
[](const std::string& user, const std::string& password) {
return user == "test_user" && password == "test_password";
},
"Some Auth Realm", default_metrics_path_);

const auto metrics = FetchMetrics(default_metrics_path_);

ASSERT_EQ(metrics.code, 401);
}
#endif

TEST_F(IntegrationTest, shouldPerformProperAuthentication) {
const std::string counter_name = "example_total";
auto registry = RegisterSomeCounter(counter_name, default_metrics_path_);

const auto my_username = "test_user";
const auto my_password = "test_password";

fetchPrePerform_ = [my_username, my_password](CURL* curl) {
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERNAME, my_username);
curl_easy_setopt(curl, CURLOPT_PASSWORD, my_password);
};

exposer_->RegisterAuth(
[my_username, my_password](const std::string& user, const std::string& password) {
return user == my_username && password == my_password;
},
"Some Auth Realm", default_metrics_path_);

const auto metrics = FetchMetrics(default_metrics_path_);

ASSERT_EQ(metrics.code, 200);
EXPECT_THAT(metrics.body, HasSubstr(counter_name));
}


} // namespace
} // namespace prometheus

0 comments on commit 85e4aff

Please sign in to comment.