1
0
mirror of https://github.com/labstack/echo.git synced 2025-03-21 21:27:04 +02:00
echo/engine/standard/response_test.go
Vishal Rana c654c422c4 More coverage and better response adapter for standard/response
Signed-off-by: Vishal Rana <vr@labstack.com>
2016-06-06 22:27:36 -07:00

52 lines
1.3 KiB
Go

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())
}
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)
}
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,
}})
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"))
}