Skip to content

Commit

Permalink
feat: Add body chunking support to test framework (GoogleCloudPlatfor…
Browse files Browse the repository at this point in the history
…m#130)


* Added automatic body chunking support

* Added chunking for response path and result matching with file

* small fix

* small license fix

* Resolve comments and improve readability

* Fix return type error when no body invocations were specified.

* resolve comments

* Move unnamed namespace to top of file
  • Loading branch information
dallen68 authored Dec 9, 2024
1 parent fbc388c commit 8e6f41d
Show file tree
Hide file tree
Showing 9 changed files with 327 additions and 64 deletions.
25 changes: 25 additions & 0 deletions plugins/samples/body_chunking/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
load("//:plugins.bzl", "proxy_wasm_plugin_cpp", "proxy_wasm_plugin_rust", "proxy_wasm_tests")

licenses(["notice"]) # Apache 2


proxy_wasm_plugin_cpp(
name = "plugin_cpp.wasm",
srcs = ["plugin.cc"],
deps = [
"@proxy_wasm_cpp_sdk//contrib:contrib_lib",
],
)

proxy_wasm_tests(
name = "tests",
data = [
":request_body.data",
":response_body.data",
":expected_request_body.data",
],
plugins = [
":plugin_cpp.wasm",
],
tests = ":tests.textpb",
)
1 change: 1 addition & 0 deletions plugins/samples/body_chunking/expected_request_body.data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
12foo34foo56foo78foo90foo
39 changes: 39 additions & 0 deletions plugins/samples/body_chunking/plugin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2024 Google LLC
//
// Licensed 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.

// [START serviceextensions_plugin_body_chunking]
#include "proxy_wasm_intrinsics.h"

class MyHttpContext : public Context {
public:
explicit MyHttpContext(uint32_t id, RootContext* root) : Context(id, root) {}

// Add foo onto the end of each request body chunk
FilterDataStatus onRequestBody(size_t chunk_len,
bool end_of_stream) override {
setBuffer(WasmBufferType::HttpRequestBody, chunk_len, 0, "foo");
return FilterDataStatus::Continue;
}

// Add bar onto the end of each response body chunk
FilterDataStatus onResponseBody(size_t chunk_len,
bool end_of_stream) override {
setBuffer(WasmBufferType::HttpResponseBody, chunk_len, 0, "bar");
return FilterDataStatus::Continue;
}
};

static RegisterContextFactory register_StaticContext(
CONTEXT_FACTORY(MyHttpContext), ROOT_FACTORY(RootContext));
// [END serviceextensions_plugin_body_chunking]
1 change: 1 addition & 0 deletions plugins/samples/body_chunking/request_body.data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1234567890
1 change: 1 addition & 0 deletions plugins/samples/body_chunking/response_body.data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0987654321
78 changes: 78 additions & 0 deletions plugins/samples/body_chunking/tests.textpb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
test {
name: "NumChunkWithFileInput"
num_chunks: 5
request_body {
input { file: "request_body.data" }
result { body { exact: "12foo34foo56foo78foo90foo" } }
}
response_body {
input { file: "response_body.data" }
result { body { exact: "09bar87bar65bar43bar21bar" } }
}
}
test {
name: "NumChunkWithFileInputFileOutput"
num_chunks: 5
request_body {
input { file: "request_body.data" }
result { body { file: "expected_request_body.data" } }
}
}
test {
name: "NumChunkWithContentInput"
num_chunks: 5
request_body {
input { content: "1234567890" }
result { body { exact: "12foo34foo56foo78foo90foo" } }
}
}
test {
name: "ChunkSizeWithFileInput"
chunk_size: 2
request_body {
input { file: "request_body.data" }
result { body { exact: "12foo34foo56foo78foo90foo" } }
}
}
test {
name: "ChunkSizeWithContentInput"
chunk_size: 2
request_body {
input { content: "1234567890" }
result { body { exact: "12foo34foo56foo78foo90foo" } }
}
}

test {
name: "NoChunking"
request_body{
input {
content: "12"
}
result {
body {
exact: "12foo"
}
}
}
request_body{
input {
content: "34"
}
result {
body {
exact: "34foo"
}
}
}
request_body{
input {
content: "56"
}
result {
body {
exact: "56foo"
}
}
}
}
Loading

0 comments on commit 8e6f41d

Please sign in to comment.