2014-08-29 21:49:50 +04:00
|
|
|
// Copyright 2014 Manu Martinez-Almeida. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-07-04 12:12:28 +04:00
|
|
|
package gin
|
|
|
|
|
2014-07-05 19:04:11 +04:00
|
|
|
import (
|
2017-05-29 11:03:49 +03:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2015-05-31 23:35:49 +03:00
|
|
|
"reflect"
|
2014-07-05 19:04:11 +04:00
|
|
|
"testing"
|
2017-05-29 11:03:49 +03:00
|
|
|
"time"
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2014-07-04 12:12:28 +04:00
|
|
|
)
|
|
|
|
|
2017-05-29 11:03:49 +03:00
|
|
|
func setupHTMLFiles(t *testing.T) func() {
|
|
|
|
go func() {
|
|
|
|
router := New()
|
|
|
|
router.Delims("{[{", "}]}")
|
|
|
|
router.LoadHTMLFiles("./fixtures/basic/hello.tmpl")
|
|
|
|
router.GET("/test", func(c *Context) {
|
|
|
|
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
|
|
|
})
|
|
|
|
router.Run(":8888")
|
|
|
|
}()
|
|
|
|
t.Log("waiting 1 second for server startup")
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
return func() {}
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupHTMLGlob(t *testing.T) func() {
|
|
|
|
go func() {
|
|
|
|
router := New()
|
|
|
|
router.Delims("{[{", "}]}")
|
|
|
|
router.LoadHTMLGlob("./fixtures/basic/*")
|
|
|
|
router.GET("/test", func(c *Context) {
|
|
|
|
c.HTML(http.StatusOK, "hello.tmpl", map[string]string{"name": "world"})
|
|
|
|
})
|
|
|
|
router.Run(":8888")
|
|
|
|
}()
|
|
|
|
t.Log("waiting 1 second for server startup")
|
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
return func() {}
|
|
|
|
}
|
|
|
|
|
2015-04-09 13:15:02 +03:00
|
|
|
//TODO
|
2017-05-29 11:03:49 +03:00
|
|
|
func TestLoadHTMLGlob(t *testing.T) {
|
|
|
|
td := setupHTMLGlob(t)
|
|
|
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _ := ioutil.ReadAll(res.Body)
|
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
|
|
|
|
|
|
|
td()
|
|
|
|
}
|
|
|
|
|
2015-04-09 13:15:02 +03:00
|
|
|
// func (engine *Engine) LoadHTMLFiles(files ...string) {
|
|
|
|
// func (engine *Engine) RunTLS(addr string, cert string, key string) error {
|
|
|
|
|
2014-08-21 03:01:42 +04:00
|
|
|
func init() {
|
|
|
|
SetMode(TestMode)
|
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestCreateEngine(t *testing.T) {
|
|
|
|
router := New()
|
2015-07-08 05:27:23 +03:00
|
|
|
assert.Equal(t, "/", router.basePath)
|
2015-04-08 03:58:35 +03:00
|
|
|
assert.Equal(t, router.engine, router)
|
|
|
|
assert.Empty(t, router.Handlers)
|
2015-06-04 05:32:18 +03:00
|
|
|
}
|
|
|
|
|
2015-08-16 17:36:47 +03:00
|
|
|
// func TestLoadHTMLDebugMode(t *testing.T) {
|
|
|
|
// router := New()
|
|
|
|
// SetMode(DebugMode)
|
|
|
|
// router.LoadHTMLGlob("*.testtmpl")
|
|
|
|
// r := router.HTMLRender.(render.HTMLDebug)
|
|
|
|
// assert.Empty(t, r.Files)
|
|
|
|
// assert.Equal(t, r.Glob, "*.testtmpl")
|
|
|
|
//
|
|
|
|
// router.LoadHTMLFiles("index.html.testtmpl", "login.html.testtmpl")
|
|
|
|
// r = router.HTMLRender.(render.HTMLDebug)
|
|
|
|
// assert.Empty(t, r.Glob)
|
|
|
|
// assert.Equal(t, r.Files, []string{"index.html", "login.html"})
|
|
|
|
// SetMode(TestMode)
|
|
|
|
// }
|
2015-07-04 21:06:40 +03:00
|
|
|
|
2017-05-29 11:03:49 +03:00
|
|
|
func TestLoadHTMLFiles(t *testing.T) {
|
|
|
|
td := setupHTMLFiles(t)
|
|
|
|
res, err := http.Get("http://127.0.0.1:8888/test")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, _ := ioutil.ReadAll(res.Body)
|
|
|
|
assert.Equal(t, "<h1>Hello world</h1>", string(resp[:]))
|
|
|
|
td()
|
|
|
|
}
|
|
|
|
|
2015-07-04 21:06:40 +03:00
|
|
|
func TestLoadHTMLReleaseMode(t *testing.T) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-06-04 05:32:18 +03:00
|
|
|
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) {}})
|
2014-07-04 12:12:28 +04:00
|
|
|
|
2015-06-04 05:32:18 +03:00
|
|
|
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 TestAddRouteFails(t *testing.T) {
|
|
|
|
router := New()
|
2015-05-20 00:22:35 +03:00
|
|
|
assert.Panics(t, func() { router.addRoute("", "/", HandlersChain{func(_ *Context) {}}) })
|
|
|
|
assert.Panics(t, func() { router.addRoute("GET", "a", HandlersChain{func(_ *Context) {}}) })
|
|
|
|
assert.Panics(t, func() { router.addRoute("GET", "/", HandlersChain{}) })
|
2015-06-04 05:32:18 +03:00
|
|
|
|
|
|
|
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
router.addRoute("POST", "/post", HandlersChain{func(_ *Context) {}})
|
|
|
|
})
|
2014-07-04 12:12:28 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestCreateDefaultRouter(t *testing.T) {
|
|
|
|
router := Default()
|
|
|
|
assert.Len(t, router.Handlers, 2)
|
2014-07-04 12:12:28 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestNoRouteWithoutGlobalHandlers(t *testing.T) {
|
2015-05-31 23:35:49 +03:00
|
|
|
var middleware0 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware1 HandlerFunc = func(c *Context) {}
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
router := New()
|
|
|
|
|
|
|
|
router.NoRoute(middleware0)
|
|
|
|
assert.Nil(t, router.Handlers)
|
|
|
|
assert.Len(t, router.noRoute, 1)
|
|
|
|
assert.Len(t, router.allNoRoute, 1)
|
2015-05-31 23:35:49 +03:00
|
|
|
compareFunc(t, router.noRoute[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoRoute[0], middleware0)
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
router.NoRoute(middleware1, middleware0)
|
|
|
|
assert.Len(t, router.noRoute, 2)
|
|
|
|
assert.Len(t, router.allNoRoute, 2)
|
2015-05-31 23:35:49 +03:00
|
|
|
compareFunc(t, router.noRoute[0], middleware1)
|
|
|
|
compareFunc(t, router.allNoRoute[0], middleware1)
|
|
|
|
compareFunc(t, router.noRoute[1], middleware0)
|
|
|
|
compareFunc(t, router.allNoRoute[1], middleware0)
|
2014-07-04 12:12:28 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestNoRouteWithGlobalHandlers(t *testing.T) {
|
2015-05-31 23:35:49 +03:00
|
|
|
var middleware0 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware1 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware2 HandlerFunc = func(c *Context) {}
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
router := New()
|
|
|
|
router.Use(middleware2)
|
|
|
|
|
|
|
|
router.NoRoute(middleware0)
|
|
|
|
assert.Len(t, router.allNoRoute, 2)
|
|
|
|
assert.Len(t, router.Handlers, 1)
|
|
|
|
assert.Len(t, router.noRoute, 1)
|
|
|
|
|
2015-05-31 23:35:49 +03:00
|
|
|
compareFunc(t, router.Handlers[0], middleware2)
|
|
|
|
compareFunc(t, router.noRoute[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoRoute[0], middleware2)
|
|
|
|
compareFunc(t, router.allNoRoute[1], middleware0)
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
router.Use(middleware1)
|
|
|
|
assert.Len(t, router.allNoRoute, 3)
|
|
|
|
assert.Len(t, router.Handlers, 2)
|
|
|
|
assert.Len(t, router.noRoute, 1)
|
|
|
|
|
2015-05-31 23:35:49 +03:00
|
|
|
compareFunc(t, router.Handlers[0], middleware2)
|
|
|
|
compareFunc(t, router.Handlers[1], middleware1)
|
|
|
|
compareFunc(t, router.noRoute[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoRoute[0], middleware2)
|
|
|
|
compareFunc(t, router.allNoRoute[1], middleware1)
|
|
|
|
compareFunc(t, router.allNoRoute[2], middleware0)
|
2014-07-04 12:12:28 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestNoMethodWithoutGlobalHandlers(t *testing.T) {
|
2015-05-31 23:35:49 +03:00
|
|
|
var middleware0 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware1 HandlerFunc = func(c *Context) {}
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
router := New()
|
|
|
|
|
|
|
|
router.NoMethod(middleware0)
|
|
|
|
assert.Empty(t, router.Handlers)
|
|
|
|
assert.Len(t, router.noMethod, 1)
|
|
|
|
assert.Len(t, router.allNoMethod, 1)
|
2015-05-31 23:35:49 +03:00
|
|
|
compareFunc(t, router.noMethod[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoMethod[0], middleware0)
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
router.NoMethod(middleware1, middleware0)
|
|
|
|
assert.Len(t, router.noMethod, 2)
|
|
|
|
assert.Len(t, router.allNoMethod, 2)
|
2015-05-31 23:35:49 +03:00
|
|
|
compareFunc(t, router.noMethod[0], middleware1)
|
|
|
|
compareFunc(t, router.allNoMethod[0], middleware1)
|
|
|
|
compareFunc(t, router.noMethod[1], middleware0)
|
|
|
|
compareFunc(t, router.allNoMethod[1], middleware0)
|
2014-07-04 12:12:28 +04:00
|
|
|
}
|
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestRebuild404Handlers(t *testing.T) {
|
2014-07-05 19:04:11 +04:00
|
|
|
|
|
|
|
}
|
2014-07-28 14:05:23 +04:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
func TestNoMethodWithGlobalHandlers(t *testing.T) {
|
2015-05-31 23:35:49 +03:00
|
|
|
var middleware0 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware1 HandlerFunc = func(c *Context) {}
|
|
|
|
var middleware2 HandlerFunc = func(c *Context) {}
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
router := New()
|
|
|
|
router.Use(middleware2)
|
|
|
|
|
|
|
|
router.NoMethod(middleware0)
|
|
|
|
assert.Len(t, router.allNoMethod, 2)
|
|
|
|
assert.Len(t, router.Handlers, 1)
|
|
|
|
assert.Len(t, router.noMethod, 1)
|
|
|
|
|
2015-05-31 23:35:49 +03:00
|
|
|
compareFunc(t, router.Handlers[0], middleware2)
|
|
|
|
compareFunc(t, router.noMethod[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoMethod[0], middleware2)
|
|
|
|
compareFunc(t, router.allNoMethod[1], middleware0)
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
router.Use(middleware1)
|
|
|
|
assert.Len(t, router.allNoMethod, 3)
|
|
|
|
assert.Len(t, router.Handlers, 2)
|
|
|
|
assert.Len(t, router.noMethod, 1)
|
|
|
|
|
2015-05-31 23:35:49 +03:00
|
|
|
compareFunc(t, router.Handlers[0], middleware2)
|
|
|
|
compareFunc(t, router.Handlers[1], middleware1)
|
|
|
|
compareFunc(t, router.noMethod[0], middleware0)
|
|
|
|
compareFunc(t, router.allNoMethod[0], middleware2)
|
|
|
|
compareFunc(t, router.allNoMethod[1], middleware1)
|
|
|
|
compareFunc(t, router.allNoMethod[2], middleware0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func compareFunc(t *testing.T, a, b interface{}) {
|
|
|
|
sf1 := reflect.ValueOf(a)
|
|
|
|
sf2 := reflect.ValueOf(b)
|
|
|
|
if sf1.Pointer() != sf2.Pointer() {
|
|
|
|
t.Error("different functions")
|
|
|
|
}
|
2014-07-28 14:05:23 +04:00
|
|
|
}
|
2015-06-07 05:20:39 +03:00
|
|
|
|
|
|
|
func TestListOfRoutes(t *testing.T) {
|
|
|
|
router := New()
|
2016-04-15 02:16:46 +03:00
|
|
|
router.GET("/favicon.ico", handlerTest1)
|
|
|
|
router.GET("/", handlerTest1)
|
2015-06-07 05:20:39 +03:00
|
|
|
group := router.Group("/users")
|
|
|
|
{
|
2016-04-15 02:16:46 +03:00
|
|
|
group.GET("/", handlerTest2)
|
|
|
|
group.GET("/:id", handlerTest1)
|
|
|
|
group.POST("/:id", handlerTest2)
|
2015-06-07 05:20:39 +03:00
|
|
|
}
|
|
|
|
router.Static("/static", ".")
|
|
|
|
|
|
|
|
list := router.Routes()
|
|
|
|
|
|
|
|
assert.Len(t, list, 7)
|
2015-08-21 21:57:53 +03:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 18:17:22 +03:00
|
|
|
Method: "GET",
|
|
|
|
Path: "/favicon.ico",
|
2016-04-15 02:35:22 +03:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
2015-06-07 05:20:39 +03:00
|
|
|
})
|
2015-08-21 21:57:53 +03:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 18:17:22 +03:00
|
|
|
Method: "GET",
|
|
|
|
Path: "/",
|
2016-04-15 02:35:22 +03:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
2015-06-07 05:20:39 +03:00
|
|
|
})
|
2015-08-21 21:57:53 +03:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 18:17:22 +03:00
|
|
|
Method: "GET",
|
|
|
|
Path: "/users/",
|
2016-04-15 02:35:22 +03:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
|
2015-06-07 05:20:39 +03:00
|
|
|
})
|
2015-08-21 21:57:53 +03:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 18:17:22 +03:00
|
|
|
Method: "GET",
|
|
|
|
Path: "/users/:id",
|
2016-04-15 02:35:22 +03:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest1$",
|
2015-06-07 05:20:39 +03:00
|
|
|
})
|
2015-08-21 21:57:53 +03:00
|
|
|
assertRoutePresent(t, list, RouteInfo{
|
2015-06-18 18:17:22 +03:00
|
|
|
Method: "POST",
|
|
|
|
Path: "/users/:id",
|
2016-04-15 02:35:22 +03:00
|
|
|
Handler: "^(.*/vendor/)?github.com/gin-gonic/gin.handlerTest2$",
|
2015-06-07 05:20:39 +03:00
|
|
|
})
|
|
|
|
}
|
2015-06-18 18:17:22 +03:00
|
|
|
|
2015-08-21 21:57:53 +03:00
|
|
|
func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo) {
|
|
|
|
for _, gotRoute := range gotRoutes {
|
|
|
|
if gotRoute.Path == wantRoute.Path && gotRoute.Method == wantRoute.Method {
|
2016-03-09 10:29:01 +03:00
|
|
|
assert.Regexp(t, wantRoute.Handler, gotRoute.Handler)
|
2015-08-21 21:57:53 +03:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.Errorf("route not found: %v", wantRoute)
|
|
|
|
}
|
|
|
|
|
2016-04-15 02:16:46 +03:00
|
|
|
func handlerTest1(c *Context) {}
|
|
|
|
func handlerTest2(c *Context) {}
|