Skip to content

Commit

Permalink
uri_template: supporting operator<< (envoyproxy#30923)
Browse files Browse the repository at this point in the history
Signed-off-by: Adi Suissa-Peleg <adip@google.com>
  • Loading branch information
adisuissa authored Nov 21, 2023
1 parent 034ba0b commit 9e644b4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
14 changes: 14 additions & 0 deletions source/extensions/path/uri_template_lib/uri_template.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "source/extensions/path/uri_template_lib/uri_template_internal.h"

#include "absl/status/statusor.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_split.h"
#include "absl/strings/string_view.h"
#include "re2/re2.h"
Expand All @@ -24,6 +25,19 @@ using Internal::ParsedPathPattern;
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif

std::ostream& operator<<(std::ostream& os, const ParsedSegment& parsed_segment) {
os << "{kind = ";
switch (parsed_segment.kind_) {
case RewriteStringKind::Variable:
os << "Variable";
break;
case RewriteStringKind::Literal:
os << "Literal";
break;
}
return os << ", value = \"" << absl::CEscape(parsed_segment.value_) << "\"}";
}

absl::StatusOr<std::string> convertPathPatternSyntaxToRegex(absl::string_view path_pattern) {
absl::StatusOr<ParsedPathPattern> status = Internal::parsePathPatternSyntax(path_pattern);
if (!status.ok()) {
Expand Down
3 changes: 3 additions & 0 deletions source/extensions/path/uri_template_lib/uri_template.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <ostream>
#include <string>

#include "source/extensions/path/uri_template_lib/uri_template_internal.h"
Expand All @@ -26,6 +27,8 @@ struct ParsedSegment {
ParsedSegment(absl::string_view value, RewriteStringKind kind) : value_(value), kind_(kind) {}
absl::string_view value_;
RewriteStringKind kind_;

friend std::ostream& operator<<(std::ostream& os, const ParsedSegment& parsed_segment);
};

// Stores string literals and regex capture indexes for rewriting paths
Expand Down
13 changes: 12 additions & 1 deletion test/extensions/path/uri_template_lib/uri_template_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace {

using ::Envoy::StatusHelpers::IsOkAndHolds;
using ::Envoy::StatusHelpers::StatusIs;
using testing::HasSubstr;

// Capture regex for /{var1}/{var2}/{var3}/{var4}/{var5}
static constexpr absl::string_view kCaptureRegex = "/(?P<var1>[a-zA-Z0-9-._~%!$&'()+,;:@]+)/"
Expand Down Expand Up @@ -67,7 +68,17 @@ TEST_P(ParseRewriteHelperSuccess, ParseRewriteHelperSuccessTest) {
std::string pattern = GetParam();
SCOPED_TRACE(pattern);

EXPECT_OK(parseRewritePattern(pattern));
const auto result = parseRewritePattern(pattern);
EXPECT_OK(result);

// The following is to exercise operator<< in ParseSegments.
const auto& parsed_segments_vec = result.value();
std::stringstream all_segments_str;
for (const auto& parsed_segment : parsed_segments_vec) {
all_segments_str << parsed_segment;
}
EXPECT_THAT(all_segments_str.str(), HasSubstr("kind = Literal, value ="));
EXPECT_THAT(all_segments_str.str(), HasSubstr("kind = Variable, value ="));
}

class ParseRewriteHelperFailure : public testing::TestWithParam<std::string> {};
Expand Down

0 comments on commit 9e644b4

Please sign in to comment.