func TestEchoStatic(t *testing.T) { e := New() // OK e.Static("/scripts", "examples/website/public/scripts") 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) // Directory with index.html e.Static("/", "examples/website/public") 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, "sub directory", r) }
func TestEchoFavicon(t *testing.T) { e := New() e.Favicon("examples/website/public/favicon.ico") c, b := request(GET, "/favicon.ico", e) assert.Equal(t, http.StatusOK, c) assert.NotEmpty(t, b) }
func TestEchoIndex(t *testing.T) { e := New() e.Index("examples/website/public/index.html") c, b := request(GET, "/", e) assert.Equal(t, http.StatusOK, c) assert.NotEmpty(t, b) }
func TestCORS(t *testing.T) { mw := cors() req := testRequest(nil, "GET", "/", nil) res := httptest.NewRecorder() c := echo.NewContext(req.Request, echo.NewResponse(res), echo.New()) called := false next := func(c *echo.Context) error { called = true return nil } // It calls the next middleware when no origin is set h := mw(next) h(c) assert.True(t, called) assert.Empty(t, res.Header().Get("Access-Control-Allow-Origin")) // It sets CORS headers and calls the next middleware when the origin is set req.Header.Set("Origin", "china") called = false h(c) assert.True(t, called) assert.NotEmpty(t, res.Header().Get("Access-Control-Allow-Origin")) // It sets CORS headers, ends the middleware chain and // returns 200 when receiving a preflight request req.Method = "OPTIONS" res = httptest.NewRecorder() c = echo.NewContext(req.Request, echo.NewResponse(res), echo.New()) res.Code = 0 called = false h(c) assert.False(t, called) assert.Equal(t, 200, res.Code) assert.NotEmpty(t, res.Header().Get("Access-Control-Allow-Origin")) assert.NotEmpty(t, res.Header().Get("Access-Control-Allow-Methods")) assert.NotEmpty(t, res.Header().Get("Access-Control-Allow-Headers")) }
func TestAddUser(t *testing.T) { err := store.AddUser(&User{ Username: "******", Password: "******", }) assert.Nil(t, err) user := store.GetUser("added") assert.NotNil(t, user) assert.Equal(t, "added", user.Username) assert.NotEmpty(t, user.Password) err = store.AddUser(&User{Username: "******"}) assert.Equal(t, ErrUsernameUnavailable, err) }
func TestRouterAPI(t *testing.T) { e := New() r := e.router for _, route := range api { r.Add(route.Method, route.Path, func(c *Context) error { return nil }, e) } c := NewContext(nil, nil, e) for _, route := range api { h, _ := r.Find(route.Method, route.Path, c) if assert.NotNil(t, h) { for i, n := range c.pnames { if assert.NotEmpty(t, n) { assert.Equal(t, ":"+n, c.P(i)) } } h(c) } } }