func TestRouterGroupBasic(t *testing.T) { router := New() group := router.Group("/hola", func(c *Context) {}) group.Use(func(c *Context) {}) assert.Len(t, group.Handlers, 2) assert.Equal(t, group.BasePath, "/hola") assert.Equal(t, group.engine, router) group2 := group.Group("manu") group2.Use(func(c *Context) {}, func(c *Context) {}) assert.Len(t, group2.Handlers, 4) assert.Equal(t, group2.BasePath, "/hola/manu") assert.Equal(t, group2.engine, router) }
func TestAddRoute(t *testing.T) { router := New() router.addRoute("GET", "/", HandlersChain{func(_ *Context) {}}) assert.Len(t, router.trees, 1) assert.NotNil(t, router.trees.get("GET")) assert.Nil(t, router.trees.get("POST")) router.addRoute("POST", "/", HandlersChain{func(_ *Context) {}}) assert.Len(t, router.trees, 2) assert.NotNil(t, router.trees.get("GET")) assert.NotNil(t, router.trees.get("POST")) router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}}) assert.Len(t, router.trees, 2) }
func TestParseAccept(t *testing.T) { parts := parseAccept("text/html , application/xhtml+xml,application/xml;q=0.9, */* ;q=0.8") assert.Len(t, parts, 4) assert.Equal(t, parts[0], "text/html") assert.Equal(t, parts[1], "application/xhtml+xml") assert.Equal(t, parts[2], "application/xml") assert.Equal(t, parts[3], "*/*") }
func TestNoRouteWithoutGlobalHandlers(t *testing.T) { var middleware0 HandlerFunc = func(c *Context) {} var middleware1 HandlerFunc = func(c *Context) {} router := New() router.NoRoute(middleware0) assert.Nil(t, router.Handlers) assert.Len(t, router.noRoute, 1) assert.Len(t, router.allNoRoute, 1) compareFunc(t, router.noRoute[0], middleware0) compareFunc(t, router.allNoRoute[0], middleware0) router.NoRoute(middleware1, middleware0) assert.Len(t, router.noRoute, 2) assert.Len(t, router.allNoRoute, 2) compareFunc(t, router.noRoute[0], middleware1) compareFunc(t, router.allNoRoute[0], middleware1) compareFunc(t, router.noRoute[1], middleware0) compareFunc(t, router.allNoRoute[1], middleware0) }
func TestNoMethodWithoutGlobalHandlers(t *testing.T) { var middleware0 HandlerFunc = func(c *Context) {} var middleware1 HandlerFunc = func(c *Context) {} router := New() router.NoMethod(middleware0) assert.Empty(t, router.Handlers) assert.Len(t, router.noMethod, 1) assert.Len(t, router.allNoMethod, 1) compareFunc(t, router.noMethod[0], middleware0) compareFunc(t, router.allNoMethod[0], middleware0) router.NoMethod(middleware1, middleware0) assert.Len(t, router.noMethod, 2) assert.Len(t, router.allNoMethod, 2) compareFunc(t, router.noMethod[0], middleware1) compareFunc(t, router.allNoMethod[0], middleware1) compareFunc(t, router.noMethod[1], middleware0) compareFunc(t, router.allNoMethod[1], middleware0) }
func TestListOfRoutes(t *testing.T) { router := New() router.GET("/favicon.ico", handler_test1) router.GET("/", handler_test1) group := router.Group("/users") { group.GET("/", handler_test2) group.GET("/:id", handler_test1) group.POST("/:id", handler_test2) } router.Static("/static", ".") list := router.Routes() assert.Len(t, list, 7) assert.Contains(t, list, RouteInfo{ Method: "GET", Path: "/favicon.ico", Handler: "github.com/gin-gonic/gin.handler_test1", }) assert.Contains(t, list, RouteInfo{ Method: "GET", Path: "/", Handler: "github.com/gin-gonic/gin.handler_test1", }) assert.Contains(t, list, RouteInfo{ Method: "GET", Path: "/users/", Handler: "github.com/gin-gonic/gin.handler_test2", }) assert.Contains(t, list, RouteInfo{ Method: "GET", Path: "/users/:id", Handler: "github.com/gin-gonic/gin.handler_test1", }) assert.Contains(t, list, RouteInfo{ Method: "POST", Path: "/users/:id", Handler: "github.com/gin-gonic/gin.handler_test2", }) }
func TestBasicAuth(t *testing.T) { pairs := processAccounts(Accounts{ "admin": "password", "foo": "bar", "bar": "foo", }) assert.Len(t, pairs, 3) assert.Contains(t, pairs, authPair{ User: "******", Value: "Basic YmFyOmZvbw==", }) assert.Contains(t, pairs, authPair{ User: "******", Value: "Basic Zm9vOmJhcg==", }) assert.Contains(t, pairs, authPair{ User: "******", Value: "Basic YWRtaW46cGFzc3dvcmQ=", }) }
func TestCreateDefaultRouter(t *testing.T) { router := Default() assert.Len(t, router.Handlers, 2) }