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.

79 lines
1.8 KiB
Go

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package router
import (
"testing"
ctxpkg "github.com/noahlann/nnet/pkg/context"
routerpkg "github.com/noahlann/nnet/pkg/router"
"github.com/stretchr/testify/assert"
)
func TestRoute(t *testing.T) {
router := NewRouter()
handler := func(ctx ctxpkg.Context) error {
return nil
}
route := router.RegisterString("/test", handler)
assert.NotNil(t, route, "Expected route to be non-nil")
// Route接口可能没有Path方法只测试route不为nil
_ = route
}
func TestRouteWithOptions(t *testing.T) {
router := NewRouter()
type RequestBody struct {
Name string `json:"name"`
}
handler := func(ctx ctxpkg.Context) error {
return nil
}
route := router.RegisterString("/test", handler, routerpkg.WithRequestType(&RequestBody{}))
assert.NotNil(t, route, "Expected route to be non-nil")
requestType := route.RequestType()
assert.NotNil(t, requestType, "Expected request type to be set")
}
func TestRouteMiddleware(t *testing.T) {
router := NewRouter()
handler := func(ctx ctxpkg.Context) error {
return nil
}
route := router.RegisterString("/test", handler)
assert.NotNil(t, route, "Expected route to be non-nil")
// 测试route的Handler方法
routeHandler := route.Handler()
assert.NotNil(t, routeHandler, "Expected handler to be non-nil")
// 测试Use方法添加中间件
middleware := func(ctx ctxpkg.Context) error {
return nil
}
route.Use(middleware)
// 验证中间件已添加
routeHandler2 := route.Handler()
assert.NotNil(t, routeHandler2, "Expected handler to be non-nil")
}
func TestRouteCodec(t *testing.T) {
router := NewRouter()
handler := func(ctx ctxpkg.Context) error {
return nil
}
route := router.RegisterString("/test", handler, routerpkg.WithCodec("json"))
assert.NotNil(t, route)
assert.Equal(t, "json", route.CodecName(), "Expected codec name to be json")
}