Skip to content
This repository was archived by the owner on Apr 24, 2025. It is now read-only.

Commit f23f536

Browse files
author
Wim Spaargaren
committed
chore: add e2e test for setting an http request body
1 parent d01c7e7 commit f23f536

File tree

6 files changed

+46
-19
lines changed

6 files changed

+46
-19
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ test:
1919
test.e2e:
2020
docker run -it -w /tmp/proxy-wasm-go -v $(shell pwd):/tmp/proxy-wasm-go getenvoy/proxy-wasm-go-sdk-ci:istio-${ISTIO_VERSION} go test -v ./e2e
2121

22+
test.e2e.single:
23+
docker run -it -w /tmp/proxy-wasm-go -v $(shell pwd):/tmp/proxy-wasm-go getenvoy/proxy-wasm-go-sdk-ci:istio-${ISTIO_VERSION} go test -v ./e2e -run ${name}
24+
2225
run:
2326
docker run --entrypoint='/usr/local/bin/envoy' \
2427
-p 18000:18000 -p 8099:8099 \

e2e/e2e_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,28 @@ func TestE2E_http_headers(t *testing.T) {
117117
assert.True(t, strings.Contains(out, "server: envoy"))
118118
}
119119

120+
func TestE2E_http_body(t *testing.T) {
121+
cmd, stdErr := startExample(t, "http_body")
122+
defer func() {
123+
require.NoError(t, cmd.Process.Kill())
124+
}()
125+
126+
req, err := http.NewRequest("GET", envoyEndpoint, bytes.NewBuffer([]byte(`{ "example": "body" }`)))
127+
require.NoError(t, err)
128+
129+
r, err := http.DefaultClient.Do(req)
130+
require.NoError(t, err)
131+
defer r.Body.Close()
132+
133+
out := stdErr.String()
134+
fmt.Println(out)
135+
assert.True(t, strings.Contains(out, "body size: 21"))
136+
assert.True(t, strings.Contains(out, `initial request body: { "example": "body" }`))
137+
assert.True(t, strings.Contains(out, "on http request body finished"))
138+
assert.False(t, strings.Contains(out, "failed to set request body"))
139+
assert.False(t, strings.Contains(out, "failed to get request body"))
140+
}
141+
120142
func TestE2E_network(t *testing.T) {
121143
cmd, stdErr := startExample(t, "network")
122144
defer func() {

examples/http_body/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## http_headers
1+
## http_body
22

33
this example replaces the request body
44

examples/http_body/envoy.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ static_resources:
3333
runtime: "envoy.wasm.runtime.v8"
3434
code:
3535
local:
36-
filename: "./examples/http_headers/main.go.wasm"
36+
filename: "./examples/http_body/main.go.wasm"
3737
allow_precompiled: true
3838
- name: envoy.router
3939
config: {}

examples/http_body/main.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,24 @@ func newContext(rootContextID, contextID uint32) proxywasm.HttpContext {
3636
// override
3737
func (ctx *httpBody) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
3838
proxywasm.LogInfof("body size: %d", bodySize)
39-
40-
b, err := proxywasm.GetHttpRequestBody(0, bodySize)
41-
if err != nil {
42-
proxywasm.LogErrorf("failed to get request body: %v", err)
43-
return types.ActionContinue
39+
if bodySize != 0 {
40+
initialBody, err := proxywasm.GetHttpRequestBody(0, bodySize)
41+
if err != nil {
42+
proxywasm.LogErrorf("failed to get request body: %v", err)
43+
return types.ActionContinue
44+
}
45+
proxywasm.LogInfof("initial request body: %s", string(initialBody))
46+
47+
b := []byte(`{ "another": "body" }`)
48+
49+
err = proxywasm.SetHttpRequestBody(b)
50+
if err != nil {
51+
proxywasm.LogErrorf("failed to set request body: %v", err)
52+
return types.ActionContinue
53+
}
54+
55+
proxywasm.LogInfof("on http request body finished")
4456
}
4557

46-
proxywasm.LogInfof("initial request body: %s", string(b))
47-
48-
err = proxywasm.SetHttpRequestBody([]byte(`{"test":"data"}`))
49-
if err != nil {
50-
proxywasm.LogErrorf("failed to get request body: %v", err)
51-
return types.ActionContinue
52-
}
53-
54-
proxywasm.LogInfof("on http request body finished")
55-
5658
return types.ActionContinue
5759
}

examples/http_body/main_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
1010
)
1111

12-
func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
12+
func TestHttpBody_OnHttpRequestBody(t *testing.T) {
1313
opt := proxytest.NewEmulatorOption().
1414
WithNewHttpContext(newContext)
1515
host := proxytest.NewHostEmulator(opt)
@@ -19,7 +19,7 @@ func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
1919
host.HttpFilterPutRequestBody(id, []byte(`{ "initial": "request body" }`))
2020

2121
res := host.HttpFilterGetRequestBody(id)
22-
assert.Equal(t, `{"test":"data"}`, string(res))
22+
assert.Equal(t, `{ "another": "body" }`, string(res))
2323

2424
logs := host.GetLogs(types.LogLevelInfo)
2525
require.Greater(t, len(logs), 1)

0 commit comments

Comments
 (0)