-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf1dc5b
commit af38a26
Showing
3 changed files
with
173 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,147 @@ | ||
package cotton | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGroup(t *testing.T) { | ||
router := NewRouter() | ||
g1 := router.Group("/v1") | ||
g1.Get("/a", func(c *Context) { | ||
c.String(http.StatusOK, "g1 a") | ||
}) | ||
g1.Get("/b", func(c *Context) { | ||
c.String(http.StatusBadGateway, "g1 b") | ||
}) | ||
|
||
w := doRequest(router, http.MethodGet, "/v1/a") | ||
|
||
assert.Equal(t, http.StatusOK, w.Code) | ||
assert.Equal(t, "g1 a", w.Body.String()) | ||
|
||
w = doRequest(router, http.MethodGet, "/v1/b") | ||
|
||
assert.Equal(t, http.StatusBadGateway, w.Code) | ||
assert.Equal(t, "g1 b", w.Body.String()) | ||
// assert.True(t, false) | ||
} | ||
func TestGroupPrefix(t *testing.T) { | ||
router := NewRouter() | ||
g1 := router.Group("/v1") | ||
g1.Get("/a", func(c *Context) { | ||
c.String(http.StatusOK, "g1 a") | ||
}) | ||
|
||
w := doRequest(router, http.MethodGet, "/v1/v1/a") | ||
|
||
assert.Equal(t, http.StatusNotFound, w.Code) | ||
} | ||
func TestGroupPanic(t *testing.T) { | ||
assert.PanicsWithError(t, "group [] must start with /", func() { | ||
router := NewRouter() | ||
router.Group("") | ||
}) | ||
assert.PanicsWithError(t, "group [abc] must start with /", func() { | ||
router := NewRouter() | ||
router.Group("abc") | ||
}) | ||
|
||
assert.PanicsWithError(t, "group [/a/] conflicts with [/a/]", func() { | ||
router := NewRouter() | ||
router.Group("/a") | ||
router.Group("/a") | ||
}) | ||
|
||
assert.PanicsWithError(t, "group path [/:method] can not has parameter", func() { | ||
router := NewRouter() | ||
router.Group("/:method") | ||
}) | ||
|
||
assert.NotPanics(t, func() { | ||
router := NewRouter() | ||
router.Group("/s") | ||
router.Group("/static") | ||
}) | ||
} | ||
|
||
func TestMatchGroup(t *testing.T) { | ||
assert.True(t, matchGroup(&Router{ | ||
prefix: "/v1/", | ||
}, "/v1/test")) | ||
|
||
assert.False(t, matchGroup(&Router{ | ||
prefix: "/v1/", | ||
}, "/v2/test")) | ||
} | ||
|
||
func TestCustomGroupNotFound(t *testing.T) { | ||
router := NewRouter() | ||
|
||
infoCustomNotFound := "not found from custom" | ||
infoCustomGroupNotFound := "not found from custom group" | ||
infoCustomGroupUserNotFound := "not found from custom group user" | ||
router.NotFound(func(ctx *Context) { | ||
ctx.String(http.StatusNotFound, infoCustomNotFound) | ||
}) | ||
|
||
g := router.Group("/v1") | ||
g.NotFound(func(ctx *Context) { | ||
ctx.String(http.StatusNotFound, infoCustomGroupNotFound) | ||
}) | ||
|
||
w := doRequest(router, http.MethodGet, "/path404") | ||
assert.Equal(t, http.StatusNotFound, w.Code) | ||
assert.Equal(t, infoCustomNotFound, w.Body.String()) | ||
|
||
w = doRequest(router, http.MethodGet, "/v1/path404") | ||
|
||
assert.Equal(t, http.StatusNotFound, w.Code) | ||
assert.Equal(t, infoCustomGroupNotFound, w.Body.String()) | ||
|
||
gUser := g.Group("/user") | ||
|
||
w = doRequest(router, http.MethodGet, "/v1/user/path404") | ||
|
||
assert.Equal(t, http.StatusNotFound, w.Code) | ||
assert.Equal(t, infoCustomGroupNotFound, w.Body.String()) | ||
|
||
gUser.NotFound(func(ctx *Context) { | ||
ctx.String(http.StatusNotFound, infoCustomGroupUserNotFound) | ||
}) | ||
|
||
w = doRequest(router, http.MethodGet, "/v1/user/path404") | ||
|
||
assert.Equal(t, http.StatusNotFound, w.Code) | ||
assert.Equal(t, infoCustomGroupUserNotFound, w.Body.String()) | ||
|
||
w = doRequest(router, http.MethodGet, "/v1/user1/path404") | ||
|
||
assert.Equal(t, http.StatusNotFound, w.Code) | ||
assert.Equal(t, infoCustomGroupNotFound, w.Body.String()) | ||
|
||
w = doRequest(router, http.MethodGet, "/v2/user1/path404") | ||
|
||
assert.Equal(t, http.StatusNotFound, w.Code) | ||
assert.Equal(t, infoCustomNotFound, w.Body.String()) | ||
} | ||
|
||
func TestGroupMulty(t *testing.T) { | ||
router := NewRouter() | ||
g1 := router.Group("/a") | ||
g1.addHandleFunc("GET", "/test", func(ctx *Context) { | ||
ctx.String(http.StatusOK, "/a/test") | ||
}) | ||
g2 := g1.Group("/b") | ||
g2.addHandleFunc("GET", "/test", func(ctx *Context) { | ||
ctx.String(http.StatusOK, "/a/b/test") | ||
}) | ||
|
||
w := doRequest(router, http.MethodGet, "/a/test") | ||
assert.Equal(t, "/a/test", w.Body.String()) | ||
|
||
w = doRequest(router, http.MethodGet, "/a/b/test") | ||
assert.Equal(t, "/a/b/test", w.Body.String()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters