Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #652
- Loading branch information
Showing
14 changed files
with
179 additions
and
5 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
File renamed without changes.
File renamed without changes.
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 "label_encoder.h" | ||
|
||
#include <algorithm> | ||
#include <ostream> | ||
|
||
#include "prometheus/detail/base64.h" | ||
|
||
namespace prometheus { | ||
namespace detail { | ||
|
||
namespace { | ||
// Does this character need encoding like in RFC 3986 section 2.3? | ||
bool needsEncoding(char c) { | ||
if (c >= 'a' && c <= 'z') { | ||
return false; | ||
} | ||
if (c >= 'A' && c <= 'Z') { | ||
return false; | ||
} | ||
if (c >= '0' && c <= '9') { | ||
return false; | ||
} | ||
if (c == '-' || c == '.' || c == '_' || c == '~') { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} // namespace | ||
|
||
void encodeLabel(std::ostream& os, const Label& label) { | ||
if (label.second.empty()) { | ||
os << "/" << label.first << "@base64/="; | ||
} else if (std::any_of(label.second.begin(), label.second.end(), | ||
needsEncoding)) { | ||
os << "/" << label.first << "@base64/" | ||
<< detail::base64url_encode(label.second); | ||
} else { | ||
os << "/" << label.first << "/" << label.second; | ||
} | ||
} | ||
} // namespace detail | ||
} // namespace prometheus |
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,13 @@ | ||
#pragma once | ||
|
||
#include <iosfwd> | ||
|
||
#include "prometheus/labels.h" | ||
|
||
namespace prometheus { | ||
namespace detail { | ||
|
||
void encodeLabel(std::ostream& os, const Label& label); | ||
|
||
} | ||
} // namespace prometheus |
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 +1,2 @@ | ||
add_subdirectory(integration) | ||
add_subdirectory(internal) |
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,13 @@ | ||
cc_test( | ||
name = "internal", | ||
srcs = glob([ | ||
"*.cc", | ||
"*.h", | ||
]), | ||
copts = ["-Iexternal/googletest/include"], | ||
linkstatic = True, | ||
deps = [ | ||
"//push:push_internal_headers", | ||
"@com_google_googletest//:gtest_main", | ||
], | ||
) |
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 @@ | ||
add_executable(prometheus_push_internal_test | ||
label_encoder_test.cc | ||
) | ||
|
||
target_link_libraries(prometheus_push_internal_test | ||
PRIVATE | ||
${PROJECT_NAME}::push_internal_headers | ||
GTest::gmock_main | ||
) | ||
|
||
add_test( | ||
NAME prometheus_push_internal_test | ||
COMMAND prometheus_push_internal_test | ||
) |
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,43 @@ | ||
#include "detail/label_encoder.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <string> | ||
#include <sstream> | ||
|
||
namespace prometheus { | ||
namespace { | ||
|
||
class LabelEncoderTest : public testing::Test { | ||
protected: | ||
std::string Encode(const Label& label) { | ||
std::stringstream ss; | ||
detail::encodeLabel(ss, label); | ||
return ss.str(); | ||
} | ||
}; | ||
|
||
// test cases taken from https://github.com/prometheus/pushgateway#url | ||
|
||
TEST_F(LabelEncoderTest, regular) { | ||
EXPECT_EQ("/foo/bar", Encode(Label{"foo", "bar"})); | ||
} | ||
|
||
TEST_F(LabelEncoderTest, empty) { | ||
EXPECT_EQ("/first_label@base64/=", Encode(Label{"first_label", ""})); | ||
} | ||
|
||
TEST_F(LabelEncoderTest, path) { | ||
EXPECT_EQ("/path@base64/L3Zhci90bXA=", Encode(Label{"path", "/var/tmp"})); | ||
} | ||
|
||
TEST_F(LabelEncoderTest, unicode) { | ||
const char unicodeText[] = | ||
"\xce\xa0\xcf\x81\xce\xbf\xce\xbc\xce\xb7\xce\xb8\xce\xb5\xcf\x8d\xcf" | ||
"\x82"; // Προμηθεύς | ||
EXPECT_EQ("/name@base64/zqDPgc6_zrzOt864zrXPjc-C", | ||
Encode(Label{"name", unicodeText})); | ||
} | ||
|
||
} // namespace | ||
} // namespace prometheus |
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