77#include " gtest/gtest.h"
88
99namespace Nighthawk {
10+ namespace {
1011
11- using namespace testing ;
12+ using ::testing::HasSubstr;
13+
14+ enum TestRequestMethod { GET, POST };
15+
16+ const std::string kBadConfigErrorSentinel =
17+ " didn't understand the request: Error merging json config: Unable to parse "
18+ " JSON as proto (INVALID_ARGUMENT:Unexpected" ;
1219
1320class HttpFilterBaseIntegrationTest
1421 : public HttpFilterIntegrationTestBase,
1522 public testing::TestWithParam<
16- std::tuple<Envoy::Network::Address::IpVersion, absl::string_view, bool >> {
23+ std::tuple<Envoy::Network::Address::IpVersion, absl::string_view, TestRequestMethod >> {
1724public:
18- HttpFilterBaseIntegrationTest () : HttpFilterIntegrationTestBase(std::get<0 >(GetParam())){};
25+ HttpFilterBaseIntegrationTest ()
26+ : HttpFilterIntegrationTestBase(std::get<0 >(GetParam())), config_(std::get<1 >(GetParam())) {
27+ initializeFilterConfiguration (config_);
28+ if (std::get<2 >(GetParam ()) == TestRequestMethod::POST) {
29+ switchToPostWithEntityBody ();
30+ }
31+ };
32+
33+ ResponseOrigin getHappyFlowResponseOrigin () {
34+ // Modulo the test-server, extensions are expected to need an upstream to synthesize a reply
35+ // when the effective configuration is valid.
36+ return config_.find_first_of (" name: test-server" ) == 0 ? ResponseOrigin::EXTENSION
37+ : ResponseOrigin::UPSTREAM;
38+ }
39+
40+ protected:
41+ const std::string config_;
1942};
2043
2144INSTANTIATE_TEST_SUITE_P (
@@ -34,56 +57,38 @@ name: dynamic-delay
3457 static_delay: 0.1s
3558)EOF" ),
3659 absl::string_view (" name: test-server" )}),
37- testing::ValuesIn({true , false })));
60+ testing::ValuesIn({TestRequestMethod::GET, TestRequestMethod::POST })));
3861
39- // Verify extensions are well-behaved when it comes to:
40- // - GET requests.
41- // - POST requests with an entity body (takes a slightly different code path).
42- // - Valid but empty configuration.
43- // - Bad request-level json configuration.
44- // We will be low on functional expectations for the extensions, but this will catch hangs and
45- // ensure that bad configuration handling is in-place.
46- TEST_P (HttpFilterBaseIntegrationTest, BasicExtensionFlows) {
47- absl::string_view config = std::get<1 >(GetParam ());
48- initializeFilterConfiguration (std::string (config));
49- bool is_post = std::get<2 >(GetParam ());
50- if (is_post) {
51- switchToPostWithEntityBody ();
52- }
53-
54- // Modulo the test-server, extensions are expected to need an upstream to synthesize a reply
55- // when effective configuration is valid.
56- ResponseOrigin happy_flow_response_origin = config.find_first_of (" name: test-server" ) == 0
57- ? ResponseOrigin::EXTENSION
58- : ResponseOrigin::UPSTREAM;
59- // Post without any request-level configuration. Should succeed.
60- Envoy::IntegrationStreamDecoderPtr response = getResponse (happy_flow_response_origin);
62+ TEST_P (HttpFilterBaseIntegrationTest, NoRequestLevelConfigurationShouldSucceed) {
63+ Envoy::IntegrationStreamDecoderPtr response = getResponse (getHappyFlowResponseOrigin ());
6164 ASSERT_TRUE (response->complete ());
6265 EXPECT_EQ (" 200" , response->headers ().Status ()->value ().getStringView ());
6366 EXPECT_TRUE (response->body ().empty ());
67+ }
6468
65- // Test with a valid but empty request-level configuration.
69+ TEST_P (HttpFilterBaseIntegrationTest, EmptyJsonRequestLevelConfigurationShouldSucceed) {
6670 setRequestLevelConfiguration (" {}" );
67- response = getResponse (happy_flow_response_origin );
71+ Envoy::IntegrationStreamDecoderPtr response = getResponse (getHappyFlowResponseOrigin () );
6872 ASSERT_TRUE (response->complete ());
6973 EXPECT_EQ (" 200" , response->headers ().Status ()->value ().getStringView ());
7074 EXPECT_TRUE (response->body ().empty ());
75+ }
7176
72- const std::string kBadConfigErrorSentinel =
73- " didn't understand the request: Error merging json config: Unable to parse "
74- " JSON as proto (INVALID_ARGUMENT:Unexpected" ;
75-
77+ TEST_P (HttpFilterBaseIntegrationTest, BadJsonAsRequestLevelConfigurationShouldFail) {
7678 // When sending bad request-level configuration, the extension ought to reply directly.
7779 setRequestLevelConfiguration (" bad_json" );
78- response = getResponse (ResponseOrigin::EXTENSION);
80+ Envoy::IntegrationStreamDecoderPtr response = getResponse (ResponseOrigin::EXTENSION);
7981 EXPECT_EQ (Envoy::Http::Utility::getResponseStatus (response->headers ()), 500 );
8082 EXPECT_THAT (response->body (), HasSubstr (kBadConfigErrorSentinel ));
83+ }
8184
85+ TEST_P (HttpFilterBaseIntegrationTest, EmptyRequestLevelConfigurationShouldFail) {
8286 // When sending empty request-level configuration, the extension ought to reply directly.
8387 setRequestLevelConfiguration (" " );
84- response = getResponse (ResponseOrigin::EXTENSION);
88+ Envoy::IntegrationStreamDecoderPtr response = getResponse (ResponseOrigin::EXTENSION);
8589 EXPECT_EQ (Envoy::Http::Utility::getResponseStatus (response->headers ()), 500 );
8690 EXPECT_THAT (response->body (), HasSubstr (kBadConfigErrorSentinel ));
8791}
8892
93+ } // namespace
8994} // namespace Nighthawk
0 commit comments