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

Commit 013f7cc

Browse files
committed
wip
Signed-off-by: mathetake <[email protected]>
1 parent 6af4e83 commit 013f7cc

File tree

22 files changed

+647
-434
lines changed

22 files changed

+647
-434
lines changed

examples/http_auth_random/main.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (ctx *httpAuthRandom) OnHttpRequestHeaders(int, bool) types.Action {
4848
proxywasm.LogInfof("request header: %s: %s", h[0], h[1])
4949
}
5050

51-
if err := proxywasm.DispatchHttpCall(clusterName, hs, "", [][2]string{},
51+
if _, err := proxywasm.DispatchHttpCall(clusterName, hs, "", [][2]string{},
5252
50000, httpCallResponseCallback); err != nil {
5353
proxywasm.LogCriticalf("dipatch httpcall failed: %v", err)
5454
return types.ActionContinue
@@ -96,9 +96,3 @@ func httpCallResponseCallback(_ int, bodySize int, _ int) {
9696
{"powered-by", "proxy-wasm-go-sdk!!"},
9797
}, msg)
9898
}
99-
100-
// override default
101-
func (ctx *httpAuthRandom) OnVMDone() bool {
102-
proxywasm.LogInfof("%d finished", ctx.contextID)
103-
return true
104-
}

examples/http_auth_random/main_test.go

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,18 @@ import (
1111
)
1212

1313
func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
14-
host, done := proxytest.NewHttpFilterHost(newContext)
15-
defer done()
14+
host := proxytest.NewHostEmulator(nil, nil, nil, nil, newContext)
15+
defer host.Done()
1616

17-
id := host.InitContext()
18-
host.PutRequestHeaders(id, [][2]string{{"key", "value"}}) // OnHttpRequestHeaders called
17+
contextID := host.HttpFilterInitContext()
18+
host.HttpFilterPutRequestHeaders(contextID, [][2]string{{"key", "value"}}) // OnHttpRequestHeaders called
1919

20-
require.True(t, host.IsDispatchCalled(id)) // check if http call is dispatched
21-
require.Equal(t, types.ActionPause, host.GetCurrentAction(id)) // check if the current action is pause
20+
attrs := host.GetCalloutAttributesFromContext(contextID)
21+
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called
22+
23+
require.Equal(t, "httpbin", attrs[0].Upstream)
24+
require.Equal(t, types.ActionPause,
25+
host.HttpFilterGetCurrentStreamAction(contextID)) // check if the current action is pause
2226

2327
logs := host.GetLogs(types.LogLevelInfo)
2428
require.GreaterOrEqual(t, len(logs), 2)
@@ -28,8 +32,8 @@ func TestHttpAuthRandom_OnHttpRequestHeaders(t *testing.T) {
2832
}
2933

3034
func TestHttpAuthRandom_OnHttpCallResponse(t *testing.T) {
31-
host, done := proxytest.NewHttpFilterHost(newContext)
32-
defer done()
35+
host := proxytest.NewHostEmulator(nil, nil, nil, nil, newContext)
36+
defer host.Done()
3337

3438
// http://httpbin.org/uuid
3539
headers := [][2]string{
@@ -40,20 +44,30 @@ func TestHttpAuthRandom_OnHttpCallResponse(t *testing.T) {
4044
}
4145

4246
// access granted body
43-
id := host.InitContext()
47+
contextID := host.HttpFilterInitContext()
48+
host.HttpFilterPutRequestHeaders(contextID, nil) // OnHttpRequestHeaders called
49+
4450
body := []byte(`{"uuid": "7b10a67a-1c67-4199-835b-cbefcd4a63d4"}`)
45-
host.PutCalloutResponse(id, headers, nil, body)
46-
assert.Nil(t, host.GetSentLocalResponse(id))
51+
attrs := host.GetCalloutAttributesFromContext(contextID)
52+
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called
53+
54+
host.PutCalloutResponse(attrs[0].CalloutID, headers, nil, body)
55+
assert.Nil(t, host.HttpFilterGetSentLocalResponse(contextID))
4756

4857
logs := host.GetLogs(types.LogLevelInfo)
4958
require.Greater(t, len(logs), 1)
5059
assert.Equal(t, "access granted", logs[len(logs)-1])
5160

5261
// access denied body
53-
id = host.InitContext()
62+
contextID = host.HttpFilterInitContext()
63+
host.HttpFilterPutRequestHeaders(contextID, nil) // OnHttpRequestHeaders called
64+
5465
body = []byte(`{"uuid": "aaaaaaaa-1c67-4199-835b-cbefcd4a63d4"}`)
55-
host.PutCalloutResponse(id, headers, nil, body)
56-
localResponse := host.GetSentLocalResponse(id) // check local responses
66+
attrs = host.GetCalloutAttributesFromContext(contextID)
67+
require.Equal(t, len(attrs), 1) // verify DispatchHttpCall is called
68+
69+
host.PutCalloutResponse(attrs[0].CalloutID, headers, nil, body)
70+
localResponse := host.HttpFilterGetSentLocalResponse(contextID) // check local responses
5771
assert.NotNil(t, localResponse)
5872
logs = host.GetLogs(types.LogLevelInfo)
5973
assert.Equal(t, "access forbidden", logs[len(logs)-1])

examples/http_headers/main_test.go

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"fmt"
45
"testing"
56

67
"github.com/stretchr/testify/assert"
@@ -11,31 +12,36 @@ import (
1112
)
1213

1314
func TestHttpHeaders_OnHttpRequestHeaders(t *testing.T) {
14-
host, done := proxytest.NewHttpFilterHost(newContext)
15-
defer done()
16-
id := host.InitContext()
15+
host := proxytest.NewHostEmulator(nil, nil, nil, nil, newContext)
16+
defer host.Done()
17+
id := host.HttpFilterInitContext()
1718

1819
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
19-
host.PutRequestHeaders(id, hs) // call OnHttpRequestHeaders
20+
host.HttpFilterPutRequestHeaders(id, hs) // call OnHttpRequestHeaders
21+
22+
host.HttpFilterCompleteHttpStream(id)
2023

2124
logs := host.GetLogs(types.LogLevelInfo)
2225
require.Greater(t, len(logs), 1)
2326

24-
assert.Equal(t, "request header --> key2: value2", logs[len(logs)-1])
25-
assert.Equal(t, "request header --> key1: value1", logs[len(logs)-2])
27+
assert.Equal(t, fmt.Sprintf("%d finished", id), logs[len(logs)-1])
28+
assert.Equal(t, "request header --> key2: value2", logs[len(logs)-2])
29+
assert.Equal(t, "request header --> key1: value1", logs[len(logs)-3])
2630
}
2731

2832
func TestHttpHeaders_OnHttpResponseHeaders(t *testing.T) {
29-
host, done := proxytest.NewHttpFilterHost(newContext)
30-
defer done()
31-
id := host.InitContext()
33+
host := proxytest.NewHostEmulator(nil, nil, nil, nil, newContext)
34+
defer host.Done()
35+
id := host.HttpFilterInitContext()
3236

3337
hs := [][2]string{{"key1", "value1"}, {"key2", "value2"}}
34-
host.PutResponseHeaders(id, hs) // call OnHttpResponseHeaders
38+
host.HttpFilterPutResponseHeaders(id, hs) // call OnHttpRequestHeaders
39+
host.HttpFilterCompleteHttpStream(id) // call OnHttpStreamDone
3540

3641
logs := host.GetLogs(types.LogLevelInfo)
3742
require.Greater(t, len(logs), 1)
3843

39-
assert.Equal(t, "response header <-- key2: value2", logs[len(logs)-1])
40-
assert.Equal(t, "response header <-- key1: value1", logs[len(logs)-2])
44+
assert.Equal(t, fmt.Sprintf("%d finished", id), logs[len(logs)-1])
45+
assert.Equal(t, "response header <-- key2: value2", logs[len(logs)-2])
46+
assert.Equal(t, "response header <-- key1: value1", logs[len(logs)-3])
4147
}

examples/metrics/main.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,25 @@ import (
2020
)
2121

2222
func main() {
23-
proxywasm.SetNewRootContext(func(uint32) proxywasm.RootContext { return &metric{} })
24-
proxywasm.SetNewHttpContext(func(uint32) proxywasm.HttpContext { return &metric{} })
23+
proxywasm.SetNewRootContext(newRootContext)
24+
proxywasm.SetNewHttpContext(newHttpContext)
2525
}
2626

2727
var counter proxywasm.MetricCounter
2828

2929
const metricsName = "proxy_wasm_go.request_counter"
3030

31-
type metric struct {
31+
type metricRootContext struct {
3232
// you must embed the default context so that you need not to reimplement all the methods by yourself
3333
proxywasm.DefaultRootContext
34-
proxywasm.DefaultHttpContext
34+
}
35+
36+
func newRootContext(uint32) proxywasm.RootContext {
37+
return &metricRootContext{}
3538
}
3639

3740
// override
38-
func (ctx *metric) OnVMStart(int) bool {
41+
func (ctx *metricRootContext) OnVMStart(int) bool {
3942
ct, err := proxywasm.DefineCounterMetric(metricsName)
4043
if err != nil {
4144
proxywasm.LogCriticalf("error defining metrics: %v", err)
@@ -44,8 +47,17 @@ func (ctx *metric) OnVMStart(int) bool {
4447
return true
4548
}
4649

50+
type metricHttpContext struct {
51+
// you must embed the default context so that you need not to reimplement all the methods by yourself
52+
proxywasm.DefaultHttpContext
53+
}
54+
55+
func newHttpContext(uint32) proxywasm.HttpContext {
56+
return &metricHttpContext{}
57+
}
58+
4759
// override
48-
func (ctx *metric) OnHttpRequestHeaders(int, bool) types.Action {
60+
func (ctx *metricHttpContext) OnHttpRequestHeaders(int, bool) types.Action {
4961
prev, err := counter.Get()
5062
if err != nil {
5163
proxywasm.LogCriticalf("error retrieving previous metric: %v", err)

examples/metrics/main_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ import (
1111
)
1212

1313
func TestMetric(t *testing.T) {
14+
host := proxytest.NewHostEmulator(nil, nil,
15+
newRootContext, nil, newHttpContext,
16+
)
17+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
1418

15-
ctx := metric{}
16-
host, done := proxytest.NewRootContextHostEmulator(ctx, nil, nil)
17-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
18-
19-
host.StartVM() // define metric
19+
host.StartVM() // call OnVMStart: define metric
2020

21+
contextID := host.HttpFilterInitContext()
2122
exp := uint64(3)
2223
for i := uint64(0); i < exp; i++ {
23-
ctx.OnHttpRequestHeaders(0, false) // increment
24+
host.HttpFilterPutRequestHeaders(contextID, nil)
2425
}
2526

2627
logs := host.GetLogs(types.LogLevelInfo)

examples/network/main.go

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@ var (
2525
)
2626

2727
func main() {
28-
proxywasm.SetNewStreamContext(func(contextID uint32) proxywasm.StreamContext { return &context{} })
29-
proxywasm.SetNewRootContext(func(contextID uint32) proxywasm.RootContext { return &context{} })
28+
proxywasm.SetNewRootContext(newRootContext)
29+
proxywasm.SetNewStreamContext(newNetworkContext)
3030
}
3131

32-
type context struct {
32+
type rootContext struct {
3333
// you must embed the default context so that you need not to reimplement all the methods by yourself
3434
proxywasm.DefaultRootContext
35-
proxywasm.DefaultStreamContext
3635
}
3736

38-
func (ctx *context) OnVMStart(int) bool {
37+
func newRootContext(uint32) proxywasm.RootContext {
38+
return &rootContext{}
39+
}
40+
41+
func (ctx *rootContext) OnVMStart(int) bool {
3942
var err error
4043
counter, err = proxywasm.DefineCounterMetric(connectionCounterName)
4144
if err != nil {
@@ -44,12 +47,21 @@ func (ctx *context) OnVMStart(int) bool {
4447
return true
4548
}
4649

47-
func (ctx *context) OnNewConnection() types.Action {
50+
type networkContext struct {
51+
// you must embed the default context so that you need not to reimplement all the methods by yourself
52+
proxywasm.DefaultStreamContext
53+
}
54+
55+
func newNetworkContext(uint32) proxywasm.StreamContext {
56+
return &networkContext{}
57+
}
58+
59+
func (ctx *networkContext) OnNewConnection() types.Action {
4860
proxywasm.LogInfo("new connection!")
4961
return types.ActionContinue
5062
}
5163

52-
func (ctx *context) OnDownstreamData(dataSize int, _ bool) types.Action {
64+
func (ctx *networkContext) OnDownstreamData(dataSize int, _ bool) types.Action {
5365
if dataSize == 0 {
5466
return types.ActionContinue
5567
}
@@ -63,12 +75,12 @@ func (ctx *context) OnDownstreamData(dataSize int, _ bool) types.Action {
6375
return types.ActionContinue
6476
}
6577

66-
func (ctx *context) OnDownstreamClose(types.PeerType) {
78+
func (ctx *networkContext) OnDownstreamClose(types.PeerType) {
6779
proxywasm.LogInfo("downstream connection close!")
6880
return
6981
}
7082

71-
func (ctx *context) OnUpstreamData(dataSize int, _ bool) types.Action {
83+
func (ctx *networkContext) OnUpstreamData(dataSize int, _ bool) types.Action {
7284
if dataSize == 0 {
7385
return types.ActionContinue
7486
}
@@ -89,7 +101,7 @@ func (ctx *context) OnUpstreamData(dataSize int, _ bool) types.Action {
89101
return types.ActionContinue
90102
}
91103

92-
func (ctx *context) OnStreamDone() {
104+
func (ctx *networkContext) OnStreamDone() {
93105
err := counter.Increment(1)
94106
if err != nil {
95107
proxywasm.LogCriticalf("failed to increment connection counter: %v", err)

examples/network/main_test.go

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -21,72 +21,74 @@ import (
2121
"github.com/stretchr/testify/require"
2222

2323
"github.com/tetratelabs/proxy-wasm-go-sdk/proxytest"
24-
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm"
2524
"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
2625
)
2726

28-
func newStreamContext(uint32) proxywasm.StreamContext {
29-
return &context{}
30-
}
31-
3227
func TestNetwork_OnNewConnection(t *testing.T) {
33-
host, done := proxytest.NewNetworkFilterHost(newStreamContext)
34-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
28+
host := proxytest.NewHostEmulator(nil, nil,
29+
newRootContext, newNetworkContext, nil)
30+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
31+
32+
host.StartVM() // call OnVMStart: init metric
3533

36-
_ = host.InitConnection() // OnNewConnection is called
34+
_ = host.NetworkFilterInitConnection() // OnNewConnection is called
3735

3836
logs := host.GetLogs(types.LogLevelInfo) // retrieve logs emitted to Envoy
3937
assert.Equal(t, logs[0], "new connection!")
4038
}
4139

4240
func TestNetwork_OnDownstreamClose(t *testing.T) {
43-
host, done := proxytest.NewNetworkFilterHost(newStreamContext)
44-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
41+
host := proxytest.NewHostEmulator(nil, nil,
42+
newRootContext, newNetworkContext, nil)
43+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
4544

46-
contextID := host.InitConnection() // OnNewConnection is called
47-
host.CloseDownstreamConnection(contextID) // OnDownstreamClose is called
45+
contextID := host.NetworkFilterInitConnection() // OnNewConnection is called
46+
host.NetworkFilterCloseDownstreamConnection(contextID) // OnDownstreamClose is called
4847

4948
logs := host.GetLogs(types.LogLevelInfo) // retrieve logs emitted to Envoy
5049
require.Len(t, logs, 2)
5150
assert.Equal(t, logs[1], "downstream connection close!")
5251
}
5352

5453
func TestNetwork_OnDownstreamData(t *testing.T) {
55-
host, done := proxytest.NewNetworkFilterHost(newStreamContext)
56-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
54+
host := proxytest.NewHostEmulator(nil, nil,
55+
newRootContext, newNetworkContext, nil)
56+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
5757

58-
contextID := host.InitConnection() // OnNewConnection is called
58+
contextID := host.NetworkFilterInitConnection() // OnNewConnection is called
5959

6060
msg := "this is downstream data"
6161
data := []byte(msg)
62-
host.PutDownstreamData(contextID, data) // OnDownstreamData is called
62+
host.NetworkFilterPutDownstreamData(contextID, data) // OnDownstreamData is called
6363

6464
logs := host.GetLogs(types.LogLevelInfo) // retrieve logs emitted to Envoy
6565
assert.Equal(t, ">>>>>> downstream data received >>>>>>\n"+msg, logs[len(logs)-1])
6666
}
6767

6868
func TestNetwork_OnUpstreamData(t *testing.T) {
69-
host, done := proxytest.NewNetworkFilterHost(newStreamContext)
70-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
69+
host := proxytest.NewHostEmulator(nil, nil,
70+
newRootContext, newNetworkContext, nil)
71+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
7172

72-
contextID := host.InitConnection() // OnNewConnection is called
73+
contextID := host.NetworkFilterInitConnection() // OnNewConnection is called
7374

7475
msg := "this is upstream data"
7576
data := []byte(msg)
76-
host.PutUpstreamData(contextID, data) // OnUpstreamData is called
77+
host.NetworkFilterPutUpstreamData(contextID, data) // OnUpstreamData is called
7778

7879
logs := host.GetLogs(types.LogLevelInfo) // retrieve logs emitted to Envoy
7980
assert.Equal(t, "<<<<<< upstream data received <<<<<<\n"+msg, logs[len(logs)-1])
8081
}
8182

8283
func TestNetwork_counter(t *testing.T) {
83-
host, done := proxytest.NewNetworkFilterHost(newStreamContext)
84-
defer done() // release the host emulation lock so that other test cases can insert their own host emulation
84+
host := proxytest.NewHostEmulator(nil, nil,
85+
newRootContext, newNetworkContext, nil)
86+
defer host.Done() // release the host emulation lock so that other test cases can insert their own host emulation
8587

86-
context{}.OnVMStart(0) // init metric
88+
host.StartVM() // call OnVMStart: init metric
8789

88-
contextID := host.InitConnection()
89-
host.CompleteConnection(contextID) // call OnDone on contextID -> increment the connection counter
90+
contextID := host.NetworkFilterInitConnection()
91+
host.NetworkFilterCompleteConnection(contextID) // call OnStreamDone on contextID -> increment the connection counter
9092

9193
logs := host.GetLogs(types.LogLevelInfo)
9294
require.Greater(t, len(logs), 0)

0 commit comments

Comments
 (0)