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