2015-05-30 01:00:02 +02:00
|
|
|
package echo
|
|
|
|
|
2016-06-14 03:50:17 +02:00
|
|
|
import (
|
2020-04-25 19:58:16 +02:00
|
|
|
"io/ioutil"
|
2018-10-14 17:16:58 +02:00
|
|
|
"net/http"
|
2020-04-25 19:58:16 +02:00
|
|
|
"net/http/httptest"
|
2016-06-14 03:50:17 +02:00
|
|
|
"testing"
|
2016-09-23 14:31:48 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2016-06-14 03:50:17 +02:00
|
|
|
)
|
2015-05-30 01:00:02 +02:00
|
|
|
|
2016-06-07 01:54:24 +02:00
|
|
|
// TODO: Fix me
|
2015-05-31 00:20:36 +02:00
|
|
|
func TestGroup(t *testing.T) {
|
|
|
|
g := New().Group("/group")
|
2016-04-02 23:19:39 +02:00
|
|
|
h := func(Context) error { return nil }
|
2016-04-20 16:32:51 +02:00
|
|
|
g.CONNECT("/", h)
|
|
|
|
g.DELETE("/", h)
|
|
|
|
g.GET("/", h)
|
|
|
|
g.HEAD("/", h)
|
|
|
|
g.OPTIONS("/", h)
|
|
|
|
g.PATCH("/", h)
|
|
|
|
g.POST("/", h)
|
|
|
|
g.PUT("/", h)
|
|
|
|
g.TRACE("/", h)
|
2016-04-09 23:00:23 +02:00
|
|
|
g.Any("/", h)
|
2018-10-14 17:16:58 +02:00
|
|
|
g.Match([]string{http.MethodGet, http.MethodPost}, "/", h)
|
2016-04-09 23:00:23 +02:00
|
|
|
g.Static("/static", "/tmp")
|
|
|
|
g.File("/walle", "_fixture/images//walle.png")
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
2016-06-14 03:50:17 +02:00
|
|
|
|
2020-04-25 19:58:16 +02:00
|
|
|
func TestGroupFile(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
g := e.Group("/group")
|
|
|
|
g.File("/walle", "_fixture/images/walle.png")
|
|
|
|
expectedData, err := ioutil.ReadFile("_fixture/images/walle.png")
|
|
|
|
assert.Nil(t, err)
|
|
|
|
req := httptest.NewRequest(http.MethodGet, "/group/walle", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, expectedData, rec.Body.Bytes())
|
|
|
|
}
|
|
|
|
|
2016-06-14 03:50:17 +02:00
|
|
|
func TestGroupRouteMiddleware(t *testing.T) {
|
|
|
|
// Ensure middleware slices are not re-used
|
|
|
|
e := New()
|
|
|
|
g := e.Group("/group")
|
|
|
|
h := func(Context) error { return nil }
|
2016-09-23 14:31:48 +02:00
|
|
|
m1 := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m2 := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m3 := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m4 := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return c.NoContent(404)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m5 := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return c.NoContent(405)
|
|
|
|
}
|
|
|
|
}
|
2016-06-14 03:50:17 +02:00
|
|
|
g.Use(m1, m2, m3)
|
|
|
|
g.GET("/404", h, m4)
|
|
|
|
g.GET("/405", h, m5)
|
|
|
|
|
2018-10-14 17:16:58 +02:00
|
|
|
c, _ := request(http.MethodGet, "/group/404", e)
|
2016-06-14 03:50:17 +02:00
|
|
|
assert.Equal(t, 404, c)
|
2018-10-14 17:16:58 +02:00
|
|
|
c, _ = request(http.MethodGet, "/group/405", e)
|
2016-06-14 03:50:17 +02:00
|
|
|
assert.Equal(t, 405, c)
|
|
|
|
}
|
2019-10-04 01:15:54 +02:00
|
|
|
|
|
|
|
func TestGroupRouteMiddlewareWithMatchAny(t *testing.T) {
|
|
|
|
// Ensure middleware and match any routes do not conflict
|
|
|
|
e := New()
|
|
|
|
g := e.Group("/group")
|
|
|
|
m1 := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m2 := func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, c.Path())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
h := func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, c.Path())
|
|
|
|
}
|
|
|
|
g.Use(m1)
|
|
|
|
g.GET("/help", h, m2)
|
|
|
|
g.GET("/*", h, m2)
|
|
|
|
g.GET("", h, m2)
|
2019-10-04 19:58:52 +02:00
|
|
|
e.GET("unrelated", h, m2)
|
2019-10-04 01:15:54 +02:00
|
|
|
e.GET("*", h, m2)
|
|
|
|
|
|
|
|
_, m := request(http.MethodGet, "/group/help", e)
|
|
|
|
assert.Equal(t, "/group/help", m)
|
|
|
|
_, m = request(http.MethodGet, "/group/help/other", e)
|
|
|
|
assert.Equal(t, "/group/*", m)
|
|
|
|
_, m = request(http.MethodGet, "/group/404", e)
|
|
|
|
assert.Equal(t, "/group/*", m)
|
|
|
|
_, m = request(http.MethodGet, "/group", e)
|
|
|
|
assert.Equal(t, "/group", m)
|
|
|
|
_, m = request(http.MethodGet, "/other", e)
|
|
|
|
assert.Equal(t, "/*", m)
|
|
|
|
_, m = request(http.MethodGet, "/", e)
|
|
|
|
assert.Equal(t, "/*", m)
|
|
|
|
|
|
|
|
}
|