|
|
package response
|
|
|
|
|
|
import (
|
|
|
"context"
|
|
|
"errors"
|
|
|
"testing"
|
|
|
|
|
|
codecimpl "github.com/noahlann/nnet/internal/codec"
|
|
|
protocolpkg "github.com/noahlann/nnet/pkg/protocol"
|
|
|
unpackerpkg "github.com/noahlann/nnet/pkg/unpacker"
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
"github.com/stretchr/testify/require"
|
|
|
)
|
|
|
|
|
|
func TestResponse(t *testing.T) {
|
|
|
mockConn := &mockConnectionWriter{}
|
|
|
codecRegistry := codecimpl.NewRegistry()
|
|
|
_ = codecRegistry.Register("json", codecimpl.NewJSONCodec())
|
|
|
resp := New(mockConn, nil, codecRegistry, "json")
|
|
|
|
|
|
// 测试WriteString
|
|
|
err := resp.WriteString("hello")
|
|
|
require.NoError(t, err, "Expected no error when writing string")
|
|
|
assert.Equal(t, 1, len(mockConn.written), "Expected data to be written")
|
|
|
}
|
|
|
|
|
|
func TestResponseWriteBytes(t *testing.T) {
|
|
|
mockConn := &mockConnectionWriter{}
|
|
|
codecRegistry := codecimpl.NewRegistry()
|
|
|
resp := New(mockConn, nil, codecRegistry, "json")
|
|
|
|
|
|
data := []byte("test data")
|
|
|
err := resp.WriteBytes(data)
|
|
|
require.NoError(t, err, "Expected no error when writing bytes")
|
|
|
assert.Equal(t, 1, len(mockConn.written), "Expected data to be written")
|
|
|
assert.Equal(t, data, mockConn.written[0], "Expected written data to match")
|
|
|
}
|
|
|
|
|
|
func TestResponseWrite(t *testing.T) {
|
|
|
mockConn := &mockConnectionWriter{}
|
|
|
codecRegistry := codecimpl.NewRegistry()
|
|
|
_ = codecRegistry.Register("json", codecimpl.NewJSONCodec())
|
|
|
resp := New(mockConn, nil, codecRegistry, "json")
|
|
|
|
|
|
type TestStruct struct {
|
|
|
Name string `json:"name"`
|
|
|
Value int `json:"value"`
|
|
|
}
|
|
|
|
|
|
data := TestStruct{
|
|
|
Name: "test",
|
|
|
Value: 42,
|
|
|
}
|
|
|
|
|
|
err := resp.WriteWithCodec(data, "json")
|
|
|
require.NoError(t, err, "Expected no error when writing struct")
|
|
|
assert.Equal(t, 1, len(mockConn.written), "Expected data to be written")
|
|
|
}
|
|
|
|
|
|
func TestResponseWriteWithCodec(t *testing.T) {
|
|
|
mockConn := &mockConnectionWriter{}
|
|
|
codecRegistry := codecimpl.NewRegistry()
|
|
|
_ = codecRegistry.Register("json", codecimpl.NewJSONCodec())
|
|
|
resp := New(mockConn, nil, codecRegistry, "json")
|
|
|
|
|
|
type TestStruct struct {
|
|
|
Name string `json:"name"`
|
|
|
}
|
|
|
|
|
|
data := TestStruct{
|
|
|
Name: "test",
|
|
|
}
|
|
|
|
|
|
err := resp.WriteWithCodec(data, "json")
|
|
|
require.NoError(t, err, "Expected no error when writing with codec")
|
|
|
assert.Equal(t, 1, len(mockConn.written), "Expected data to be written")
|
|
|
}
|
|
|
|
|
|
func TestResponseWriteWithInvalidCodec(t *testing.T) {
|
|
|
mockConn := &mockConnectionWriter{}
|
|
|
codecRegistry := codecimpl.NewRegistry()
|
|
|
resp := New(mockConn, nil, codecRegistry, "json")
|
|
|
|
|
|
err := resp.WriteWithCodec("test", "nonexistent")
|
|
|
assert.Error(t, err, "Expected error for nonexistent codec")
|
|
|
}
|
|
|
|
|
|
func TestResponseHeader(t *testing.T) {
|
|
|
mockConn := &mockConnectionWriter{}
|
|
|
codecRegistry := codecimpl.NewRegistry()
|
|
|
resp := New(mockConn, nil, codecRegistry, "json")
|
|
|
|
|
|
// 测试Header(初始可能为nil,取决于协议)
|
|
|
header := resp.Header()
|
|
|
// Header可能为nil(无帧头协议),这是正常的
|
|
|
_ = header
|
|
|
}
|
|
|
|
|
|
func TestResponseSetHeader(t *testing.T) {
|
|
|
mockConn := &mockConnectionWriter{}
|
|
|
codecRegistry := codecimpl.NewRegistry()
|
|
|
resp := New(mockConn, nil, codecRegistry, "json").(*responseImpl)
|
|
|
|
|
|
// 设置Header
|
|
|
newHeader := NewFrameHeader()
|
|
|
newHeader.Set("message_id", uint64(123))
|
|
|
newHeader.Set("type", "response")
|
|
|
resp.SetHeader(newHeader)
|
|
|
|
|
|
// 验证Header
|
|
|
header := resp.Header()
|
|
|
assert.NotNil(t, header, "Expected header to be non-nil")
|
|
|
assert.Equal(t, uint64(123), header.Get("message_id"), "Expected message ID to match")
|
|
|
assert.Equal(t, "response", header.Get("type"), "Expected type to match")
|
|
|
}
|
|
|
|
|
|
func TestResponseWriteError(t *testing.T) {
|
|
|
mockConn := &mockConnectionWriter{
|
|
|
writeError: errors.New("write error"),
|
|
|
}
|
|
|
codecRegistry := codecimpl.NewRegistry()
|
|
|
resp := New(mockConn, nil, codecRegistry, "json")
|
|
|
|
|
|
err := resp.WriteString("test")
|
|
|
assert.Error(t, err, "Expected error when write fails")
|
|
|
}
|
|
|
|
|
|
func TestResponseWriteWithProtocol(t *testing.T) {
|
|
|
mockConn := &mockConnectionWriter{}
|
|
|
mockProtocol := &mockProtocol{}
|
|
|
codecRegistry := codecimpl.NewRegistry()
|
|
|
resp := New(mockConn, mockProtocol, codecRegistry, "json")
|
|
|
|
|
|
err := resp.WriteString("test")
|
|
|
require.NoError(t, err, "Expected no error when writing with protocol")
|
|
|
assert.Equal(t, 1, len(mockConn.written), "Expected data to be written")
|
|
|
}
|
|
|
|
|
|
// mockConnectionWriter 模拟连接写入器
|
|
|
type mockConnectionWriter struct {
|
|
|
written [][]byte
|
|
|
writeError error
|
|
|
}
|
|
|
|
|
|
func (m *mockConnectionWriter) Write(data []byte) error {
|
|
|
if m.writeError != nil {
|
|
|
return m.writeError
|
|
|
}
|
|
|
m.written = append(m.written, data)
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
// mockProtocol 模拟协议
|
|
|
type mockProtocol struct{}
|
|
|
|
|
|
func (m *mockProtocol) Name() string {
|
|
|
return "test"
|
|
|
}
|
|
|
|
|
|
func (m *mockProtocol) Version() string {
|
|
|
return "1.0"
|
|
|
}
|
|
|
|
|
|
func (m *mockProtocol) HasHeader() bool {
|
|
|
return false
|
|
|
}
|
|
|
|
|
|
func (m *mockProtocol) Encode(data []byte, header protocolpkg.FrameHeader) ([]byte, error) {
|
|
|
return data, nil
|
|
|
}
|
|
|
|
|
|
func (m *mockProtocol) Decode(data []byte) (protocolpkg.FrameHeader, []byte, error) {
|
|
|
return nil, data, nil
|
|
|
}
|
|
|
|
|
|
func (m *mockProtocol) Handle(ctx context.Context, data []byte) ([]byte, error) {
|
|
|
return data, nil
|
|
|
}
|
|
|
|
|
|
func (m *mockProtocol) Unpacker() unpackerpkg.Unpacker {
|
|
|
return nil
|
|
|
}
|
|
|
|