func TestGzip(t *testing.T) { // Empty Accept-Encoding header req, _ := http.NewRequest(echo.GET, "/", nil) rec := httptest.NewRecorder() c := echo.NewContext(req, echo.NewResponse(rec), echo.New()) h := func(c *echo.Context) error { return c.String(http.StatusOK, "test") } Gzip()(h)(c) assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(t, "test", rec.Body.String()) // With Accept-Encoding header req, _ = http.NewRequest(echo.GET, "/", nil) req.Header.Set(echo.AcceptEncoding, "gzip") rec = httptest.NewRecorder() c = echo.NewContext(req, echo.NewResponse(rec), echo.New()) Gzip()(h)(c) assert.Equal(t, http.StatusOK, rec.Code) assert.Equal(t, "gzip", rec.Header().Get(echo.ContentEncoding)) r, err := gzip.NewReader(rec.Body) defer r.Close() if assert.NoError(t, err) { buf := new(bytes.Buffer) buf.ReadFrom(r) assert.Equal(t, "test", buf.String()) } }
func TestStripTrailingSlash(t *testing.T) { req, _ := http.NewRequest(echo.GET, "/users/", nil) rec := httptest.NewRecorder() c := echo.NewContext(req, echo.NewResponse(rec), echo.New()) StripTrailingSlash()(c) assert.Equal(t, "/users", c.Request().URL.Path) }
func TestRedirectToSlash(t *testing.T) { req, _ := http.NewRequest(echo.GET, "/users", nil) rec := httptest.NewRecorder() c := echo.NewContext(req, echo.NewResponse(rec), echo.New()) RedirectToSlash(RedirectToSlashOptions{Code: http.StatusTemporaryRedirect})(c) assert.Equal(t, http.StatusTemporaryRedirect, rec.Code) assert.Equal(t, "/users/", c.Response().Header().Get("Location")) }
func TestRecover(t *testing.T) { e := echo.New() e.SetDebug(true) req, _ := http.NewRequest(echo.GET, "/", nil) rec := httptest.NewRecorder() c := echo.NewContext(req, echo.NewResponse(rec), e) h := func(c *echo.Context) error { panic("test") } Recover()(h)(c) assert.Equal(t, http.StatusInternalServerError, rec.Code) assert.Contains(t, rec.Body.String(), "panic recover") }
func TestLogger(t *testing.T) { e := echo.New() req, _ := http.NewRequest(echo.GET, "/", nil) rec := httptest.NewRecorder() c := echo.NewContext(req, echo.NewResponse(rec), e) // Status 2xx h := func(c *echo.Context) error { return c.String(http.StatusOK, "test") } Logger()(h)(c) // Status 3xx rec = httptest.NewRecorder() c = echo.NewContext(req, echo.NewResponse(rec), e) h = func(c *echo.Context) error { return c.String(http.StatusTemporaryRedirect, "test") } Logger()(h)(c) // Status 4xx rec = httptest.NewRecorder() c = echo.NewContext(req, echo.NewResponse(rec), e) h = func(c *echo.Context) error { return c.String(http.StatusNotFound, "test") } Logger()(h)(c) // Status 5xx with empty path req, _ = http.NewRequest(echo.GET, "", nil) rec = httptest.NewRecorder() c = echo.NewContext(req, echo.NewResponse(rec), e) h = func(c *echo.Context) error { return errors.New("error") } Logger()(h)(c) }
func TestBasicAuth(t *testing.T) { req, _ := http.NewRequest(echo.GET, "/", nil) rec := httptest.NewRecorder() c := echo.NewContext(req, echo.NewResponse(rec), echo.New()) fn := func(u, p string) bool { if u == "joe" && p == "secret" { return true } return false } ba := BasicAuth(fn) //------------------- // Valid credentials //------------------- auth := Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) req.Header.Set(echo.Authorization, auth) assert.NoError(t, ba(c)) // Case insensitive auth = "basic " + base64.StdEncoding.EncodeToString([]byte("joe:secret")) req.Header.Set(echo.Authorization, auth) assert.NoError(t, ba(c)) //--------------------- // Invalid credentials //--------------------- // Incorrect password auth = Basic + " " + base64.StdEncoding.EncodeToString([]byte("joe:password")) req.Header.Set(echo.Authorization, auth) ba = BasicAuth(fn) he := ba(c).(*echo.HTTPError) assert.Equal(t, http.StatusUnauthorized, he.Code()) // Empty Authorization header req.Header.Set(echo.Authorization, "") ba = BasicAuth(fn) he = ba(c).(*echo.HTTPError) assert.Equal(t, http.StatusBadRequest, he.Code()) // Invalid Authorization header auth = base64.StdEncoding.EncodeToString([]byte(" :secret")) req.Header.Set(echo.Authorization, auth) ba = BasicAuth(fn) he = ba(c).(*echo.HTTPError) assert.Equal(t, http.StatusBadRequest, he.Code()) // Invalid scheme auth = "Base " + base64.StdEncoding.EncodeToString([]byte(" :secret")) req.Header.Set(echo.Authorization, auth) ba = BasicAuth(fn) he = ba(c).(*echo.HTTPError) assert.Equal(t, http.StatusBadRequest, he.Code()) // WebSocket c.Request().Header.Set(echo.Upgrade, echo.WebSocket) ba = BasicAuth(fn) assert.NoError(t, ba(c)) }