mirror of
https://github.com/labstack/echo.git
synced 2025-04-21 12:17:04 +02:00
Fixed middleware test cases
Signed-off-by: Vishal Rana <vr@labstack.com>
This commit is contained in:
parent
467cf05b41
commit
ce80fc8ea4
@ -14,7 +14,7 @@ func TestBasicAuth(t *testing.T) {
|
|||||||
e := echo.New()
|
e := echo.New()
|
||||||
rq := test.NewRequest(echo.GET, "/", nil)
|
rq := test.NewRequest(echo.GET, "/", nil)
|
||||||
rs := test.NewResponseRecorder()
|
rs := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(rq, rs, e)
|
c := e.NewContext(rq, rs)
|
||||||
f := func(u, p string) bool {
|
f := func(u, p string) bool {
|
||||||
if u == "joe" && p == "secret" {
|
if u == "joe" && p == "secret" {
|
||||||
return true
|
return true
|
||||||
|
@ -15,8 +15,8 @@ import (
|
|||||||
func TestGzip(t *testing.T) {
|
func TestGzip(t *testing.T) {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
rq := test.NewRequest(echo.GET, "/", nil)
|
rq := test.NewRequest(echo.GET, "/", nil)
|
||||||
rec := test.NewResponseRecorder()
|
rc := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(rq, rec, e)
|
c := e.NewContext(rq, rc)
|
||||||
|
|
||||||
// Skip if no Accept-Encoding header
|
// Skip if no Accept-Encoding header
|
||||||
h := Gzip()(func(c echo.Context) error {
|
h := Gzip()(func(c echo.Context) error {
|
||||||
@ -24,18 +24,18 @@ func TestGzip(t *testing.T) {
|
|||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
h(c)
|
h(c)
|
||||||
assert.Equal(t, "test", rec.Body.String())
|
assert.Equal(t, "test", rc.Body.String())
|
||||||
|
|
||||||
rq = test.NewRequest(echo.GET, "/", nil)
|
rq = test.NewRequest(echo.GET, "/", nil)
|
||||||
rq.Header().Set(echo.HeaderAcceptEncoding, "gzip")
|
rq.Header().Set(echo.HeaderAcceptEncoding, "gzip")
|
||||||
rec = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rec, e)
|
c = e.NewContext(rq, rc)
|
||||||
|
|
||||||
// Gzip
|
// Gzip
|
||||||
h(c)
|
h(c)
|
||||||
assert.Equal(t, "gzip", rec.Header().Get(echo.HeaderContentEncoding))
|
assert.Equal(t, "gzip", rc.Header().Get(echo.HeaderContentEncoding))
|
||||||
assert.Contains(t, rec.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
|
assert.Contains(t, rc.Header().Get(echo.HeaderContentType), echo.MIMETextPlain)
|
||||||
r, err := gzip.NewReader(rec.Body)
|
r, err := gzip.NewReader(rc.Body)
|
||||||
defer r.Close()
|
defer r.Close()
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
@ -47,16 +47,16 @@ func TestGzip(t *testing.T) {
|
|||||||
func TestGzipNoContent(t *testing.T) {
|
func TestGzipNoContent(t *testing.T) {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
rq := test.NewRequest(echo.GET, "/", nil)
|
rq := test.NewRequest(echo.GET, "/", nil)
|
||||||
rec := test.NewResponseRecorder()
|
rc := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(rq, rec, e)
|
c := e.NewContext(rq, rc)
|
||||||
h := Gzip()(func(c echo.Context) error {
|
h := Gzip()(func(c echo.Context) error {
|
||||||
return c.NoContent(http.StatusOK)
|
return c.NoContent(http.StatusOK)
|
||||||
})
|
})
|
||||||
h(c)
|
h(c)
|
||||||
|
|
||||||
assert.Empty(t, rec.Header().Get(echo.HeaderContentEncoding))
|
assert.Empty(t, rc.Header().Get(echo.HeaderContentEncoding))
|
||||||
assert.Empty(t, rec.Header().Get(echo.HeaderContentType))
|
assert.Empty(t, rc.Header().Get(echo.HeaderContentType))
|
||||||
b, err := ioutil.ReadAll(rec.Body)
|
b, err := ioutil.ReadAll(rc.Body)
|
||||||
if assert.NoError(t, err) {
|
if assert.NoError(t, err) {
|
||||||
assert.Equal(t, 0, len(b))
|
assert.Equal(t, 0, len(b))
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ func TestCORS(t *testing.T) {
|
|||||||
e := echo.New()
|
e := echo.New()
|
||||||
rq := test.NewRequest(echo.GET, "/", nil)
|
rq := test.NewRequest(echo.GET, "/", nil)
|
||||||
rc := test.NewResponseRecorder()
|
rc := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(rq, rc, e)
|
c := e.NewContext(rq, rc)
|
||||||
cors := CORSWithConfig(CORSConfig{
|
cors := CORSWithConfig(CORSConfig{
|
||||||
AllowCredentials: true,
|
AllowCredentials: true,
|
||||||
})
|
})
|
||||||
@ -28,7 +28,7 @@ func TestCORS(t *testing.T) {
|
|||||||
// Wildcard origin
|
// Wildcard origin
|
||||||
rq = test.NewRequest(echo.GET, "/", nil)
|
rq = test.NewRequest(echo.GET, "/", nil)
|
||||||
rc = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rc, e)
|
c = e.NewContext(rq, rc)
|
||||||
rq.Header().Set(echo.HeaderOrigin, "localhost")
|
rq.Header().Set(echo.HeaderOrigin, "localhost")
|
||||||
h(c)
|
h(c)
|
||||||
assert.Equal(t, "*", rc.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
assert.Equal(t, "*", rc.Header().Get(echo.HeaderAccessControlAllowOrigin))
|
||||||
@ -36,7 +36,7 @@ func TestCORS(t *testing.T) {
|
|||||||
// Simple request
|
// Simple request
|
||||||
rq = test.NewRequest(echo.GET, "/", nil)
|
rq = test.NewRequest(echo.GET, "/", nil)
|
||||||
rc = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rc, e)
|
c = e.NewContext(rq, rc)
|
||||||
rq.Header().Set(echo.HeaderOrigin, "localhost")
|
rq.Header().Set(echo.HeaderOrigin, "localhost")
|
||||||
cors = CORSWithConfig(CORSConfig{
|
cors = CORSWithConfig(CORSConfig{
|
||||||
AllowOrigins: []string{"localhost"},
|
AllowOrigins: []string{"localhost"},
|
||||||
@ -52,7 +52,7 @@ func TestCORS(t *testing.T) {
|
|||||||
// Preflight request
|
// Preflight request
|
||||||
rq = test.NewRequest(echo.OPTIONS, "/", nil)
|
rq = test.NewRequest(echo.OPTIONS, "/", nil)
|
||||||
rc = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rc, e)
|
c = e.NewContext(rq, rc)
|
||||||
rq.Header().Set(echo.HeaderOrigin, "localhost")
|
rq.Header().Set(echo.HeaderOrigin, "localhost")
|
||||||
rq.Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
rq.Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
|
||||||
h(c)
|
h(c)
|
||||||
|
@ -15,8 +15,8 @@ func TestLogger(t *testing.T) {
|
|||||||
// Note: Just for the test coverage, not a real test.
|
// Note: Just for the test coverage, not a real test.
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
rq := test.NewRequest(echo.GET, "/", nil)
|
rq := test.NewRequest(echo.GET, "/", nil)
|
||||||
rec := test.NewResponseRecorder()
|
rc := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(rq, rec, e)
|
c := e.NewContext(rq, rc)
|
||||||
h := Logger()(func(c echo.Context) error {
|
h := Logger()(func(c echo.Context) error {
|
||||||
return c.String(http.StatusOK, "test")
|
return c.String(http.StatusOK, "test")
|
||||||
})
|
})
|
||||||
@ -25,16 +25,16 @@ func TestLogger(t *testing.T) {
|
|||||||
h(c)
|
h(c)
|
||||||
|
|
||||||
// Status 3xx
|
// Status 3xx
|
||||||
rec = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rec, e)
|
c = e.NewContext(rq, rc)
|
||||||
h = Logger()(func(c echo.Context) error {
|
h = Logger()(func(c echo.Context) error {
|
||||||
return c.String(http.StatusTemporaryRedirect, "test")
|
return c.String(http.StatusTemporaryRedirect, "test")
|
||||||
})
|
})
|
||||||
h(c)
|
h(c)
|
||||||
|
|
||||||
// Status 4xx
|
// Status 4xx
|
||||||
rec = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rec, e)
|
c = e.NewContext(rq, rc)
|
||||||
h = Logger()(func(c echo.Context) error {
|
h = Logger()(func(c echo.Context) error {
|
||||||
return c.String(http.StatusNotFound, "test")
|
return c.String(http.StatusNotFound, "test")
|
||||||
})
|
})
|
||||||
@ -42,8 +42,8 @@ func TestLogger(t *testing.T) {
|
|||||||
|
|
||||||
// Status 5xx with empty path
|
// Status 5xx with empty path
|
||||||
rq = test.NewRequest(echo.GET, "", nil)
|
rq = test.NewRequest(echo.GET, "", nil)
|
||||||
rec = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rec, e)
|
c = e.NewContext(rq, rc)
|
||||||
h = Logger()(func(c echo.Context) error {
|
h = Logger()(func(c echo.Context) error {
|
||||||
return errors.New("error")
|
return errors.New("error")
|
||||||
})
|
})
|
||||||
@ -53,8 +53,8 @@ func TestLogger(t *testing.T) {
|
|||||||
func TestLoggerIPAddress(t *testing.T) {
|
func TestLoggerIPAddress(t *testing.T) {
|
||||||
e := echo.New()
|
e := echo.New()
|
||||||
rq := test.NewRequest(echo.GET, "/", nil)
|
rq := test.NewRequest(echo.GET, "/", nil)
|
||||||
rec := test.NewResponseRecorder()
|
rc := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(rq, rec, e)
|
c := e.NewContext(rq, rc)
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
e.Logger().SetOutput(buf)
|
e.Logger().SetOutput(buf)
|
||||||
ip := "127.0.0.1"
|
ip := "127.0.0.1"
|
||||||
|
@ -15,12 +15,12 @@ func TestRecover(t *testing.T) {
|
|||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
e.SetLogOutput(buf)
|
e.SetLogOutput(buf)
|
||||||
rq := test.NewRequest(echo.GET, "/", nil)
|
rq := test.NewRequest(echo.GET, "/", nil)
|
||||||
rec := test.NewResponseRecorder()
|
rc := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(rq, rec, e)
|
c := e.NewContext(rq, rc)
|
||||||
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
|
h := Recover()(echo.HandlerFunc(func(c echo.Context) error {
|
||||||
panic("test")
|
panic("test")
|
||||||
}))
|
}))
|
||||||
h(c)
|
h(c)
|
||||||
assert.Equal(t, http.StatusInternalServerError, rec.Status())
|
assert.Equal(t, http.StatusInternalServerError, rc.Status())
|
||||||
assert.Contains(t, buf.String(), "PANIC RECOVER")
|
assert.Contains(t, buf.String(), "PANIC RECOVER")
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ func TestAddTrailingSlash(t *testing.T) {
|
|||||||
e := echo.New()
|
e := echo.New()
|
||||||
rq := test.NewRequest(echo.GET, "/add-slash", nil)
|
rq := test.NewRequest(echo.GET, "/add-slash", nil)
|
||||||
rc := test.NewResponseRecorder()
|
rc := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(rq, rc, e)
|
c := e.NewContext(rq, rc)
|
||||||
h := AddTrailingSlash()(func(c echo.Context) error {
|
h := AddTrailingSlash()(func(c echo.Context) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -24,7 +24,7 @@ func TestAddTrailingSlash(t *testing.T) {
|
|||||||
// With config
|
// With config
|
||||||
rq = test.NewRequest(echo.GET, "/add-slash?key=value", nil)
|
rq = test.NewRequest(echo.GET, "/add-slash?key=value", nil)
|
||||||
rc = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rc, e)
|
c = e.NewContext(rq, rc)
|
||||||
h = AddTrailingSlashWithConfig(TrailingSlashConfig{
|
h = AddTrailingSlashWithConfig(TrailingSlashConfig{
|
||||||
RedirectCode: http.StatusMovedPermanently,
|
RedirectCode: http.StatusMovedPermanently,
|
||||||
})(func(c echo.Context) error {
|
})(func(c echo.Context) error {
|
||||||
@ -39,7 +39,7 @@ func TestRemoveTrailingSlash(t *testing.T) {
|
|||||||
e := echo.New()
|
e := echo.New()
|
||||||
rq := test.NewRequest(echo.GET, "/remove-slash/", nil)
|
rq := test.NewRequest(echo.GET, "/remove-slash/", nil)
|
||||||
rc := test.NewResponseRecorder()
|
rc := test.NewResponseRecorder()
|
||||||
c := echo.NewContext(rq, rc, e)
|
c := e.NewContext(rq, rc)
|
||||||
h := RemoveTrailingSlash()(func(c echo.Context) error {
|
h := RemoveTrailingSlash()(func(c echo.Context) error {
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
@ -50,7 +50,7 @@ func TestRemoveTrailingSlash(t *testing.T) {
|
|||||||
// With config
|
// With config
|
||||||
rq = test.NewRequest(echo.GET, "/remove-slash/?key=value", nil)
|
rq = test.NewRequest(echo.GET, "/remove-slash/?key=value", nil)
|
||||||
rc = test.NewResponseRecorder()
|
rc = test.NewResponseRecorder()
|
||||||
c = echo.NewContext(rq, rc, e)
|
c = e.NewContext(rq, rc)
|
||||||
h = RemoveTrailingSlashWithConfig(TrailingSlashConfig{
|
h = RemoveTrailingSlashWithConfig(TrailingSlashConfig{
|
||||||
RedirectCode: http.StatusMovedPermanently,
|
RedirectCode: http.StatusMovedPermanently,
|
||||||
})(func(c echo.Context) error {
|
})(func(c echo.Context) error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user