wa-lang.org/wazero@v1.0.2/imports/proxywasm/_proxytest/_timing_off_test.go (about)

     1  //go:build !proxywasm_timing
     2  
     3  package proxytest
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/stretchr/testify/require"
     9  
    10  	"github.com/tetratelabs/proxy-wasm-go-sdk/proxywasm/types"
    11  )
    12  
    13  type noopPlugin struct {
    14  	types.DefaultVMContext
    15  	tcp bool
    16  }
    17  
    18  // NewPluginContext implements the same method on types.DefaultVMContext.
    19  func (p *noopPlugin) NewPluginContext(uint32) types.PluginContext {
    20  	return &noopPluginContext{tcp: p.tcp}
    21  }
    22  
    23  type noopPluginContext struct {
    24  	types.DefaultPluginContext
    25  	tcp bool
    26  }
    27  
    28  // NewHttpContext implements the same method on types.DefaultPluginContext.
    29  func (p *noopPluginContext) NewHttpContext(uint32) types.HttpContext {
    30  	if !p.tcp {
    31  		return &noopHttpContext{}
    32  	}
    33  	return nil
    34  }
    35  
    36  // NewTcpContext implements the same method on types.DefaultPluginContext.
    37  func (p *noopPluginContext) NewTcpContext(uint32) types.TcpContext {
    38  	if p.tcp {
    39  		return &noopTcpContext{}
    40  	}
    41  	return nil
    42  }
    43  
    44  type noopHttpContext struct {
    45  	types.DefaultHttpContext
    46  }
    47  
    48  type noopTcpContext struct {
    49  	types.DefaultTcpContext
    50  }
    51  
    52  // Execute lifecycle methods, there should be no logs for the no-op plugin.
    53  func TestTimingOff(t *testing.T) {
    54  	t.Run("http", func(t *testing.T) {
    55  		host, reset := NewHostEmulator(NewEmulatorOption().WithVMContext(&noopPlugin{}))
    56  		defer reset()
    57  
    58  		require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
    59  		id := host.InitializeHttpContext()
    60  
    61  		require.Equal(t, types.ActionContinue, host.CallOnRequestHeaders(id, nil, false))
    62  		require.Equal(t, types.ActionContinue, host.CallOnRequestBody(id, nil, false))
    63  		require.Equal(t, types.ActionContinue, host.CallOnRequestTrailers(id, nil))
    64  		require.Equal(t, types.ActionContinue, host.CallOnResponseHeaders(id, nil, false))
    65  		require.Equal(t, types.ActionContinue, host.CallOnResponseBody(id, nil, false))
    66  		require.Equal(t, types.ActionContinue, host.CallOnResponseTrailers(id, nil))
    67  		host.CompleteHttpContext(id)
    68  
    69  		require.Empty(t, host.GetDebugLogs())
    70  	})
    71  
    72  	t.Run("tcp", func(t *testing.T) {
    73  		host, reset := NewHostEmulator(NewEmulatorOption().WithVMContext(&noopPlugin{tcp: true}))
    74  		defer reset()
    75  
    76  		require.Equal(t, types.OnPluginStartStatusOK, host.StartPlugin())
    77  		id, action := host.InitializeConnection()
    78  		require.Equal(t, types.ActionContinue, action)
    79  
    80  		require.Equal(t, types.ActionContinue, host.CallOnDownstreamData(id, nil))
    81  		require.Equal(t, types.ActionContinue, host.CallOnUpstreamData(id, nil))
    82  		host.CompleteConnection(id)
    83  
    84  		require.Empty(t, host.GetDebugLogs())
    85  	})
    86  }