1
0
mirror of https://github.com/labstack/echo.git synced 2024-11-24 08:22:21 +02:00

More coverage on engine/...

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana 2016-06-07 17:08:13 -07:00
parent c654c422c4
commit f96f40b5e6
5 changed files with 181 additions and 36 deletions

View File

@ -0,0 +1,41 @@
package fasthttp
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
"github.com/labstack/gommon/log"
)
func TestResponseWriteHeader(t *testing.T) {
c := new(fasthttp.RequestCtx)
res := NewResponse(c, log.New("test"))
res.WriteHeader(http.StatusOK)
assert.True(t, res.Committed())
assert.Equal(t, http.StatusOK, res.Status())
}
func TestResponseWrite(t *testing.T) {
c := new(fasthttp.RequestCtx)
res := NewResponse(c, log.New("test"))
res.Write([]byte("test"))
assert.Equal(t, int64(4), res.Size())
assert.Equal(t, "test", string(c.Response.Body()))
}
func TestResponseSetCookie(t *testing.T) {
c := new(fasthttp.RequestCtx)
res := NewResponse(c, log.New("test"))
cookie := new(fasthttp.Cookie)
cookie.SetKey("name")
cookie.SetValue("Jon Snow")
res.SetCookie(&Cookie{cookie})
c.Response.Header.SetCookie(cookie)
ck := new(fasthttp.Cookie)
ck.SetKey("name")
assert.True(t, c.Response.Header.Cookie(ck))
assert.Equal(t, "Jon Snow", string(ck.Value()))
}

View File

@ -161,17 +161,20 @@ func WrapHandler(h fasthttp.RequestHandler) echo.HandlerFunc {
}
}
// WrapMiddleware wraps `fasthttp.RequestHandler` into `echo.MiddlewareFunc`
func WrapMiddleware(h fasthttp.RequestHandler) echo.MiddlewareFunc {
// WrapMiddleware wraps `func(fasthttp.RequestHandler) fasthttp.RequestHandler`
// into `echo.MiddlewareFunc`
func WrapMiddleware(m func(fasthttp.RequestHandler) fasthttp.RequestHandler) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
return func(c echo.Context) (err error) {
req := c.Request().(*Request)
res := c.Response().(*Response)
ctx := req.RequestCtx
h(ctx)
m(func(ctx *fasthttp.RequestCtx) {
next(c)
})(ctx)
res.status = ctx.Response.StatusCode()
res.size = int64(ctx.Response.Header.ContentLength())
return next(c)
return
}
}
}

View File

@ -0,0 +1,56 @@
package fasthttp
import (
"bytes"
"net/http"
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
"github.com/valyala/fasthttp"
)
// TODO: Fix me
func TestServer(t *testing.T) {
s := New("")
ctx := new(fasthttp.RequestCtx)
s.ServeHTTP(ctx)
}
func TestServerWrapHandler(t *testing.T) {
e := echo.New()
ctx := new(fasthttp.RequestCtx)
req := NewRequest(ctx, nil)
res := NewResponse(ctx, nil)
c := e.NewContext(req, res)
h := WrapHandler(func(ctx *fasthttp.RequestCtx) {
ctx.Write([]byte("test"))
})
if assert.NoError(t, h(c)) {
assert.Equal(t, http.StatusOK, ctx.Response.StatusCode())
assert.Equal(t, "test", string(ctx.Response.Body()))
}
}
func TestServerWrapMiddleware(t *testing.T) {
e := echo.New()
ctx := new(fasthttp.RequestCtx)
req := NewRequest(ctx, nil)
res := NewResponse(ctx, nil)
c := e.NewContext(req, res)
buf := new(bytes.Buffer)
mw := WrapMiddleware(func(h fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
buf.Write([]byte("mw"))
h(ctx)
}
})
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, ctx.Response.StatusCode())
assert.Equal(t, "OK", string(ctx.Response.Body()))
}
}

View File

@ -1,51 +1,38 @@
package standard
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"time"
"github.com/labstack/gommon/log"
"github.com/stretchr/testify/assert"
)
func TestResponseWriteHeader(t *testing.T) {
recorder := httptest.NewRecorder()
resp := NewResponse(recorder, log.New("echo"))
resp.WriteHeader(http.StatusNotFound)
assert.Equal(t, http.StatusNotFound, resp.Status())
assert.True(t, resp.Committed())
rec := httptest.NewRecorder()
res := NewResponse(rec, log.New("test"))
res.WriteHeader(http.StatusOK)
assert.True(t, res.Committed())
assert.Equal(t, http.StatusOK, res.Status())
}
func TestResponseWrite(t *testing.T) {
recorder := httptest.NewRecorder()
resp := NewResponse(recorder, log.New("echo"))
resp.Write([]byte("Hello"))
assert.Equal(t, int64(5), resp.Size())
if body, err := ioutil.ReadAll(recorder.Body); assert.NoError(t, err) {
assert.Equal(t, "Hello", string(body))
}
resp.Flush()
assert.True(t, recorder.Flushed)
rec := httptest.NewRecorder()
res := NewResponse(rec, log.New("test"))
res.Write([]byte("test"))
assert.Equal(t, int64(4), res.Size())
assert.Equal(t, "test", rec.Body.String())
res.Flush()
assert.True(t, rec.Flushed)
}
func TestResponseSetCookie(t *testing.T) {
recorder := httptest.NewRecorder()
resp := NewResponse(recorder, log.New("echo"))
resp.SetCookie(&Cookie{&http.Cookie{
Name: "session",
Value: "securetoken",
Path: "/",
Domain: "github.com",
Expires: time.Date(2016, time.January, 1, 0, 0, 0, 0, time.UTC),
Secure: true,
HttpOnly: true,
rec := httptest.NewRecorder()
res := NewResponse(rec, log.New("test"))
res.SetCookie(&Cookie{&http.Cookie{
Name: "name",
Value: "Jon Snow",
}})
assert.Equal(t, "session=securetoken; Path=/; Domain=github.com; Expires=Fri, 01 Jan 2016 00:00:00 GMT; HttpOnly; Secure", recorder.Header().Get("Set-Cookie"))
assert.Equal(t, "name=Jon Snow", rec.Header().Get("Set-Cookie"))
}

View File

@ -0,0 +1,58 @@
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())
}
}