1
0
mirror of https://github.com/labstack/echo.git synced 2025-06-17 00:17:36 +02:00

Fixed middleware test cases

Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
Vishal Rana
2016-04-16 16:02:22 -07:00
parent 467cf05b41
commit ce80fc8ea4
6 changed files with 35 additions and 35 deletions

View File

@ -15,8 +15,8 @@ import (
func TestGzip(t *testing.T) {
e := echo.New()
rq := test.NewRequest(echo.GET, "/", nil)
rec := test.NewResponseRecorder()
c := echo.NewContext(rq, rec, e)
rc := test.NewResponseRecorder()
c := e.NewContext(rq, rc)
// Skip if no Accept-Encoding header
h := Gzip()(func(c echo.Context) error {
@ -24,18 +24,18 @@ func TestGzip(t *testing.T) {
return nil
})
h(c)
assert.Equal(t, "test", rec.Body.String())
assert.Equal(t, "test", rc.Body.String())
rq = test.NewRequest(echo.GET, "/", nil)
rq.Header().Set(echo.HeaderAcceptEncoding, "gzip")
rec = test.NewResponseRecorder()
c = echo.NewContext(rq, rec, e)
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc)
// Gzip
h(c)
assert.Equal(t, "gzip", rec.Header().Get(echo.HeaderContentEncoding))
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
r, err := gzip.NewReader(rec.Body)
assert.Equal(t, "gzip", rc.Header().Get(echo.HeaderContentEncoding))
assert.Contains(t, rc.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
r, err := gzip.NewReader(rc.Body)
defer r.Close()
if assert.NoError(t, err) {
buf := new(bytes.Buffer)
@ -47,16 +47,16 @@ func TestGzip(t *testing.T) {
func TestGzipNoContent(t *testing.T) {
e := echo.New()
rq := test.NewRequest(echo.GET, "/", nil)
rec := test.NewResponseRecorder()
c := echo.NewContext(rq, rec, e)
rc := test.NewResponseRecorder()
c := e.NewContext(rq, rc)
h := Gzip()(func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})
h(c)
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
assert.Empty(t, rec.Header().Get(echo.HeaderContentType))
b, err := ioutil.ReadAll(rec.Body)
assert.Empty(t, rc.Header().Get(echo.HeaderContentEncoding))
assert.Empty(t, rc.Header().Get(echo.HeaderContentType))
b, err := ioutil.ReadAll(rc.Body)
if assert.NoError(t, err) {
assert.Equal(t, 0, len(b))
}