You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

146 lines
3.8 KiB
Go

package router
import (
"context"
"testing"
"github.com/noahlann/nnet/internal/codec"
"github.com/noahlann/nnet/internal/request"
"github.com/noahlann/nnet/internal/response"
ctxpkg "github.com/noahlann/nnet/pkg/context"
routerpkg "github.com/noahlann/nnet/pkg/router"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestStringMatcher(t *testing.T) {
matcher := routerpkg.NewStringMatcher("hello")
// 创建正确的 Context
parentCtx := context.Background()
conn := &mockConnection{id: "test-conn"}
req := request.New([]byte("test"), nil)
codecRegistry := codec.NewRegistry()
resp := response.New(conn, nil, codecRegistry, "json")
ctx := ctxpkg.New(parentCtx, conn, req, resp)
// 测试精确匹配
assert.True(t, matcher.Match(routerpkg.MatchInput{Raw: []byte("hello")}, ctx), "Expected match for 'hello'")
// 测试不匹配
assert.False(t, matcher.Match(routerpkg.MatchInput{Raw: []byte("world")}, ctx), "Expected no match for 'world'")
}
func TestRouterRegister(t *testing.T) {
r := NewRouter()
handler := func(ctx ctxpkg.Context) error {
return ctx.Response().WriteString("OK")
}
route := r.RegisterString("/hello", handler)
require.NotNil(t, route, "Expected route to be returned")
}
func TestRouterMatch(t *testing.T) {
r := NewRouter()
handler := func(ctx ctxpkg.Context) error {
return ctx.Response().WriteString("OK")
}
r.RegisterString("/hello", handler)
// 创建正确的 Context
parentCtx := context.Background()
conn := &mockConnection{id: "test-conn"}
req := request.New([]byte("/hello"), nil)
codecRegistry := codec.NewRegistry()
resp := response.New(conn, nil, codecRegistry, "json")
ctx := ctxpkg.New(parentCtx, conn, req, resp)
route, matchedHandler, err := r.Match(routerpkg.MatchInput{Raw: []byte("/hello")}, ctx)
require.NoError(t, err, "Expected no error")
require.NotNil(t, route, "Expected route to be returned")
require.NotNil(t, matchedHandler, "Expected handler to be returned")
}
func TestRouterMatchNotFound(t *testing.T) {
r := NewRouter()
handler := func(ctx ctxpkg.Context) error {
return ctx.Response().WriteString("OK")
}
r.RegisterString("/hello", handler)
// 创建正确的 Context
parentCtx := context.Background()
conn := &mockConnection{id: "test-conn"}
req := request.New([]byte("/notfound"), nil)
codecRegistry := codec.NewRegistry()
resp := response.New(conn, nil, codecRegistry, "json")
ctx := ctxpkg.New(parentCtx, conn, req, resp)
_, _, err := r.Match(routerpkg.MatchInput{Raw: []byte("/notfound")}, ctx)
assert.Error(t, err, "Expected error for unmatched route")
}
// TestRouterGroup moved to group_test.go
func TestRouterRegisterWithOptions(t *testing.T) {
r := NewRouter()
type RequestBody struct {
Name string `json:"name"`
}
handler := func(ctx ctxpkg.Context) error {
return ctx.Response().WriteString("OK")
}
route := r.RegisterString("/hello", handler, routerpkg.WithRequestType(&RequestBody{}))
require.NotNil(t, route, "Expected route to be returned")
assert.NotNil(t, route.RequestType(), "Expected RequestType to be set")
}
func TestRouterRegisterCustom(t *testing.T) {
r := NewRouter()
handler := func(ctx ctxpkg.Context) error {
return ctx.Response().WriteString("OK")
}
customMatcher := func(input routerpkg.MatchInput, ctx ctxpkg.Context) bool {
return len(input.Raw) > 0 && input.Raw[0] == 'X'
}
route := r.RegisterCustom(customMatcher, handler)
require.NotNil(t, route, "Expected route to be returned")
}
// 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
}