forked from tetratelabs/proxy-wasm-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathhttp_test.go
151 lines (124 loc) · 3.9 KB
/
http_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package proxytest
import (
"fmt"
"testing"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/internal"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types"
"github.com/stretchr/testify/require"
)
type testPlugin struct {
types.DefaultVMContext
buffered bool
}
type testPluginContext struct {
types.DefaultPluginContext
buffered bool
}
type testHttpContext struct {
types.DefaultHttpContext
buffered bool
}
// NewPluginContext implements the same method on types.VMContext.
func (p *testPlugin) NewPluginContext(uint32) types.PluginContext {
return &testPluginContext{buffered: p.buffered}
}
// NewPluginContext implements the same method on types.PluginContext.
func (p *testPluginContext) NewHttpContext(uint32) types.HttpContext {
return &testHttpContext{buffered: p.buffered}
}
// OnHttpRequestBody implements the same method on types.HttpContext.
func (h *testHttpContext) OnHttpRequestBody(bodySize int, endOfStream bool) types.Action {
if !endOfStream {
if h.buffered {
return types.ActionPause
} else {
return types.ActionContinue
}
}
body, err := proxywasm.GetHttpRequestBody(0, bodySize)
if err != nil {
panic(err)
}
proxywasm.LogInfo(fmt.Sprintf("request body:%s", string(body)))
return types.ActionContinue
}
// OnHttpResponseBody implements the same method on types.HttpContext.
func (h *testHttpContext) OnHttpResponseBody(bodySize int, endOfStream bool) types.Action {
if !endOfStream {
if h.buffered {
return types.ActionPause
} else {
return types.ActionContinue
}
}
body, err := proxywasm.GetHttpResponseBody(0, bodySize)
if err != nil {
panic(err)
}
proxywasm.LogInfo(fmt.Sprintf("response body:%s", string(body)))
return types.ActionContinue
}
func TestBodyBuffering(t *testing.T) {
tests := []struct {
name string
buffered bool
action types.Action
logged string
}{
{
name: "buffered",
buffered: true,
action: types.ActionPause,
// The first chunk has been buffered, therefore it will be retrieved when calling GetHttpRequestBody at the end of stream.
logged: "1111122222",
},
{
name: "unbuffered",
buffered: false,
action: types.ActionContinue,
// The first chunk has not been buffered, therefore it will not be retrieved when calling GetHttpRequestBody at the end of stream.
logged: "22222",
},
}
for _, tc := range tests {
tt := tc
t.Run(tt.name, func(t *testing.T) {
host, reset := NewHostEmulator(NewEmulatorOption().WithVMContext(&testPlugin{buffered: tt.buffered}))
defer reset()
id := host.InitializeHttpContext()
action := host.CallOnRequestBody(id, []byte("11111"), false)
require.Equal(t, tt.action, action)
action = host.CallOnRequestBody(id, []byte("22222"), true)
require.Equal(t, types.ActionContinue, action)
action = host.CallOnResponseBody(id, []byte("11111"), false)
require.Equal(t, tt.action, action)
action = host.CallOnResponseBody(id, []byte("22222"), true)
require.Equal(t, types.ActionContinue, action)
logs := host.GetInfoLogs()
require.Contains(t, logs, fmt.Sprintf("request body:%s", tt.logged))
require.Contains(t, logs, fmt.Sprintf("response body:%s", tt.logged))
})
}
}
func TestProperties(t *testing.T) {
t.Run("Set and get properties", func(t *testing.T) {
host, reset := NewHostEmulator(NewEmulatorOption().WithVMContext(&testPlugin{}))
defer reset()
_ = host.InitializeHttpContext()
propertyPath := []string{
"route_metadata",
"filter_metadata",
"envoy.filters.http.wasm",
"hello",
}
propertyData := []byte("world")
err := host.SetProperty(propertyPath, propertyData)
require.Equal(t, err, nil)
data, err := host.GetProperty(propertyPath)
require.Equal(t, err, nil)
require.Equal(t, data, propertyData)
_, err = host.GetProperty([]string{"non-existent path"})
require.Equal(t, err, internal.StatusToError(internal.StatusNotFound))
})
}