mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-22 09:34:33 +08:00
Add test to check if the route is in the context
This commit is contained in:
parent
57460a99d1
commit
37c19a17ab
39
gin_test.go
39
gin_test.go
@ -483,3 +483,42 @@ func assertRoutePresent(t *testing.T, gotRoutes RoutesInfo, wantRoute RouteInfo)
|
||||
|
||||
func handlerTest1(c *Context) {}
|
||||
func handlerTest2(c *Context) {}
|
||||
|
||||
func TestRegistredRouteInContext(t *testing.T) {
|
||||
tt := []struct {
|
||||
Route string
|
||||
Paths []string
|
||||
}{
|
||||
{"/", []string{"/"}},
|
||||
{"/user", []string{"/user"}},
|
||||
{"/user/:id", []string{"/user/123", "/user/abc"}},
|
||||
{"/user/:id/profile/:p_id", []string{"/user/123/profile/123", "/user/abc/profile/abc"}},
|
||||
}
|
||||
|
||||
r := New()
|
||||
h := func(p string) HandlerFunc {
|
||||
return func(c *Context) {
|
||||
if p == c.Route {
|
||||
c.JSON(http.StatusOK, H{"message": "route matched"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, H{
|
||||
"message": "route missmatch",
|
||||
"expected": p,
|
||||
"found": c.Route,
|
||||
})
|
||||
}
|
||||
}
|
||||
for _, tc := range tt {
|
||||
r.GET(tc.Route, h(tc.Route))
|
||||
}
|
||||
|
||||
for _, tc := range tt {
|
||||
for _, path := range tc.Paths {
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", path, nil)
|
||||
r.ServeHTTP(w, req)
|
||||
assert.Equal(t, 200, w.Code, w.Body.String())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user