Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions plugins/header_rewrite/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ if(BUILD_TESTING)
if(maxminddb_FOUND)
target_link_libraries(test_header_rewrite PRIVATE maxminddb::maxminddb)
endif()

add_executable(test_matcher matcher_tests.cc matcher.cc lulu.cc regex_helper.cc resources.cc)
add_test(NAME test_matcher COMMAND $<TARGET_FILE:test_matcher>)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to land #12453 so we don't conflict with these.


target_link_libraries(test_matcher PRIVATE catch2::catch2 ts::tscore libswoc::libswoc PCRE::PCRE)

endif()
verify_global_plugin(header_rewrite)
verify_remap_plugin(header_rewrite)
6 changes: 0 additions & 6 deletions plugins/header_rewrite/header_rewrite.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,6 @@
// Debugs
namespace header_rewrite_ns
{
const char PLUGIN_NAME[] = "header_rewrite";
const char PLUGIN_NAME_DBG[] = "dbg_header_rewrite";

DbgCtl dbg_ctl{PLUGIN_NAME_DBG};
DbgCtl pi_dbg_ctl{PLUGIN_NAME};

std::once_flag initHRWLibs;
PluginFactory plugin_factory;

Expand Down
10 changes: 10 additions & 0 deletions plugins/header_rewrite/matcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,21 @@
limitations under the License.
*/

#include "tsutil/DbgCtl.h"
#include <string>
#include <algorithm>

#include "matcher.h"

namespace header_rewrite_ns
{
const char PLUGIN_NAME[] = "header_rewrite";
const char PLUGIN_NAME_DBG[] = "dbg_header_rewrite";

DbgCtl dbg_ctl{PLUGIN_NAME_DBG};
DbgCtl pi_dbg_ctl{PLUGIN_NAME};
} // namespace header_rewrite_ns

static bool
match_with_modifiers(std::string_view rhs, std::string_view lhs, CondModifiers mods)
{
Expand Down
8 changes: 8 additions & 0 deletions plugins/header_rewrite/matcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -380,3 +380,11 @@ template <class T> class Matchers : public Matcher
std::variant<T, std::set<T>, swoc::IPRangeSet, regexHelper> _data;
CondModifiers _mods = CondModifiers::NONE;
};

// forward declare spcializations implemented in matcher.cc

template <> bool Matchers<std::string>::test_eq(const std::string &) const;

template <> bool Matchers<std::string>::test_set(const std::string &) const;

template <> bool Matchers<const sockaddr *>::test(const sockaddr *const &, const Resources &) const;
109 changes: 109 additions & 0 deletions plugins/header_rewrite/matcher_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
@file Test for matcher.cc

@section license License

Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include "matcher.h"
#include "ts/apidefs.h"
#define CATCH_CONFIG_MAIN
#include "catch.hpp"

int
_TSAssert(const char *, const char *, int)
{
return 0;
}

void
TSError(const char *fmt, ...)
{
va_list args;

va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
fprintf(stderr, "\n");
}

TSHttpStatus
TSHttpHdrStatusGet(TSMBuffer, TSMLoc)
{
return TS_HTTP_STATUS_OK;
}

TSReturnCode
TSHandleMLocRelease(TSMBuffer, TSMLoc, TSMLoc)
{
return TS_SUCCESS;
}

const char *
TSHttpHookNameLookup(TSHttpHookID)
{
return nullptr;
}

TSReturnCode
TSHttpTxnClientReqGet(TSHttpTxn, TSMBuffer *, TSMLoc *)
{
return TS_SUCCESS;
}

TSReturnCode
TSHttpTxnServerReqGet(TSHttpTxn, TSMBuffer *, TSMLoc *)
{
return TS_SUCCESS;
}

TSReturnCode
TSHttpTxnClientRespGet(TSHttpTxn, TSMBuffer *, TSMLoc *)
{
return TS_SUCCESS;
}

TSReturnCode
TSHttpTxnServerRespGet(TSHttpTxn, TSMBuffer *, TSMLoc *)
{
return TS_SUCCESS;
}

ClassAllocator<ProxyMutex> mutexAllocator("mutexAllocator");

TEST_CASE("Matcher", "[plugins][header_rewrite]")
{
Matchers<std::string> foo(MATCH_EQUAL);
TSHttpTxn txn = nullptr;
TSCont c = nullptr;
Resources res(txn, c);

foo.set("FOO", CondModifiers::MOD_NOCASE);
REQUIRE(foo.test("foo", res) == true);
}

TEST_CASE("MatcherSet", "[plugins][header_rewrite]")
{
Matchers<std::string> foo(MATCH_SET);
TSHttpTxn txn = nullptr;
TSCont c = nullptr;
Resources res(txn, c);

foo.set("foo, bar, baz", CondModifiers::MOD_NOCASE);
REQUIRE(foo.test("FOO", res) == true);
}