Skip to content

Commit

Permalink
Fixed middleware test cases
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Apr 16, 2016
1 parent 467cf05 commit ce80fc8
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 35 deletions.
2 changes: 1 addition & 1 deletion middleware/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func TestBasicAuth(t *testing.T) {
e := echo.New()
rq := test.NewRequest(echo.GET, "/", nil)
rs := test.NewResponseRecorder()
c := echo.NewContext(rq, rs, e)
c := e.NewContext(rq, rs)
f := func(u, p string) bool {
if u == "joe" && p == "secret" {
return true
Expand Down
26 changes: 13 additions & 13 deletions middleware/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,27 @@ 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 {
c.Response().Write([]byte("test")) // For Content-Type sniffing
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)
Expand All @@ -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))
}
Expand Down
8 changes: 4 additions & 4 deletions middleware/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestCORS(t *testing.T) {
e := echo.New()
rq := test.NewRequest(echo.GET, "/", nil)
rc := test.NewResponseRecorder()
c := echo.NewContext(rq, rc, e)
c := e.NewContext(rq, rc)
cors := CORSWithConfig(CORSConfig{
AllowCredentials: true,
})
Expand All @@ -28,15 +28,15 @@ func TestCORS(t *testing.T) {
// Wildcard origin
rq = test.NewRequest(echo.GET, "/", nil)
rc = test.NewResponseRecorder()
c = echo.NewContext(rq, rc, e)
c = e.NewContext(rq, rc)
rq.Header().Set(echo.HeaderOrigin, "localhost")
h(c)
assert.Equal(t, "*", rc.Header().Get(echo.HeaderAccessControlAllowOrigin))

// Simple request
rq = test.NewRequest(echo.GET, "/", nil)
rc = test.NewResponseRecorder()
c = echo.NewContext(rq, rc, e)
c = e.NewContext(rq, rc)
rq.Header().Set(echo.HeaderOrigin, "localhost")
cors = CORSWithConfig(CORSConfig{
AllowOrigins: []string{"localhost"},
Expand All @@ -52,7 +52,7 @@ func TestCORS(t *testing.T) {
// Preflight request
rq = test.NewRequest(echo.OPTIONS, "/", nil)
rc = test.NewResponseRecorder()
c = echo.NewContext(rq, rc, e)
c = e.NewContext(rq, rc)
rq.Header().Set(echo.HeaderOrigin, "localhost")
rq.Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
h(c)
Expand Down
20 changes: 10 additions & 10 deletions middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ func TestLogger(t *testing.T) {
// Note: Just for the test coverage, not a real test.
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 := Logger()(func(c echo.Context) error {
return c.String(http.StatusOK, "test")
})
Expand All @@ -25,25 +25,25 @@ func TestLogger(t *testing.T) {
h(c)

// Status 3xx
rec = test.NewResponseRecorder()
c = echo.NewContext(rq, rec, e)
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc)
h = Logger()(func(c echo.Context) error {
return c.String(http.StatusTemporaryRedirect, "test")
})
h(c)

// Status 4xx
rec = test.NewResponseRecorder()
c = echo.NewContext(rq, rec, e)
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc)
h = Logger()(func(c echo.Context) error {
return c.String(http.StatusNotFound, "test")
})
h(c)

// Status 5xx with empty path
rq = test.NewRequest(echo.GET, "", nil)
rec = test.NewResponseRecorder()
c = echo.NewContext(rq, rec, e)
rc = test.NewResponseRecorder()
c = e.NewContext(rq, rc)
h = Logger()(func(c echo.Context) error {
return errors.New("error")
})
Expand All @@ -53,8 +53,8 @@ func TestLogger(t *testing.T) {
func TestLoggerIPAddress(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)
buf := new(bytes.Buffer)
e.Logger().SetOutput(buf)
ip := "127.0.0.1"
Expand Down
6 changes: 3 additions & 3 deletions middleware/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ func TestRecover(t *testing.T) {
buf := new(bytes.Buffer)
e.SetLogOutput(buf)
rq := test.NewRequest(echo.GET, "/", nil)
rec := test.NewResponseRecorder()
c := echo.NewContext(rq, rec, e)
rc := test.NewResponseRecorder()
c := e.NewContext(rq, rc)
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
panic("test")
}))
h(c)
assert.Equal(t, http.StatusInternalServerError, rec.Status())
assert.Equal(t, http.StatusInternalServerError, rc.Status())
assert.Contains(t, buf.String(), "PANIC RECOVER")
}
8 changes: 4 additions & 4 deletions middleware/slash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestAddTrailingSlash(t *testing.T) {
e := echo.New()
rq := test.NewRequest(echo.GET, "/add-slash", nil)
rc := test.NewResponseRecorder()
c := echo.NewContext(rq, rc, e)
c := e.NewContext(rq, rc)
h := AddTrailingSlash()(func(c echo.Context) error {
return nil
})
Expand All @@ -24,7 +24,7 @@ func TestAddTrailingSlash(t *testing.T) {
// With config
rq = test.NewRequest(echo.GET, "/add-slash?key=value", nil)
rc = test.NewResponseRecorder()
c = echo.NewContext(rq, rc, e)
c = e.NewContext(rq, rc)
h = AddTrailingSlashWithConfig(TrailingSlashConfig{
RedirectCode: http.StatusMovedPermanently,
})(func(c echo.Context) error {
Expand All @@ -39,7 +39,7 @@ func TestRemoveTrailingSlash(t *testing.T) {
e := echo.New()
rq := test.NewRequest(echo.GET, "/remove-slash/", nil)
rc := test.NewResponseRecorder()
c := echo.NewContext(rq, rc, e)
c := e.NewContext(rq, rc)
h := RemoveTrailingSlash()(func(c echo.Context) error {
return nil
})
Expand All @@ -50,7 +50,7 @@ func TestRemoveTrailingSlash(t *testing.T) {
// With config
rq = test.NewRequest(echo.GET, "/remove-slash/?key=value", nil)
rc = test.NewResponseRecorder()
c = echo.NewContext(rq, rc, e)
c = e.NewContext(rq, rc)
h = RemoveTrailingSlashWithConfig(TrailingSlashConfig{
RedirectCode: http.StatusMovedPermanently,
})(func(c echo.Context) error {
Expand Down

0 comments on commit ce80fc8

Please sign in to comment.