2015-03-27 23:35:15 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
2015-03-30 16:38:53 +02:00
|
|
|
"bytes"
|
2018-04-03 13:09:21 +02:00
|
|
|
stdContext "context"
|
2018-02-21 20:44:17 +02:00
|
|
|
"errors"
|
2015-03-27 23:35:15 +02:00
|
|
|
"net/http"
|
2016-09-23 07:53:44 +02:00
|
|
|
"net/http/httptest"
|
2015-05-30 01:00:02 +02:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
2018-02-21 20:44:17 +02:00
|
|
|
"testing"
|
2017-01-18 22:17:44 +02:00
|
|
|
"time"
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
user struct {
|
2017-08-15 06:48:19 +02:00
|
|
|
ID int `json:"id" xml:"id" form:"id" query:"id"`
|
2016-12-10 18:38:27 +02:00
|
|
|
Name string `json:"name" xml:"name" form:"name" query:"name"`
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2016-05-01 21:38:51 +02:00
|
|
|
const (
|
2018-09-28 19:41:13 +02:00
|
|
|
userJSON = `{"id":1,"name":"Jon Snow"}`
|
|
|
|
userXML = `<user><id>1</id><name>Jon Snow</name></user>`
|
|
|
|
userForm = `id=1&name=Jon Snow`
|
|
|
|
invalidContent = "invalid content"
|
|
|
|
userJSONInvalidType = `{"id":"1","name":"Jon Snow"}`
|
|
|
|
userXMLConvertNumberError = `<user><id>Number one</id><name>Jon Snow</name></user>`
|
|
|
|
userXMLUnsupportedTypeError = `<user><>Number one</><name>Jon Snow</name></user>`
|
2016-05-01 21:38:51 +02:00
|
|
|
)
|
|
|
|
|
2016-12-09 21:13:55 +02:00
|
|
|
const userJSONPretty = `{
|
2017-04-28 06:41:46 +02:00
|
|
|
"id": 1,
|
|
|
|
"name": "Jon Snow"
|
2016-12-09 21:13:55 +02:00
|
|
|
}`
|
|
|
|
|
|
|
|
const userXMLPretty = `<user>
|
2017-04-28 06:41:46 +02:00
|
|
|
<id>1</id>
|
|
|
|
<name>Jon Snow</name>
|
2016-12-09 21:13:55 +02:00
|
|
|
</user>`
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
func TestEcho(t *testing.T) {
|
|
|
|
e := New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(GET, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
2016-04-24 19:21:23 +02:00
|
|
|
c := e.NewContext(req, rec)
|
2015-05-31 00:20:36 +02:00
|
|
|
|
|
|
|
// Router
|
|
|
|
assert.NotNil(t, e.Router())
|
|
|
|
|
|
|
|
// DefaultHTTPErrorHandler
|
|
|
|
e.DefaultHTTPErrorHandler(errors.New("error"), c)
|
2016-09-23 07:53:44 +02:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
2015-05-31 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
2016-03-12 15:14:15 +02:00
|
|
|
func TestEchoStatic(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
|
|
|
// OK
|
|
|
|
e.Static("/images", "_fixture/images")
|
|
|
|
c, b := request(GET, "/images/walle.png", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.NotEmpty(t, b)
|
|
|
|
|
|
|
|
// No file
|
|
|
|
e.Static("/images", "_fixture/scripts")
|
|
|
|
c, _ = request(GET, "/images/bolt.png", e)
|
|
|
|
assert.Equal(t, http.StatusNotFound, c)
|
|
|
|
|
|
|
|
// Directory
|
|
|
|
e.Static("/images", "_fixture/images")
|
|
|
|
c, _ = request(GET, "/images", e)
|
|
|
|
assert.Equal(t, http.StatusNotFound, c)
|
|
|
|
|
|
|
|
// Directory with index.html
|
|
|
|
e.Static("/", "_fixture")
|
|
|
|
c, r := request(GET, "/", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.Equal(t, true, strings.HasPrefix(r, "<!doctype html>"))
|
|
|
|
|
|
|
|
// Sub-directory with index.html
|
|
|
|
c, r = request(GET, "/folder", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.Equal(t, true, strings.HasPrefix(r, "<!doctype html>"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoFile(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
e.File("/walle", "_fixture/images/walle.png")
|
|
|
|
c, b := request(GET, "/walle", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.NotEmpty(t, b)
|
|
|
|
}
|
|
|
|
|
2015-03-30 05:44:55 +02:00
|
|
|
func TestEchoMiddleware(t *testing.T) {
|
2015-03-30 08:35:08 +02:00
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
buf := new(bytes.Buffer)
|
2015-03-30 05:44:55 +02:00
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Pre(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
assert.Empty(t, c.Path())
|
|
|
|
buf.WriteString("-1")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-03-17 22:24:52 +02:00
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("1")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-03-17 22:24:52 +02:00
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("2")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-03-17 22:24:52 +02:00
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("3")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2015-03-30 08:35:08 +02:00
|
|
|
|
2015-03-30 05:44:55 +02:00
|
|
|
// Route
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/", func(c Context) error {
|
2016-01-09 19:44:18 +02:00
|
|
|
return c.String(http.StatusOK, "OK")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-03-30 05:44:55 +02:00
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
c, b := request(GET, "/", e)
|
2016-03-17 22:24:52 +02:00
|
|
|
assert.Equal(t, "-1123", buf.String())
|
2015-05-31 00:20:36 +02:00
|
|
|
assert.Equal(t, http.StatusOK, c)
|
2016-01-09 19:44:18 +02:00
|
|
|
assert.Equal(t, "OK", b)
|
2016-03-17 22:24:52 +02:00
|
|
|
}
|
2015-05-31 00:20:36 +02:00
|
|
|
|
2016-03-17 22:24:52 +02:00
|
|
|
func TestEchoMiddlewareError(t *testing.T) {
|
|
|
|
e := New()
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
return errors.New("error")
|
|
|
|
}
|
|
|
|
})
|
2016-07-05 17:43:46 +02:00
|
|
|
e.GET("/", NotFoundHandler)
|
2016-03-17 22:24:52 +02:00
|
|
|
c, _ := request(GET, "/", e)
|
2015-05-31 00:20:36 +02:00
|
|
|
assert.Equal(t, http.StatusInternalServerError, c)
|
2015-03-30 08:35:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoHandler(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
|
2015-04-29 20:22:07 +02:00
|
|
|
// HandlerFunc
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/ok", func(c Context) error {
|
2016-01-09 19:44:18 +02:00
|
|
|
return c.String(http.StatusOK, "OK")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2016-01-09 19:44:18 +02:00
|
|
|
c, b := request(GET, "/ok", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.Equal(t, "OK", b)
|
2015-03-30 05:44:55 +02:00
|
|
|
}
|
|
|
|
|
2016-09-23 14:31:48 +02:00
|
|
|
func TestEchoWrapHandler(t *testing.T) {
|
|
|
|
e := New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(GET, "/", nil)
|
2016-09-23 14:31:48 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
h := WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
w.Write([]byte("test"))
|
|
|
|
}))
|
|
|
|
if assert.NoError(t, h(c)) {
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "test", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoWrapMiddleware(t *testing.T) {
|
|
|
|
e := New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(GET, "/", nil)
|
2016-09-23 14:31:48 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := e.NewContext(req, rec)
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
mw := WrapMiddleware(func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
buf.Write([]byte("mw"))
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
h := mw(func(c Context) error {
|
|
|
|
return c.String(http.StatusOK, "OK")
|
|
|
|
})
|
|
|
|
if assert.NoError(t, h(c)) {
|
|
|
|
assert.Equal(t, "mw", buf.String())
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
assert.Equal(t, "OK", rec.Body.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-30 01:00:02 +02:00
|
|
|
func TestEchoConnect(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
testMethod(t, CONNECT, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoDelete(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
testMethod(t, DELETE, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoGet(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
testMethod(t, GET, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoHead(t *testing.T) {
|
2015-04-06 00:30:03 +02:00
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
testMethod(t, HEAD, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoOptions(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
testMethod(t, OPTIONS, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoPatch(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
testMethod(t, PATCH, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoPost(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
testMethod(t, POST, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoPut(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
testMethod(t, PUT, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoTrace(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
testMethod(t, TRACE, "/", e)
|
2015-05-30 01:00:02 +02:00
|
|
|
}
|
|
|
|
|
2015-08-26 05:36:15 +02:00
|
|
|
func TestEchoAny(t *testing.T) { // JFC
|
|
|
|
e := New()
|
2016-04-02 23:19:39 +02:00
|
|
|
e.Any("/", func(c Context) error {
|
2015-08-26 05:36:15 +02:00
|
|
|
return c.String(http.StatusOK, "Any")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoMatch(t *testing.T) { // JFC
|
|
|
|
e := New()
|
2016-04-02 23:19:39 +02:00
|
|
|
e.Match([]string{GET, POST}, "/", func(c Context) error {
|
2015-08-26 05:36:15 +02:00
|
|
|
return c.String(http.StatusOK, "Match")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-08-26 05:36:15 +02:00
|
|
|
}
|
|
|
|
|
2015-04-22 07:12:41 +02:00
|
|
|
func TestEchoURL(t *testing.T) {
|
|
|
|
e := New()
|
2016-04-02 23:19:39 +02:00
|
|
|
static := func(Context) error { return nil }
|
|
|
|
getUser := func(Context) error { return nil }
|
|
|
|
getFile := func(Context) error { return nil }
|
2015-05-31 18:37:26 +02:00
|
|
|
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/static/file", static)
|
|
|
|
e.GET("/users/:id", getUser)
|
2015-05-31 18:37:26 +02:00
|
|
|
g := e.Group("/group")
|
2016-10-13 22:51:37 +02:00
|
|
|
g.GET("/users/:uid/files/:fid", getFile)
|
2015-05-31 18:37:26 +02:00
|
|
|
|
|
|
|
assert.Equal(t, "/static/file", e.URL(static))
|
|
|
|
assert.Equal(t, "/users/:id", e.URL(getUser))
|
|
|
|
assert.Equal(t, "/users/1", e.URL(getUser, "1"))
|
|
|
|
assert.Equal(t, "/group/users/1/files/:fid", e.URL(getFile, "1"))
|
|
|
|
assert.Equal(t, "/group/users/1/files/1", e.URL(getFile, "1", "1"))
|
2015-04-22 07:12:41 +02:00
|
|
|
}
|
|
|
|
|
2015-06-01 09:07:53 +02:00
|
|
|
func TestEchoRoutes(t *testing.T) {
|
|
|
|
e := New()
|
2017-06-20 17:58:53 +02:00
|
|
|
routes := []*Route{
|
2016-01-09 19:44:18 +02:00
|
|
|
{GET, "/users/:user/events", ""},
|
|
|
|
{GET, "/users/:user/events/public", ""},
|
|
|
|
{POST, "/repos/:owner/:repo/git/refs", ""},
|
|
|
|
{POST, "/repos/:owner/:repo/git/tags", ""},
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
for _, r := range routes {
|
2017-07-04 04:10:58 +02:00
|
|
|
e.Add(r.Method, r.Path, func(c Context) error {
|
2016-01-09 19:44:18 +02:00
|
|
|
return c.String(http.StatusOK, "OK")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 17:58:53 +02:00
|
|
|
if assert.Equal(t, len(routes), len(e.Routes())) {
|
|
|
|
for _, r := range e.Routes() {
|
|
|
|
found := false
|
|
|
|
for _, rr := range routes {
|
|
|
|
if r.Method == rr.Method && r.Path == rr.Path {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !found {
|
|
|
|
t.Errorf("Route %s %s not found", r.Method, r.Path)
|
2016-06-01 14:35:59 +02:00
|
|
|
}
|
|
|
|
}
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-07 17:23:43 +02:00
|
|
|
func TestEchoEncodedPath(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
e.GET("/:id", func(c Context) error {
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
|
|
})
|
|
|
|
req := httptest.NewRequest(GET, "/with%2Fslash", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
|
|
}
|
|
|
|
|
2015-06-01 09:07:53 +02:00
|
|
|
func TestEchoGroup(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
buf := new(bytes.Buffer)
|
2016-09-23 14:31:48 +02:00
|
|
|
e.Use(MiddlewareFunc(func(next HandlerFunc) HandlerFunc {
|
2016-04-02 23:19:39 +02:00
|
|
|
return func(c Context) error {
|
2016-01-29 09:46:11 +02:00
|
|
|
buf.WriteString("0")
|
2016-09-23 14:31:48 +02:00
|
|
|
return next(c)
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2016-02-16 03:12:15 +02:00
|
|
|
}))
|
2016-04-02 23:19:39 +02:00
|
|
|
h := func(c Context) error {
|
2016-01-29 09:46:11 +02:00
|
|
|
return c.NoContent(http.StatusOK)
|
2016-04-02 23:19:39 +02:00
|
|
|
}
|
2015-06-01 09:07:53 +02:00
|
|
|
|
|
|
|
//--------
|
|
|
|
// Routes
|
|
|
|
//--------
|
|
|
|
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/users", h)
|
2015-06-01 09:07:53 +02:00
|
|
|
|
|
|
|
// Group
|
|
|
|
g1 := e.Group("/group1")
|
2016-09-23 14:31:48 +02:00
|
|
|
g1.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("1")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-10-13 22:51:37 +02:00
|
|
|
g1.GET("", h)
|
2015-06-01 09:07:53 +02:00
|
|
|
|
2016-03-17 05:15:38 +02:00
|
|
|
// Nested groups with middleware
|
2016-02-15 18:11:29 +02:00
|
|
|
g2 := e.Group("/group2")
|
2016-09-23 14:31:48 +02:00
|
|
|
g2.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("2")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-02-15 18:11:29 +02:00
|
|
|
g3 := g2.Group("/group3")
|
2016-09-23 14:31:48 +02:00
|
|
|
g3.Use(func(next HandlerFunc) HandlerFunc {
|
|
|
|
return func(c Context) error {
|
|
|
|
buf.WriteString("3")
|
|
|
|
return next(c)
|
|
|
|
}
|
|
|
|
})
|
2016-10-13 22:51:37 +02:00
|
|
|
g3.GET("", h)
|
2015-06-01 09:07:53 +02:00
|
|
|
|
|
|
|
request(GET, "/users", e)
|
|
|
|
assert.Equal(t, "0", buf.String())
|
|
|
|
|
|
|
|
buf.Reset()
|
2016-03-09 05:31:11 +02:00
|
|
|
request(GET, "/group1", e)
|
2015-06-01 09:07:53 +02:00
|
|
|
assert.Equal(t, "01", buf.String())
|
|
|
|
|
|
|
|
buf.Reset()
|
2016-03-17 05:15:38 +02:00
|
|
|
request(GET, "/group2/group3", e)
|
|
|
|
assert.Equal(t, "023", buf.String())
|
2015-06-01 09:07:53 +02:00
|
|
|
}
|
|
|
|
|
2015-04-06 05:08:52 +02:00
|
|
|
func TestEchoNotFound(t *testing.T) {
|
2015-03-31 05:54:38 +02:00
|
|
|
e := New()
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(GET, "/files", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusNotFound, rec.Code)
|
2015-03-31 05:54:38 +02:00
|
|
|
}
|
|
|
|
|
2015-10-06 15:48:33 +02:00
|
|
|
func TestEchoMethodNotAllowed(t *testing.T) {
|
2015-11-04 02:26:20 +02:00
|
|
|
e := New()
|
2016-04-19 01:59:58 +02:00
|
|
|
e.GET("/", func(c Context) error {
|
2015-11-04 02:26:20 +02:00
|
|
|
return c.String(http.StatusOK, "Echo!")
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(POST, "/", nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
|
2015-07-24 21:03:36 +02:00
|
|
|
}
|
|
|
|
|
2016-06-07 01:54:24 +02:00
|
|
|
func TestEchoContext(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
c := e.AcquireContext()
|
2016-10-11 02:31:26 +02:00
|
|
|
assert.IsType(t, new(context), c)
|
2016-06-07 01:54:24 +02:00
|
|
|
e.ReleaseContext(c)
|
|
|
|
}
|
|
|
|
|
2016-09-25 02:48:40 +02:00
|
|
|
func TestEchoStart(t *testing.T) {
|
2016-09-25 01:19:38 +02:00
|
|
|
e := New()
|
2016-09-25 02:48:40 +02:00
|
|
|
go func() {
|
2016-09-25 21:14:50 +02:00
|
|
|
assert.NoError(t, e.Start(":0"))
|
2016-09-25 02:48:40 +02:00
|
|
|
}()
|
2017-01-18 22:17:44 +02:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2016-09-25 02:48:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoStartTLS(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
go func() {
|
2018-03-01 19:27:31 +02:00
|
|
|
err := e.StartTLS(":0", "_fixture/certs/cert.pem", "_fixture/certs/key.pem")
|
|
|
|
// Prevent the test to fail after closing the servers
|
|
|
|
if err != http.ErrServerClosed {
|
|
|
|
assert.NoError(t, err)
|
|
|
|
}
|
2016-09-25 02:48:40 +02:00
|
|
|
}()
|
2017-01-18 22:17:44 +02:00
|
|
|
time.Sleep(200 * time.Millisecond)
|
2018-03-01 19:27:31 +02:00
|
|
|
|
|
|
|
e.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoStartAutoTLS(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
errChan := make(chan error, 0)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
errChan <- e.StartAutoTLS(":0")
|
|
|
|
}()
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errChan:
|
|
|
|
assert.NoError(t, err)
|
|
|
|
default:
|
|
|
|
assert.NoError(t, e.Close())
|
|
|
|
}
|
2016-09-25 01:19:38 +02:00
|
|
|
}
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
func testMethod(t *testing.T, method, path string, e *Echo) {
|
|
|
|
p := reflect.ValueOf(path)
|
2016-04-02 23:19:39 +02:00
|
|
|
h := reflect.ValueOf(func(c Context) error {
|
2015-07-24 21:03:36 +02:00
|
|
|
return c.String(http.StatusOK, method)
|
2016-04-02 23:19:39 +02:00
|
|
|
})
|
2015-05-31 00:20:36 +02:00
|
|
|
i := interface{}(e)
|
2016-09-23 07:53:44 +02:00
|
|
|
reflect.ValueOf(i).MethodByName(method).Call([]reflect.Value{p, h})
|
2015-05-31 00:20:36 +02:00
|
|
|
_, body := request(method, path, e)
|
2017-06-20 17:58:53 +02:00
|
|
|
assert.Equal(t, method, body)
|
2015-05-31 00:20:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func request(method, path string, e *Echo) (int, string) {
|
2017-02-23 01:57:12 +02:00
|
|
|
req := httptest.NewRequest(method, path, nil)
|
2016-09-23 07:53:44 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(rec, req)
|
|
|
|
return rec.Code, rec.Body.String()
|
2016-04-27 14:57:35 +02:00
|
|
|
}
|
2017-06-20 17:58:53 +02:00
|
|
|
|
|
|
|
func TestHTTPError(t *testing.T) {
|
2018-08-29 03:40:40 +02:00
|
|
|
err := NewHTTPError(http.StatusBadRequest, map[string]interface{}{
|
2017-06-20 17:58:53 +02:00
|
|
|
"code": 12,
|
|
|
|
})
|
|
|
|
assert.Equal(t, "code=400, message=map[code:12]", err.Error())
|
|
|
|
}
|
2018-04-03 13:09:21 +02:00
|
|
|
|
|
|
|
func TestEchoClose(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
errCh <- e.Start(":0")
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
if err := e.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NoError(t, e.Close())
|
|
|
|
|
|
|
|
err := <-errCh
|
|
|
|
assert.Equal(t, err.Error(), "http: Server closed")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoShutdown(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
errCh <- e.Start(":0")
|
|
|
|
}()
|
|
|
|
|
|
|
|
time.Sleep(200 * time.Millisecond)
|
|
|
|
|
|
|
|
if err := e.Close(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := stdContext.WithTimeout(stdContext.Background(), 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
assert.NoError(t, e.Shutdown(ctx))
|
|
|
|
|
|
|
|
err := <-errCh
|
|
|
|
assert.Equal(t, err.Error(), "http: Server closed")
|
|
|
|
}
|