2015-03-31 06:06:21 +02:00
|
|
|
package echo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
2015-09-13 10:38:22 +02:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2015-03-31 06:06:21 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestResponse(t *testing.T) {
|
2015-12-01 21:22:45 +02:00
|
|
|
e := New()
|
2015-05-30 01:50:46 +02:00
|
|
|
w := httptest.NewRecorder()
|
2015-12-01 21:22:45 +02:00
|
|
|
r := NewResponse(w, e)
|
2015-05-30 01:50:46 +02:00
|
|
|
|
|
|
|
// SetWriter
|
|
|
|
r.SetWriter(w)
|
|
|
|
|
|
|
|
// Writer
|
|
|
|
assert.Equal(t, w, r.Writer())
|
2015-03-31 06:06:21 +02:00
|
|
|
|
2015-04-19 01:47:48 +02:00
|
|
|
// Header
|
2015-05-30 01:50:46 +02:00
|
|
|
assert.NotNil(t, r.Header())
|
2015-03-31 06:06:21 +02:00
|
|
|
|
2015-04-19 01:47:48 +02:00
|
|
|
// WriteHeader
|
|
|
|
r.WriteHeader(http.StatusOK)
|
2015-05-30 01:50:46 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.status)
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// Committed
|
2015-05-30 01:50:46 +02:00
|
|
|
assert.True(t, r.committed)
|
|
|
|
|
2015-05-30 19:54:55 +02:00
|
|
|
// Already committed
|
|
|
|
r.WriteHeader(http.StatusTeapot)
|
|
|
|
assert.NotEqual(t, http.StatusTeapot, r.Status())
|
2015-04-19 01:47:48 +02:00
|
|
|
|
|
|
|
// Status
|
|
|
|
r.status = http.StatusOK
|
2015-05-30 01:50:46 +02:00
|
|
|
assert.Equal(t, http.StatusOK, r.Status())
|
2015-04-19 01:47:48 +02:00
|
|
|
|
2015-05-30 01:50:46 +02:00
|
|
|
// Write
|
2015-04-19 01:47:48 +02:00
|
|
|
s := "echo"
|
2015-05-30 01:50:46 +02:00
|
|
|
_, err := r.Write([]byte(s))
|
|
|
|
assert.NoError(t, err)
|
|
|
|
|
|
|
|
// Flush
|
|
|
|
r.Flush()
|
|
|
|
|
|
|
|
// Size
|
2015-05-31 00:20:36 +02:00
|
|
|
assert.EqualValues(t, len(s), r.Size())
|
2015-05-30 01:50:46 +02:00
|
|
|
|
2015-09-13 10:38:22 +02:00
|
|
|
// Committed
|
|
|
|
assert.Equal(t, true, r.Committed())
|
|
|
|
|
2015-05-30 01:50:46 +02:00
|
|
|
// Hijack
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
r.Hijack()
|
|
|
|
})
|
|
|
|
|
|
|
|
// CloseNotify
|
|
|
|
assert.Panics(t, func() {
|
|
|
|
r.CloseNotify()
|
|
|
|
})
|
2015-05-29 01:50:49 +02:00
|
|
|
|
|
|
|
// reset
|
2015-12-02 18:07:22 +02:00
|
|
|
r.reset(httptest.NewRecorder(), New())
|
2015-03-31 06:06:21 +02:00
|
|
|
}
|
2016-06-03 16:50:24 +02:00
|
|
|
|
|
|
|
func TestResponseWriteCommit(t *testing.T) {
|
|
|
|
e := New()
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
r := NewResponse(w, e)
|
|
|
|
|
|
|
|
// Write body, it writes header if not committed yet
|
|
|
|
s := "echo"
|
|
|
|
r.Write([]byte(s))
|
|
|
|
|
|
|
|
assert.Equal(t, w.Code, 200)
|
|
|
|
assert.Equal(t, w.Body.String(), s)
|
|
|
|
|
|
|
|
assert.Equal(t, r.Status(), 200)
|
|
|
|
assert.Equal(t, r.Size(), int64(4))
|
|
|
|
assert.True(t, r.Committed())
|
|
|
|
|
|
|
|
// This is ignored with warning
|
|
|
|
r.WriteHeader(400)
|
|
|
|
|
|
|
|
assert.Equal(t, r.Status(), 200)
|
|
|
|
assert.Equal(t, r.Size(), int64(4))
|
|
|
|
assert.True(t, r.Committed())
|
|
|
|
}
|