|
|
@@ -5,6 +5,7 @@
|
|
|
package gin
|
|
|
|
|
|
import (
|
|
|
+ "fmt"
|
|
|
"io/ioutil"
|
|
|
"net/http"
|
|
|
"net/http/httptest"
|
|
|
@@ -110,18 +111,28 @@ func TestRouteNotOK2(t *testing.T) {
|
|
|
func TestRouteParamsByName(t *testing.T) {
|
|
|
name := ""
|
|
|
lastName := ""
|
|
|
+ wild := ""
|
|
|
router := New()
|
|
|
- router.GET("/test/:name/:last_name", func(c *Context) {
|
|
|
+ router.GET("/test/:name/:last_name/*wild", func(c *Context) {
|
|
|
name = c.Params.ByName("name")
|
|
|
lastName = c.Params.ByName("last_name")
|
|
|
+ wild = c.Params.ByName("wild")
|
|
|
+
|
|
|
+ assert.Equal(t, name, c.ParamValue("name"))
|
|
|
+ assert.Equal(t, lastName, c.ParamValue("last_name"))
|
|
|
+
|
|
|
+ assert.Equal(t, name, c.DefaultParamValue("name", "nothing"))
|
|
|
+ assert.Equal(t, lastName, c.DefaultParamValue("last_name", "nothing"))
|
|
|
+ assert.Equal(t, c.DefaultParamValue("noKey", "default"), "default")
|
|
|
})
|
|
|
// RUN
|
|
|
- w := performRequest(router, "GET", "/test/john/smith")
|
|
|
+ w := performRequest(router, "GET", "/test/john/smith/is/super/great")
|
|
|
|
|
|
// TEST
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
assert.Equal(t, name, "john")
|
|
|
assert.Equal(t, lastName, "smith")
|
|
|
+ assert.Equal(t, wild, "/is/super/great")
|
|
|
}
|
|
|
|
|
|
// TestHandleStaticFile - ensure the static file handles properly
|
|
|
@@ -183,3 +194,70 @@ func TestRouteHeadToDir(t *testing.T) {
|
|
|
assert.Contains(t, bodyAsString, "gin.go")
|
|
|
assert.Equal(t, w.HeaderMap.Get("Content-Type"), "text/html; charset=utf-8")
|
|
|
}
|
|
|
+
|
|
|
+func TestRouteNotAllowed(t *testing.T) {
|
|
|
+ router := New()
|
|
|
+
|
|
|
+ router.POST("/path", func(c *Context) {})
|
|
|
+ w := performRequest(router, "GET", "/path")
|
|
|
+ assert.Equal(t, w.Code, http.StatusMethodNotAllowed)
|
|
|
+
|
|
|
+ router.NoMethod(func(c *Context) {
|
|
|
+ c.String(http.StatusTeapot, "responseText")
|
|
|
+ })
|
|
|
+ w = performRequest(router, "GET", "/path")
|
|
|
+ assert.Equal(t, w.Body.String(), "responseText")
|
|
|
+ assert.Equal(t, w.Code, http.StatusTeapot)
|
|
|
+}
|
|
|
+
|
|
|
+func TestRouterNotFound(t *testing.T) {
|
|
|
+ router := New()
|
|
|
+ router.GET("/path", func(c *Context) {})
|
|
|
+ router.GET("/dir/", func(c *Context) {})
|
|
|
+ router.GET("/", func(c *Context) {})
|
|
|
+
|
|
|
+ testRoutes := []struct {
|
|
|
+ route string
|
|
|
+ code int
|
|
|
+ header string
|
|
|
+ }{
|
|
|
+ {"/path/", 301, "map[Location:[/path]]"}, // TSR -/
|
|
|
+ {"/dir", 301, "map[Location:[/dir/]]"}, // TSR +/
|
|
|
+ {"", 301, "map[Location:[/]]"}, // TSR +/
|
|
|
+ {"/PATH", 301, "map[Location:[/path]]"}, // Fixed Case
|
|
|
+ {"/DIR/", 301, "map[Location:[/dir/]]"}, // Fixed Case
|
|
|
+ {"/PATH/", 301, "map[Location:[/path]]"}, // Fixed Case -/
|
|
|
+ {"/DIR", 301, "map[Location:[/dir/]]"}, // Fixed Case +/
|
|
|
+ {"/../path", 301, "map[Location:[/path]]"}, // CleanPath
|
|
|
+ {"/nope", 404, ""}, // NotFound
|
|
|
+ }
|
|
|
+ for _, tr := range testRoutes {
|
|
|
+ w := performRequest(router, "GET", tr.route)
|
|
|
+ assert.Equal(t, w.Code, tr.code)
|
|
|
+ if w.Code != 404 {
|
|
|
+ assert.Equal(t, fmt.Sprint(w.Header()), tr.header)
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // Test custom not found handler
|
|
|
+ var notFound bool
|
|
|
+ router.NoRoute(func(c *Context) {
|
|
|
+ c.AbortWithStatus(404)
|
|
|
+ notFound = true
|
|
|
+ })
|
|
|
+ w := performRequest(router, "GET", "/nope")
|
|
|
+ assert.Equal(t, w.Code, 404)
|
|
|
+ assert.True(t, notFound)
|
|
|
+
|
|
|
+ // Test other method than GET (want 307 instead of 301)
|
|
|
+ router.PATCH("/path", func(c *Context) {})
|
|
|
+ w = performRequest(router, "PATCH", "/path/")
|
|
|
+ assert.Equal(t, w.Code, 307)
|
|
|
+ assert.Equal(t, fmt.Sprint(w.Header()), "map[Location:[/path]]")
|
|
|
+
|
|
|
+ // Test special case where no node for the prefix "/" exists
|
|
|
+ router = New()
|
|
|
+ router.GET("/a", func(c *Context) {})
|
|
|
+ w = performRequest(router, "GET", "/")
|
|
|
+ assert.Equal(t, w.Code, 404)
|
|
|
+}
|