mirror of
https://github.com/labstack/echo.git
synced 2025-07-03 00:56:59 +02:00
@ -4,19 +4,19 @@ import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/labstack/echo"
|
||||
"github.com/labstack/echo/test"
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestLogger(t *testing.T) {
|
||||
// Note: Just for the test coverage, not a real test.
|
||||
e := echo.New()
|
||||
req, _ := http.NewRequest(echo.GET, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
c := echo.NewContext(req, echo.NewResponse(rec, e), e)
|
||||
req := test.NewRequest(echo.GET, "/", nil)
|
||||
res := test.NewResponseRecorder()
|
||||
c := echo.NewContext(req, res, e)
|
||||
|
||||
// Status 2xx
|
||||
h := func(c echo.Context) error {
|
||||
@ -25,25 +25,25 @@ func TestLogger(t *testing.T) {
|
||||
Logger()(h)(c)
|
||||
|
||||
// Status 3xx
|
||||
rec = httptest.NewRecorder()
|
||||
c = echo.NewContext(req, echo.NewResponse(rec, e), e)
|
||||
res = test.NewResponseRecorder()
|
||||
c = echo.NewContext(req, res, 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), e)
|
||||
res = test.NewResponseRecorder()
|
||||
c = echo.NewContext(req, res, 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), e)
|
||||
req = test.NewRequest(echo.GET, "", nil)
|
||||
res = test.NewResponseRecorder()
|
||||
c = echo.NewContext(req, res, e)
|
||||
h = func(c echo.Context) error {
|
||||
return errors.New("error")
|
||||
}
|
||||
@ -52,9 +52,9 @@ func TestLogger(t *testing.T) {
|
||||
|
||||
func TestLoggerIPAddress(t *testing.T) {
|
||||
e := echo.New()
|
||||
req, _ := http.NewRequest(echo.GET, "/", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
c := echo.NewContext(req, echo.NewResponse(rec, e), e)
|
||||
req := test.NewRequest(echo.GET, "/", nil)
|
||||
res := test.NewResponseRecorder()
|
||||
c := echo.NewContext(req, res, e)
|
||||
buf := new(bytes.Buffer)
|
||||
e.Logger().SetOutput(buf)
|
||||
ip := "127.0.0.1"
|
||||
@ -65,14 +65,14 @@ func TestLoggerIPAddress(t *testing.T) {
|
||||
mw := Logger()
|
||||
|
||||
// With X-Real-IP
|
||||
req.Header.Add(echo.XRealIP, ip)
|
||||
req.Header().Add(echo.XRealIP, ip)
|
||||
mw(h)(c)
|
||||
assert.Contains(t, buf.String(), ip)
|
||||
|
||||
// With X-Forwarded-For
|
||||
buf.Reset()
|
||||
req.Header.Del(echo.XRealIP)
|
||||
req.Header.Add(echo.XForwardedFor, ip)
|
||||
req.Header().Del(echo.XRealIP)
|
||||
req.Header().Add(echo.XForwardedFor, ip)
|
||||
mw(h)(c)
|
||||
assert.Contains(t, buf.String(), ip)
|
||||
|
||||
|
Reference in New Issue
Block a user