2015-05-12 00:43:54 +02:00
|
|
|
package middleware
|
2015-05-20 23:38:51 +02:00
|
|
|
|
|
|
|
import (
|
2015-05-31 00:20:36 +02:00
|
|
|
"errors"
|
2015-05-20 23:38:51 +02:00
|
|
|
"github.com/labstack/echo"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogger(t *testing.T) {
|
|
|
|
e := echo.New()
|
|
|
|
req, _ := http.NewRequest(echo.GET, "/", nil)
|
2015-05-30 19:54:55 +02:00
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
c := echo.NewContext(req, echo.NewResponse(rec), e)
|
2015-05-20 23:38:51 +02:00
|
|
|
|
|
|
|
// Status 2xx
|
|
|
|
h := func(c *echo.Context) error {
|
|
|
|
return c.String(http.StatusOK, "test")
|
|
|
|
}
|
|
|
|
Logger()(h)(c)
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// 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)
|
|
|
|
|
2015-05-20 23:38:51 +02:00
|
|
|
// Status 4xx
|
2015-05-30 19:54:55 +02:00
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = echo.NewContext(req, echo.NewResponse(rec), e)
|
2015-05-20 23:38:51 +02:00
|
|
|
h = func(c *echo.Context) error {
|
|
|
|
return c.String(http.StatusNotFound, "test")
|
|
|
|
}
|
|
|
|
Logger()(h)(c)
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// Status 5xx with empty path
|
|
|
|
req, _ = http.NewRequest(echo.GET, "", nil)
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
c = echo.NewContext(req, echo.NewResponse(rec), e)
|
2015-05-20 23:38:51 +02:00
|
|
|
h = func(c *echo.Context) error {
|
2015-05-30 19:54:55 +02:00
|
|
|
return errors.New("error")
|
2015-05-20 23:38:51 +02:00
|
|
|
}
|
|
|
|
Logger()(h)(c)
|
|
|
|
}
|