mirror of
https://github.com/labstack/echo.git
synced 2024-12-26 20:54:00 +02:00
f96f40b5e6
Signed-off-by: Vishal Rana <vr@labstack.com>
59 lines
1.4 KiB
Go
59 lines
1.4 KiB
Go
package standard
|
|
|
|
import (
|
|
"bytes"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/labstack/echo"
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
// TODO: Fix me
|
|
func TestServer(t *testing.T) {
|
|
s := New("")
|
|
rec := httptest.NewRecorder()
|
|
req := new(http.Request)
|
|
s.ServeHTTP(rec, req)
|
|
}
|
|
|
|
func TestServerWrapHandler(t *testing.T) {
|
|
e := echo.New()
|
|
req := NewRequest(new(http.Request), nil)
|
|
rec := httptest.NewRecorder()
|
|
res := NewResponse(rec, nil)
|
|
c := e.NewContext(req, res)
|
|
h := WrapHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte("test"))
|
|
}))
|
|
if assert.NoError(t, h(c)) {
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Equal(t, "test", rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestServerWrapMiddleware(t *testing.T) {
|
|
e := echo.New()
|
|
req := NewRequest(new(http.Request), nil)
|
|
rec := httptest.NewRecorder()
|
|
res := NewResponse(rec, nil)
|
|
c := e.NewContext(req, res)
|
|
buf := new(bytes.Buffer)
|
|
mw := WrapMiddleware(func(h http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
buf.Write([]byte("mw"))
|
|
h.ServeHTTP(w, r)
|
|
})
|
|
})
|
|
h := mw(func(c echo.Context) error {
|
|
return c.String(http.StatusOK, "OK")
|
|
})
|
|
if assert.NoError(t, h(c)) {
|
|
assert.Equal(t, "mw", buf.String())
|
|
assert.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Equal(t, "OK", rec.Body.String())
|
|
}
|
|
}
|