Skip to content

Commit

Permalink
Added unit tests for isCacheableRequest in CacheFilterUtils
Browse files Browse the repository at this point in the history
Signed-off-by: Yosry Ahmed <yosryahmed@google.com>
  • Loading branch information
yosrym93 committed Jun 15, 2020
1 parent c47f8b4 commit 83b8b5f
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 4 deletions.
4 changes: 2 additions & 2 deletions source/extensions/filters/http/cache/cache_filter_utils.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace Extensions {
namespace HttpFilters {
namespace Cache {

bool CacheFilterUtils::isCacheableRequest(Http::RequestHeaderMap& headers) {
bool CacheFilterUtils::isCacheableRequest(const Http::RequestHeaderMap& headers) {
const Http::HeaderEntry* method = headers.Method();
const Http::HeaderEntry* forwarded_proto = headers.ForwardedProto();
const Http::HeaderValues& header_values = Http::Headers::get();
Expand All @@ -18,7 +18,7 @@ bool CacheFilterUtils::isCacheableRequest(Http::RequestHeaderMap& headers) {
forwarded_proto->value() == header_values.SchemeValues.Https);
}

bool CacheFilterUtils::isCacheableResponse(Http::ResponseHeaderMap& headers) {
bool CacheFilterUtils::isCacheableResponse(const Http::ResponseHeaderMap& headers) {
const absl::string_view cache_control = headers.getCacheControlValue();
// TODO(toddmgreer): fully check for cacheability. See for example
// https://github.com/apache/incubator-pagespeed-mod/blob/master/pagespeed/kernel/http/caching_headers.h.
Expand Down
4 changes: 2 additions & 2 deletions source/extensions/filters/http/cache/cache_filter_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ namespace Cache {
class CacheFilterUtils {
public:
// Checks if a request can be served from cache
static bool isCacheableRequest(Http::RequestHeaderMap& headers);
static bool isCacheableRequest(const Http::RequestHeaderMap& headers);

// Checks if a response can be stored in cache
static bool isCacheableResponse(Http::ResponseHeaderMap& headers);
static bool isCacheableResponse(const Http::ResponseHeaderMap& headers);
};
} // namespace Cache
} // namespace HttpFilters
Expand Down
10 changes: 10 additions & 0 deletions test/extensions/filters/http/cache/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ envoy_extension_cc_test(
],
)

envoy_extension_cc_test(
name = "cache_filter_utils_test",
srcs = ["cache_filter_utils_test.cc"],
extension_name = "envoy.filters.http.cache",
deps = [
"//source/extensions/filters/http/cache:cache_filter_utils_lib",
"//test/test_common:utility_lib",
],
)

envoy_extension_cc_test(
name = "config_test",
srcs = ["config_test.cc"],
Expand Down
70 changes: 70 additions & 0 deletions test/extensions/filters/http/cache/cache_filter_utils_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include "extensions/filters/http/cache/cache_filter_utils.h"

#include "test/test_common/utility.h"

#include "gtest/gtest.h"

namespace Envoy {
namespace Extensions {
namespace HttpFilters {
namespace Cache {
namespace {

struct IsCacheableRequestParams {
Http::TestRequestHeaderMapImpl request_headers;
bool is_cacheable;
};

IsCacheableRequestParams params[] = {
{
{},
false
},
{
{{":path", "/"}},
false
},
{
{{":path", "/"}, {":method", "GET"}},
false
},
{
{{":path", "/"}, {":method", "GET"}, {"x-forwarded-proto", "https"}},
false
},
{
{{":path", "/"}, {":method", "GET"}, {"x-forwarded-proto", "https"}, {":authority", "test"}},
true
},
{
{{":path", "/"}, {":method", "POST"}, {"x-forwarded-proto", "https"}, {":authority", "test"}},
false
},
{
{{":path", "/"}, {":method", "GET"}, {"x-forwarded-proto", "http"}, {":authority", "test"}},
true
},
{
{{":path", "/"}, {":method", "GET"}, {"x-forwarded-proto", "http"}, {":authority", "test"}},
true
},
{
{{":path", "/"}, {":method", "GET"}, {"x-forwarded-proto", "ftp"}, {":authority", "test"}},
false
},
};

class IsCacheableRequestTest : public testing::TestWithParam<IsCacheableRequestParams> {};

INSTANTIATE_TEST_SUITE_P(IsCacheableRequestTest, IsCacheableRequestTest, testing::ValuesIn(params));

TEST_P(IsCacheableRequestTest, IsCacheableRequestTest) {
EXPECT_EQ(CacheFilterUtils::isCacheableRequest(GetParam().request_headers), GetParam().is_cacheable);
}


} // namespace
} // namespace Cache
} // namespace HttpFilters
} // namespace Extensions
} // namespace Envoy

0 comments on commit 83b8b5f

Please sign in to comment.