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 (
|
|
|
|
"testing"
|
2015-04-08 03:58:35 +03:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2014-07-04 12:12:28 +04:00
|
|
|
)
|
|
|
|
|
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()
|
|
|
|
assert.Equal(t, "/", router.absolutePath)
|
|
|
|
assert.Equal(t, router.engine, router)
|
|
|
|
assert.Empty(t, router.Handlers)
|
2014-07-04 12:12:28 +04:00
|
|
|
|
2015-04-08 03:58:35 +03:00
|
|
|
// TODO
|
|
|
|
// assert.Equal(t, router.router.NotFound, router.handle404)
|
|
|
|
// assert.Equal(t, router.router.MethodNotAllowed, router.handle405)
|
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) {
|
|
|
|
middleware0 := func(c *Context) {}
|
|
|
|
middleware1 := 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)
|
|
|
|
assert.Equal(t, router.noRoute[0], middleware0)
|
|
|
|
assert.Equal(t, router.allNoRoute[0], middleware0)
|
|
|
|
|
|
|
|
router.NoRoute(middleware1, middleware0)
|
|
|
|
assert.Len(t, router.noRoute, 2)
|
|
|
|
assert.Len(t, router.allNoRoute, 2)
|
|
|
|
assert.Equal(t, router.noRoute[0], middleware1)
|
|
|
|
assert.Equal(t, router.allNoRoute[0], middleware1)
|
|
|
|
assert.Equal(t, router.noRoute[1], middleware0)
|
|
|
|
assert.Equal(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) {
|
|
|
|
middleware0 := func(c *Context) {}
|
|
|
|
middleware1 := func(c *Context) {}
|
|
|
|
middleware2 := func(c *Context) {}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
assert.Equal(t, router.Handlers[0], middleware2)
|
|
|
|
assert.Equal(t, router.noRoute[0], middleware0)
|
|
|
|
assert.Equal(t, router.allNoRoute[0], middleware2)
|
|
|
|
assert.Equal(t, router.allNoRoute[1], middleware0)
|
|
|
|
|
|
|
|
router.Use(middleware1)
|
|
|
|
assert.Len(t, router.allNoRoute, 3)
|
|
|
|
assert.Len(t, router.Handlers, 2)
|
|
|
|
assert.Len(t, router.noRoute, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, router.Handlers[0], middleware2)
|
|
|
|
assert.Equal(t, router.Handlers[1], middleware1)
|
|
|
|
assert.Equal(t, router.noRoute[0], middleware0)
|
|
|
|
assert.Equal(t, router.allNoRoute[0], middleware2)
|
|
|
|
assert.Equal(t, router.allNoRoute[1], middleware1)
|
|
|
|
assert.Equal(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) {
|
|
|
|
middleware0 := func(c *Context) {}
|
|
|
|
middleware1 := 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)
|
|
|
|
assert.Equal(t, router.noMethod[0], middleware0)
|
|
|
|
assert.Equal(t, router.allNoMethod[0], middleware0)
|
|
|
|
|
|
|
|
router.NoMethod(middleware1, middleware0)
|
|
|
|
assert.Len(t, router.noMethod, 2)
|
|
|
|
assert.Len(t, router.allNoMethod, 2)
|
|
|
|
assert.Equal(t, router.noMethod[0], middleware1)
|
|
|
|
assert.Equal(t, router.allNoMethod[0], middleware1)
|
|
|
|
assert.Equal(t, router.noMethod[1], middleware0)
|
|
|
|
assert.Equal(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) {
|
|
|
|
middleware0 := func(c *Context) {}
|
|
|
|
middleware1 := func(c *Context) {}
|
|
|
|
middleware2 := func(c *Context) {}
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
assert.Equal(t, router.Handlers[0], middleware2)
|
|
|
|
assert.Equal(t, router.noMethod[0], middleware0)
|
|
|
|
assert.Equal(t, router.allNoMethod[0], middleware2)
|
|
|
|
assert.Equal(t, router.allNoMethod[1], middleware0)
|
|
|
|
|
|
|
|
router.Use(middleware1)
|
|
|
|
assert.Len(t, router.allNoMethod, 3)
|
|
|
|
assert.Len(t, router.Handlers, 2)
|
|
|
|
assert.Len(t, router.noMethod, 1)
|
|
|
|
|
|
|
|
assert.Equal(t, router.Handlers[0], middleware2)
|
|
|
|
assert.Equal(t, router.Handlers[1], middleware1)
|
|
|
|
assert.Equal(t, router.noMethod[0], middleware0)
|
|
|
|
assert.Equal(t, router.allNoMethod[0], middleware2)
|
|
|
|
assert.Equal(t, router.allNoMethod[1], middleware1)
|
|
|
|
assert.Equal(t, router.allNoMethod[2], middleware0)
|
2014-07-28 14:05:23 +04:00
|
|
|
}
|