forked from tetratelabs/proxy-wasm-go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtiming_off_test.go
85 lines (67 loc) · 2.41 KB
/
timing_off_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
//go:build !proxywasm_timing
package proxytest
import (
"testing"
"github.com/proxy-wasm/proxy-wasm-go-sdk/proxywasm/types"
"github.com/stretchr/testify/require"
)
type noopPlugin struct {
types.DefaultVMContext
tcp bool
}
// NewPluginContext implements the same method on types.DefaultVMContext.
func (p *noopPlugin) NewPluginContext(uint32) types.PluginContext {
return &noopPluginContext{tcp: p.tcp}
}
type noopPluginContext struct {
types.DefaultPluginContext
tcp bool
}
// NewHttpContext implements the same method on types.DefaultPluginContext.
func (p *noopPluginContext) NewHttpContext(uint32) types.HttpContext {
if !p.tcp {
return &noopHttpContext{}
}
return nil
}
// NewTcpContext implements the same method on types.DefaultPluginContext.
func (p *noopPluginContext) NewTcpContext(uint32) types.TcpContext {
if p.tcp {
return &noopTcpContext{}
}
return nil
}
type noopHttpContext struct {
types.DefaultHttpContext
}
type noopTcpContext struct {
types.DefaultTcpContext
}
// Execute lifecycle methods, there should be no logs for the no-op plugin.
func TestTimingOff(t *testing.T) {
t.Run("http", func(t *testing.T) {
host, reset := NewHostEmulator(NewEmulatorOption().WithVMContext(&noopPlugin{}))
defer reset()
require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
id := host.InitializeHttpContext()
require.Equal(t, types.ActionContinue, host.CallOnRequestHeaders(id, nil, false))
require.Equal(t, types.ActionContinue, host.CallOnRequestBody(id, nil, false))
require.Equal(t, types.ActionContinue, host.CallOnRequestTrailers(id, nil))
require.Equal(t, types.ActionContinue, host.CallOnResponseHeaders(id, nil, false))
require.Equal(t, types.ActionContinue, host.CallOnResponseBody(id, nil, false))
require.Equal(t, types.ActionContinue, host.CallOnResponseTrailers(id, nil))
host.CompleteHttpContext(id)
require.Empty(t, host.GetDebugLogs())
})
t.Run("tcp", func(t *testing.T) {
host, reset := NewHostEmulator(NewEmulatorOption().WithVMContext(&noopPlugin{tcp: true}))
defer reset()
require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
id, action := host.InitializeConnection()
require.Equal(t, types.ActionContinue, action)
require.Equal(t, types.ActionContinue, host.CallOnDownstreamData(id, nil))
require.Equal(t, types.ActionContinue, host.CallOnUpstreamData(id, nil))
host.CompleteConnection(id)
require.Empty(t, host.GetDebugLogs())
})
}