|
|
package builtin
|
|
|
|
|
|
import (
|
|
|
"testing"
|
|
|
|
|
|
"github.com/noahlann/nnet/internal/codec"
|
|
|
interceptorimpl "github.com/noahlann/nnet/internal/interceptor"
|
|
|
"github.com/noahlann/nnet/internal/request"
|
|
|
"github.com/noahlann/nnet/internal/response"
|
|
|
ctxpkg "github.com/noahlann/nnet/pkg/context"
|
|
|
interceptorpkg "github.com/noahlann/nnet/pkg/interceptor"
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
"github.com/stretchr/testify/require"
|
|
|
)
|
|
|
|
|
|
func TestValidationInterceptor(t *testing.T) {
|
|
|
conn := &mockConnection{id: "test-conn"}
|
|
|
req := request.New([]byte("test"), nil)
|
|
|
codecRegistry := codec.NewRegistry()
|
|
|
resp := response.New(conn, nil, codecRegistry, "json")
|
|
|
ctx := ctxpkg.New(nil, conn, req, resp)
|
|
|
|
|
|
// 创建验证拦截器
|
|
|
validator := func(data []byte, ctx ctxpkg.Context) error {
|
|
|
if len(data) == 0 {
|
|
|
return interceptorpkg.New("data is empty")
|
|
|
}
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
interceptor := ValidationInterceptor(validator)
|
|
|
chain := interceptorimpl.NewChain()
|
|
|
|
|
|
// 测试有效数据
|
|
|
data := []byte("test")
|
|
|
result, continue_, err := interceptor.Intercept(data, ctx, chain)
|
|
|
require.NoError(t, err)
|
|
|
assert.True(t, continue_)
|
|
|
assert.Equal(t, data, result)
|
|
|
|
|
|
// 测试无效数据
|
|
|
emptyData := []byte("")
|
|
|
emptyChain := interceptorimpl.NewChain()
|
|
|
_, continue_, err = interceptor.Intercept(emptyData, ctx, emptyChain)
|
|
|
assert.Error(t, err)
|
|
|
assert.False(t, continue_)
|
|
|
}
|
|
|
|
|
|
func TestValidationInterceptorNil(t *testing.T) {
|
|
|
conn := &mockConnection{id: "test-conn"}
|
|
|
req := request.New([]byte("test"), nil)
|
|
|
codecRegistry := codec.NewRegistry()
|
|
|
resp := response.New(conn, nil, codecRegistry, "json")
|
|
|
ctx := ctxpkg.New(nil, conn, req, resp)
|
|
|
|
|
|
// 创建nil验证拦截器(应该直接通过)
|
|
|
interceptor := ValidationInterceptor(nil)
|
|
|
chain := interceptorimpl.NewChain()
|
|
|
|
|
|
data := []byte("test")
|
|
|
result, continue_, err := interceptor.Intercept(data, ctx, chain)
|
|
|
require.NoError(t, err)
|
|
|
assert.True(t, continue_)
|
|
|
assert.Equal(t, data, result)
|
|
|
}
|
|
|
|
|
|
func TestMinLengthInterceptor(t *testing.T) {
|
|
|
conn := &mockConnection{id: "test-conn"}
|
|
|
req := request.New([]byte("test"), nil)
|
|
|
codecRegistry := codec.NewRegistry()
|
|
|
resp := response.New(conn, nil, codecRegistry, "json")
|
|
|
ctx := ctxpkg.New(nil, conn, req, resp)
|
|
|
|
|
|
// 创建最小长度拦截器
|
|
|
interceptor := MinLengthInterceptor(5)
|
|
|
chain := interceptorimpl.NewChain()
|
|
|
|
|
|
// 测试数据长度不足
|
|
|
shortData := []byte("test")
|
|
|
shortChain := interceptorimpl.NewChain()
|
|
|
_, continue_, err := interceptor.Intercept(shortData, ctx, shortChain)
|
|
|
assert.Error(t, err)
|
|
|
assert.False(t, continue_)
|
|
|
|
|
|
// 测试数据长度足够
|
|
|
longData := []byte("test data")
|
|
|
result, continue_, err := interceptor.Intercept(longData, ctx, chain)
|
|
|
require.NoError(t, err)
|
|
|
assert.True(t, continue_)
|
|
|
assert.Equal(t, longData, result)
|
|
|
}
|
|
|
|
|
|
func TestMaxLengthInterceptor(t *testing.T) {
|
|
|
conn := &mockConnection{id: "test-conn"}
|
|
|
req := request.New([]byte("test"), nil)
|
|
|
codecRegistry := codec.NewRegistry()
|
|
|
resp := response.New(conn, nil, codecRegistry, "json")
|
|
|
ctx := ctxpkg.New(nil, conn, req, resp)
|
|
|
|
|
|
// 创建最大长度拦截器
|
|
|
interceptor := MaxLengthInterceptor(5)
|
|
|
chain := interceptorimpl.NewChain()
|
|
|
|
|
|
// 测试数据长度超过限制
|
|
|
longData := []byte("test data")
|
|
|
longChain := interceptorimpl.NewChain()
|
|
|
_, continue_, err := interceptor.Intercept(longData, ctx, longChain)
|
|
|
assert.Error(t, err)
|
|
|
assert.False(t, continue_)
|
|
|
|
|
|
// 测试数据长度在限制内
|
|
|
shortData := []byte("test")
|
|
|
result, continue_, err := interceptor.Intercept(shortData, ctx, chain)
|
|
|
require.NoError(t, err)
|
|
|
assert.True(t, continue_)
|
|
|
assert.Equal(t, shortData, result)
|
|
|
}
|
|
|
|
|
|
// mockConnection 模拟连接
|
|
|
type mockConnection struct {
|
|
|
id string
|
|
|
}
|
|
|
|
|
|
func (m *mockConnection) ID() string {
|
|
|
return m.id
|
|
|
}
|
|
|
|
|
|
func (m *mockConnection) RemoteAddr() string {
|
|
|
return "127.0.0.1:6995"
|
|
|
}
|
|
|
|
|
|
func (m *mockConnection) LocalAddr() string {
|
|
|
return "127.0.0.1:6995"
|
|
|
}
|
|
|
|
|
|
func (m *mockConnection) Write(data []byte) error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
func (m *mockConnection) Close() error {
|
|
|
return nil
|
|
|
}
|