2015-03-27 23:35:15 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
2015-03-30 16:38:53 +02:00
|
|
|
"bytes"
|
2015-05-22 07:48:34 +02:00
|
|
|
"fmt"
|
2015-03-27 23:35:15 +02:00
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2015-05-22 07:48:34 +02:00
|
|
|
|
2015-05-30 01:00:02 +02:00
|
|
|
"reflect"
|
|
|
|
"strings"
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
"errors"
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2015-05-22 07:48:34 +02:00
|
|
|
"golang.org/x/net/websocket"
|
2015-03-27 23:35:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
user struct {
|
2015-03-31 05:54:38 +02:00
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
func TestEcho(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
req, _ := http.NewRequest(GET, "/", nil)
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := NewContext(req, NewResponse(rec), e)
|
|
|
|
|
|
|
|
// Router
|
|
|
|
assert.NotNil(t, e.Router())
|
|
|
|
|
|
|
|
// Debug
|
|
|
|
e.SetDebug(true)
|
|
|
|
assert.True(t, e.Debug())
|
|
|
|
|
|
|
|
// DefaultHTTPErrorHandler
|
|
|
|
e.DefaultHTTPErrorHandler(errors.New("error"), c)
|
|
|
|
assert.Equal(t, http.StatusInternalServerError, rec.Code)
|
|
|
|
}
|
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
func TestEchoIndex(t *testing.T) {
|
2015-03-30 08:35:08 +02:00
|
|
|
e := New()
|
2015-05-18 22:41:50 +02:00
|
|
|
e.Index("examples/website/public/index.html")
|
2015-05-31 00:20:36 +02:00
|
|
|
c, b := request(GET, "/", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.NotEmpty(t, b)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
2015-05-14 00:20:09 +02:00
|
|
|
func TestEchoFavicon(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-18 22:41:50 +02:00
|
|
|
e.Favicon("examples/website/public/favicon.ico")
|
2015-05-31 00:20:36 +02:00
|
|
|
c, b := request(GET, "/favicon.ico", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.NotEmpty(t, b)
|
2015-05-14 00:20:09 +02:00
|
|
|
}
|
|
|
|
|
2015-03-27 23:35:15 +02:00
|
|
|
func TestEchoStatic(t *testing.T) {
|
2015-03-30 08:35:08 +02:00
|
|
|
e := New()
|
2015-05-31 00:20:36 +02:00
|
|
|
|
|
|
|
// OK
|
2015-05-18 22:41:50 +02:00
|
|
|
e.Static("/scripts", "examples/website/public/scripts")
|
2015-05-31 00:20:36 +02:00
|
|
|
c, b := request(GET, "/scripts/main.js", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.NotEmpty(t, b)
|
|
|
|
|
|
|
|
// No file
|
|
|
|
e.Static("/scripts", "examples/website/public/scripts")
|
|
|
|
c, _ = request(GET, "/scripts/index.js", e)
|
|
|
|
assert.Equal(t, http.StatusNotFound, c)
|
|
|
|
|
|
|
|
// Directory
|
|
|
|
e.Static("/scripts", "examples/website/public/scripts")
|
|
|
|
c, _ = request(GET, "/scripts", e)
|
|
|
|
assert.Equal(t, http.StatusForbidden, c)
|
2015-06-20 00:37:26 +02:00
|
|
|
|
|
|
|
// Directory with index.html
|
|
|
|
e.Static("/", "examples/website/public")
|
2015-06-20 02:39:05 +02:00
|
|
|
c, r := request(GET, "/", e)
|
2015-06-20 00:37:26 +02:00
|
|
|
assert.Equal(t, http.StatusOK, c)
|
2015-06-20 02:39:05 +02:00
|
|
|
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, "sub directory", r)
|
2015-03-27 23:35:15 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// echo.MiddlewareFunc
|
2015-05-11 06:09:28 +02:00
|
|
|
e.Use(MiddlewareFunc(func(h HandlerFunc) HandlerFunc {
|
2015-05-20 23:38:51 +02:00
|
|
|
return func(c *Context) error {
|
2015-05-31 00:20:36 +02:00
|
|
|
buf.WriteString("a")
|
2015-05-11 06:09:28 +02:00
|
|
|
return h(c)
|
|
|
|
}
|
|
|
|
}))
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2015-05-12 05:41:28 +02:00
|
|
|
// func(echo.HandlerFunc) echo.HandlerFunc
|
2015-04-19 06:46:00 +02:00
|
|
|
e.Use(func(h HandlerFunc) HandlerFunc {
|
2015-05-20 23:38:51 +02:00
|
|
|
return func(c *Context) error {
|
2015-05-31 00:20:36 +02:00
|
|
|
buf.WriteString("b")
|
2015-04-19 01:47:48 +02:00
|
|
|
return h(c)
|
2015-04-19 06:46:00 +02:00
|
|
|
}
|
2015-03-30 05:44:55 +02:00
|
|
|
})
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// echo.HandlerFunc
|
|
|
|
e.Use(HandlerFunc(func(c *Context) error {
|
2015-05-31 00:20:36 +02:00
|
|
|
buf.WriteString("c")
|
2015-05-30 19:54:55 +02:00
|
|
|
return nil
|
|
|
|
}))
|
|
|
|
|
2015-05-20 23:38:51 +02:00
|
|
|
// func(*echo.Context) error
|
|
|
|
e.Use(func(c *Context) error {
|
2015-05-31 00:20:36 +02:00
|
|
|
buf.WriteString("d")
|
2015-05-11 06:09:28 +02:00
|
|
|
return nil
|
|
|
|
})
|
2015-03-30 05:44:55 +02:00
|
|
|
|
|
|
|
// func(http.Handler) http.Handler
|
2015-04-21 08:17:21 +02:00
|
|
|
e.Use(func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-05-31 00:20:36 +02:00
|
|
|
buf.WriteString("e")
|
2015-04-21 08:17:21 +02:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
})
|
2015-03-30 05:44:55 +02:00
|
|
|
|
2015-05-11 06:09:28 +02:00
|
|
|
// http.Handler
|
|
|
|
e.Use(http.Handler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-05-31 00:20:36 +02:00
|
|
|
buf.WriteString("f")
|
2015-05-11 06:09:28 +02:00
|
|
|
})))
|
|
|
|
|
|
|
|
// http.HandlerFunc
|
|
|
|
e.Use(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-05-31 00:20:36 +02:00
|
|
|
buf.WriteString("g")
|
2015-05-11 06:09:28 +02:00
|
|
|
}))
|
|
|
|
|
2015-03-30 08:35:08 +02:00
|
|
|
// func(http.ResponseWriter, *http.Request)
|
|
|
|
e.Use(func(w http.ResponseWriter, r *http.Request) {
|
2015-05-31 00:20:36 +02:00
|
|
|
buf.WriteString("h")
|
2015-05-30 19:54:55 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
// Unknown
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
e.Use(nil)
|
2015-03-30 08:35:08 +02:00
|
|
|
})
|
|
|
|
|
2015-03-30 05:44:55 +02:00
|
|
|
// Route
|
2015-05-31 00:20:36 +02:00
|
|
|
e.Get("/", func(c *Context) error {
|
|
|
|
return c.String(http.StatusOK, "Hello!")
|
2015-03-30 05:44:55 +02:00
|
|
|
})
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
c, b := request(GET, "/", e)
|
|
|
|
assert.Equal(t, "abcdefgh", buf.String())
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.Equal(t, "Hello!", b)
|
|
|
|
|
|
|
|
// Error
|
|
|
|
e.Use(func(*Context) error {
|
|
|
|
return errors.New("error")
|
|
|
|
})
|
|
|
|
c, b = request(GET, "/", e)
|
|
|
|
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
|
2015-05-20 23:38:51 +02:00
|
|
|
e.Get("/1", HandlerFunc(func(c *Context) error {
|
2015-04-29 20:22:07 +02:00
|
|
|
return c.String(http.StatusOK, "1")
|
|
|
|
}))
|
2015-03-30 08:35:08 +02:00
|
|
|
|
2015-05-20 23:38:51 +02:00
|
|
|
// func(*echo.Context) error
|
|
|
|
e.Get("/2", func(c *Context) error {
|
2015-04-29 20:22:07 +02:00
|
|
|
return c.String(http.StatusOK, "2")
|
2015-04-19 01:47:48 +02:00
|
|
|
})
|
2015-03-30 08:35:08 +02:00
|
|
|
|
2015-05-12 05:41:28 +02:00
|
|
|
// http.Handler/http.HandlerFunc
|
|
|
|
e.Get("/3", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Write([]byte("3"))
|
|
|
|
}))
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2015-05-12 05:41:28 +02:00
|
|
|
// func(http.ResponseWriter, *http.Request)
|
|
|
|
e.Get("/4", func(w http.ResponseWriter, r *http.Request) {
|
2015-04-19 01:47:48 +02:00
|
|
|
w.Write([]byte("4"))
|
2015-05-12 05:41:28 +02:00
|
|
|
})
|
2015-05-31 00:20:36 +02:00
|
|
|
|
|
|
|
for _, p := range []string{"1", "2", "3", "4"} {
|
|
|
|
c, b := request(GET, "/"+p, e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
assert.Equal(t, p, b)
|
2015-04-19 01:47:48 +02:00
|
|
|
}
|
2015-05-30 19:54:55 +02:00
|
|
|
|
|
|
|
// Unknown
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
e.Get("/5", nil)
|
|
|
|
})
|
2015-03-30 05:44:55 +02:00
|
|
|
}
|
|
|
|
|
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-05-31 00:20:36 +02:00
|
|
|
func TestEchoWebSocket(t *testing.T) {
|
2015-05-22 07:48:34 +02:00
|
|
|
e := New()
|
|
|
|
e.WebSocket("/ws", func(c *Context) error {
|
2015-05-22 13:40:01 +02:00
|
|
|
c.socket.Write([]byte("test"))
|
2015-05-22 07:48:34 +02:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
srv := httptest.NewServer(e)
|
|
|
|
defer srv.Close()
|
|
|
|
addr := srv.Listener.Addr().String()
|
|
|
|
origin := "http://localhost"
|
|
|
|
url := fmt.Sprintf("ws://%s/ws", addr)
|
|
|
|
ws, err := websocket.Dial(url, "", origin)
|
2015-05-31 18:37:26 +02:00
|
|
|
if assert.NoError(t, err) {
|
|
|
|
ws.Write([]byte("test"))
|
|
|
|
defer ws.Close()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
buf.ReadFrom(ws)
|
|
|
|
assert.Equal(t, "test", buf.String())
|
2015-05-22 07:48:34 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-22 07:12:41 +02:00
|
|
|
func TestEchoURL(t *testing.T) {
|
|
|
|
e := New()
|
2015-05-31 18:37:26 +02:00
|
|
|
|
2015-05-20 23:38:51 +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
|
|
|
|
2015-04-22 07:12:41 +02:00
|
|
|
e.Get("/static/file", static)
|
|
|
|
e.Get("/users/:id", getUser)
|
2015-05-31 18:37:26 +02:00
|
|
|
g := e.Group("/group")
|
|
|
|
g.Get("/users/:uid/files/:fid", getFile)
|
|
|
|
|
|
|
|
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()
|
|
|
|
h := func(*Context) error { return nil }
|
|
|
|
routes := []Route{
|
|
|
|
{GET, "/users/:user/events", h},
|
|
|
|
{GET, "/users/:user/events/public", h},
|
|
|
|
{POST, "/repos/:owner/:repo/git/refs", h},
|
|
|
|
{POST, "/repos/:owner/:repo/git/tags", h},
|
|
|
|
}
|
|
|
|
for _, r := range routes {
|
|
|
|
e.add(r.Method, r.Path, h)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i, r := range e.Routes() {
|
|
|
|
assert.Equal(t, routes[i].Method, r.Method)
|
|
|
|
assert.Equal(t, routes[i].Path, r.Path)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestEchoGroup(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
buf := new(bytes.Buffer)
|
|
|
|
e.Use(func(*Context) error {
|
|
|
|
buf.WriteString("0")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
h := func(*Context) error { return nil }
|
|
|
|
|
|
|
|
//--------
|
|
|
|
// Routes
|
|
|
|
//--------
|
|
|
|
|
|
|
|
e.Get("/users", h)
|
|
|
|
|
|
|
|
// Group
|
|
|
|
g1 := e.Group("/group1")
|
|
|
|
g1.Use(func(*Context) error {
|
|
|
|
buf.WriteString("1")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
g1.Get("/", h)
|
|
|
|
|
|
|
|
// Group with no parent middleware
|
|
|
|
g2 := e.Group("/group2", func(*Context) error {
|
|
|
|
buf.WriteString("2")
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
g2.Get("/", h)
|
|
|
|
|
|
|
|
// Nested groups
|
|
|
|
g3 := e.Group("/group3")
|
|
|
|
g4 := g3.Group("/group4")
|
|
|
|
g4.Get("/", func(c *Context) error {
|
|
|
|
return c.NoContent(http.StatusOK)
|
|
|
|
})
|
|
|
|
|
|
|
|
request(GET, "/users", e)
|
|
|
|
// println(len(e.middleware))
|
|
|
|
assert.Equal(t, "0", buf.String())
|
|
|
|
|
|
|
|
buf.Reset()
|
|
|
|
request(GET, "/group1/", e)
|
|
|
|
// println(len(g1.echo.middleware))
|
|
|
|
assert.Equal(t, "01", buf.String())
|
|
|
|
|
|
|
|
buf.Reset()
|
|
|
|
request(GET, "/group2/", e)
|
|
|
|
assert.Equal(t, "2", buf.String())
|
|
|
|
|
|
|
|
buf.Reset()
|
|
|
|
c, _ := request(GET, "/group3/group4/", e)
|
|
|
|
assert.Equal(t, http.StatusOK, c)
|
|
|
|
}
|
|
|
|
|
2015-04-06 05:08:52 +02:00
|
|
|
func TestEchoNotFound(t *testing.T) {
|
2015-03-31 05:54:38 +02:00
|
|
|
e := New()
|
2015-04-06 06:49:55 +02:00
|
|
|
r, _ := http.NewRequest(GET, "/files", nil)
|
2015-03-31 05:54:38 +02:00
|
|
|
w := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(w, r)
|
2015-05-31 18:37:26 +02:00
|
|
|
assert.Equal(t, http.StatusNotFound, w.Code)
|
2015-03-31 05:54:38 +02:00
|
|
|
}
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
func TestEchoHTTPError(t *testing.T) {
|
|
|
|
m := http.StatusText(http.StatusBadRequest)
|
|
|
|
he := NewHTTPError(http.StatusBadRequest, m)
|
|
|
|
assert.Equal(t, http.StatusBadRequest, he.Code())
|
|
|
|
assert.Equal(t, m, he.Error())
|
|
|
|
}
|
|
|
|
|
2015-06-27 23:34:22 +02:00
|
|
|
func TestEchoServer(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
s := e.Server(":1323")
|
|
|
|
assert.IsType(t, &http.Server{}, s)
|
|
|
|
}
|
|
|
|
|
2015-05-31 00:20:36 +02:00
|
|
|
func testMethod(t *testing.T, method, path string, e *Echo) {
|
|
|
|
m := fmt.Sprintf("%c%s", method[0], strings.ToLower(method[1:]))
|
|
|
|
p := reflect.ValueOf(path)
|
|
|
|
h := reflect.ValueOf(func(c *Context) error {
|
|
|
|
c.String(http.StatusOK, method)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
i := interface{}(e)
|
|
|
|
reflect.ValueOf(i).MethodByName(m).Call([]reflect.Value{p, h})
|
|
|
|
_, body := request(method, path, e)
|
|
|
|
if body != method {
|
|
|
|
t.Errorf("expected body `%s`, got %s.", method, body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func request(method, path string, e *Echo) (int, string) {
|
|
|
|
r, _ := http.NewRequest(method, path, nil)
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
e.ServeHTTP(w, r)
|
|
|
|
return w.Code, w.Body.String()
|
|
|
|
}
|