mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +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()
|
||||
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
|
||||
|
@ -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))
|
||||
}
|
||||
|
@ -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,
|
||||
})
|
||||
@ -28,7 +28,7 @@ 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))
|
||||
@ -36,7 +36,7 @@ func TestCORS(t *testing.T) {
|
||||
// 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"},
|
||||
@ -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)
|
||||
|
@ -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")
|
||||
})
|
||||
@ -25,16 +25,16 @@ 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")
|
||||
})
|
||||
@ -42,8 +42,8 @@ func TestLogger(t *testing.T) {
|
||||
|
||||
// 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")
|
||||
})
|
||||
@ -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"
|
||||
|
@ -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")
|
||||
}
|
||||
|
@ -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
|
||||
})
|
||||
@ -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 {
|
||||
@ -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
|
||||
})
|
||||
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user