Skip to content

Commit

Permalink
Add ext_proc integration tests for STREAMED body immediate responses.
Browse files Browse the repository at this point in the history
Signed-off-by: Martijn Stevenson <mstevenson@google.com>
  • Loading branch information
martijneken committed Feb 21, 2025
1 parent 652f256 commit 33f71c0
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions test/extensions/filters/http/ext_proc/ext_proc_integration_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include "absl/strings/str_cat.h"
#include "gtest/gtest.h"
#include <string_view>
#include "ocpdiag/core/testing/status_matchers.h"

namespace Envoy {
Expand Down Expand Up @@ -2216,6 +2217,65 @@ TEST_P(ExtProcIntegrationTest, GetAndRespondImmediatelyOnResponse) {
EXPECT_EQ("{\"reason\": \"Not authorized\"}", response->body());
}

// Test immediate_response behavior with STREAMED request body. Even though the
// headers have been processed, an immediate response on a request body chunk
// should still be seen by the downstream.
TEST_P(ExtProcIntegrationTest, GetAndRespondImmediatelyOnStreamedRequestBody) {
proto_config_.mutable_processing_mode()->set_request_body_mode(ProcessingMode::STREAMED);
initializeConfig();
HttpIntegrationTest::initialize();
auto response = sendDownstreamRequestWithBody("Evil content!", absl::nullopt);
processRequestHeadersMessage(
*grpc_upstreams_[0], true, [](const HttpHeaders&, HeadersResponse& resp) {
auto* hdr = resp.mutable_response()->mutable_header_mutation()->add_set_headers();
hdr->mutable_header()->set_key("foo");
hdr->mutable_header()->set_raw_value("bar");
return true;
});
processAndRespondImmediately(
*grpc_upstreams_[0], false, [](ImmediateResponse& immediate) {
immediate.mutable_status()->set_code(envoy::type::v3::StatusCode::BadRequest);
immediate.set_body("{\"reason\": \"Request too evil\"}");
immediate.set_details("Failed because I don't like this payload");
});
verifyDownstreamResponse(*response, 400);
EXPECT_EQ("{\"reason\": \"Request too evil\"}", response->body());
// The previously added request header is not sent to the client.
EXPECT_THAT(response->headers(), HasNoHeader("foo"));
}

// Test immediate_response behavior with STREAMED response body.
//
// In this test the client sees the immediate response, but that may not always
// be the case: "If a response has already started -- for example, if this
// message is sent response to a ``response_body`` message -- then this will
// either ship the reply directly to the downstream codec, or reset the stream."
TEST_P(ExtProcIntegrationTest, GetAndRespondImmediatelyOnStreamedResponseBody) {
proto_config_.mutable_processing_mode()->set_response_body_mode(ProcessingMode::BUFFERED);
initializeConfig();
HttpIntegrationTest::initialize();
auto response = sendDownstreamRequest(absl::nullopt);
processRequestHeadersMessage(*grpc_upstreams_[0], true, absl::nullopt);
handleUpstreamRequest();
processResponseHeadersMessage(
*grpc_upstreams_[0], false, [](const HttpHeaders&, HeadersResponse& resp) {
auto* hdr = resp.mutable_response()->mutable_header_mutation()->add_set_headers();
hdr->mutable_header()->set_key("foo");
hdr->mutable_header()->set_raw_value("bar");
return true;
});
processAndRespondImmediately(
*grpc_upstreams_[0], false, [](ImmediateResponse& immediate) {
immediate.mutable_status()->set_code(envoy::type::v3::StatusCode::BadRequest);
immediate.set_body("{\"reason\": \"Response too evil\"}");
immediate.set_details("Failed because I don't like this payload");
});
verifyDownstreamResponse(*response, 400);
EXPECT_EQ("{\"reason\": \"Response too evil\"}", response->body());
// The previously added response header is not sent to the client.
EXPECT_THAT(response->headers(), HasNoHeader("foo"));
}

// Test the filter with request body buffering enabled using
// an ext_proc server that responds to the request_body message
// by sending back an immediate_response message
Expand Down

0 comments on commit 33f71c0

Please sign in to comment.