package test import ( "context" "testing" "github.com/noahlann/nnet/internal/codec" "github.com/noahlann/nnet/internal/request" "github.com/noahlann/nnet/internal/response" internalrouter "github.com/noahlann/nnet/internal/router" ctxpkg "github.com/noahlann/nnet/pkg/context" "github.com/noahlann/nnet/pkg/router" ) func TestStringMatcher(t *testing.T) { matcher := router.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) // 测试精确匹配 if !matcher.Match(router.MatchInput{Raw: []byte("hello")}, ctx) { t.Error("Expected match for 'hello'") } // 测试不匹配 if matcher.Match(router.MatchInput{Raw: []byte("world")}, ctx) { t.Error("Expected no match for 'world'") } } func TestRouterRegister(t *testing.T) { r := internalrouter.NewRouter() handler := func(ctx ctxpkg.Context) error { return ctx.Response().WriteString("OK") } route := r.RegisterString("/hello", handler) if route == nil { t.Error("Expected route to be returned") } } func TestRouterMatch(t *testing.T) { r := internalrouter.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(router.MatchInput{Raw: []byte("/hello")}, ctx) if err != nil { t.Errorf("Expected no error, got: %v", err) } if route == nil { t.Error("Expected route to be returned") } if matchedHandler == nil { t.Error("Expected handler to be returned") } } func TestRouterMatchNotFound(t *testing.T) { r := internalrouter.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(router.MatchInput{Raw: []byte("/notfound")}, ctx) if err == nil { t.Error("Expected error for unmatched route") } } func TestRouterGroup(t *testing.T) { r := internalrouter.NewRouter() group := r.Group() if group == nil { t.Error("Expected group to be returned") } handler := func(ctx ctxpkg.Context) error { return ctx.Response().WriteString("OK") } route := group.RegisterString("/test", handler) if route == nil { t.Error("Expected route to be returned") } } func TestRouterRegisterWithOptions(t *testing.T) { r := internalrouter.NewRouter() type RequestBody struct { Name string `json:"name"` } handler := func(ctx ctxpkg.Context) error { return ctx.Response().WriteString("OK") } route := r.RegisterString("/hello", handler, router.WithRequestType(&RequestBody{})) if route == nil { t.Error("Expected route to be returned") } if route.RequestType() == nil { t.Error("Expected RequestType to be set") } } func TestRouterRegisterCustom(t *testing.T) { r := internalrouter.NewRouter() handler := func(ctx ctxpkg.Context) error { return ctx.Response().WriteString("OK") } customMatcher := func(input router.MatchInput, ctx ctxpkg.Context) bool { return len(input.Raw) > 0 && input.Raw[0] == 'X' } route := r.RegisterCustom(customMatcher, handler) if route == nil { t.Error("Expected route to be returned") } }