Skip to content

Commit

Permalink
test: update test
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Apr 18, 2020
1 parent a613fc0 commit 1a5da37
Show file tree
Hide file tree
Showing 14 changed files with 83 additions and 105 deletions.
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
Expand Down
18 changes: 9 additions & 9 deletions middleware/basic_auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestNoVildatePanic(t *testing.T) {
defer func() {
r := recover()
assert.NotNil(r)
assert.Equal(r.(error), ErrBasicAuthRequireValidateFunction)
assert.Equal(ErrBasicAuthRequireValidateFunction, r.(error))
}()

NewBasicAuth(BasicAuthConfig{})
Expand Down Expand Up @@ -89,8 +89,8 @@ func TestBasicAuth(t *testing.T) {
})
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusUnauthorized)
assert.Equal(resp.Header().Get(elton.HeaderWWWAuthenticate), `basic realm="basic auth tips"`)
assert.Equal(http.StatusUnauthorized, resp.Code)
assert.Equal(`basic realm="basic auth tips"`, resp.Header().Get(elton.HeaderWWWAuthenticate))
})

t.Run("auth validate fail", func(t *testing.T) {
Expand All @@ -103,14 +103,14 @@ func TestBasicAuth(t *testing.T) {
req.Header.Set(elton.HeaderAuthorization, "basic YTpi")
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusUnauthorized)
assert.Equal(resp.Body.String(), "category=elton-basic-auth, message=unAuthorized")
assert.Equal(http.StatusUnauthorized, resp.Code)
assert.Equal("category=elton-basic-auth, message=unAuthorized", resp.Body.String())

req.Header.Set(elton.HeaderAuthorization, "basic bjph")
resp = httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusBadRequest)
assert.Equal(resp.Body.String(), "message=account is invalid")
assert.Equal(http.StatusBadRequest, resp.Code)
assert.Equal("message=account is invalid", resp.Body.String())
})

t.Run("validate error", func(t *testing.T) {
Expand All @@ -127,8 +127,8 @@ func TestBasicAuth(t *testing.T) {
})
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusBadRequest)
assert.Equal(resp.Body.String(), "category=elton-basic-auth, message=abcd")
assert.Equal(http.StatusBadRequest, resp.Code)
assert.Equal("category=elton-basic-auth, message=abcd", resp.Body.String())
})

t.Run("auth success", func(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions middleware/body_parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func TestBodyParser(t *testing.T) {

assert.Nil(err)
assert.True(done)
assert.Equal(c.RequestBody, []byte("a"))
assert.Equal([]byte("a"), c.RequestBody)
})

t.Run("pass method", func(t *testing.T) {
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestBodyParser(t *testing.T) {
c := elton.NewContext(nil, req)
err := bodyParser(c)
assert.NotNil(err)
assert.Equal(err.Error(), "category=elton-body-parser, message=message=abc")
assert.Equal("category=elton-body-parser, message=message=abc", err.Error())
})

t.Run("body over limit size", func(t *testing.T) {
Expand All @@ -243,7 +243,7 @@ func TestBodyParser(t *testing.T) {
c := elton.NewContext(nil, req)
err := bodyParser(c)
assert.NotNil(err)
assert.Equal(err.Error(), "category=elton-body-parser, message=request body is too large, it should be <= 1")
assert.Equal("category=elton-body-parser, message=request body is too large, it should be <= 1", err.Error())
})

t.Run("parse json success", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion middleware/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func TestCompress(t *testing.T) {
}
fn := NewDefaultCompress()
err := fn(c)
assert.Equal(err, customErr)
assert.Equal(customErr, err)
})

t.Run("normal", func(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion middleware/concurrent_limiter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,6 @@ func TestConcurrentLimiter(t *testing.T) {
assert := assert.New(t)
c.Params = new(elton.RouteParams)
err := fn(c)
assert.Equal(err.Error(), "message=key is invalid")
assert.Equal("message=key is invalid", err.Error())
})
}
11 changes: 4 additions & 7 deletions middleware/etag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestETag(t *testing.T) {
return customErr
}
err := fn(c)
assert.Equal(err, customErr)
assert.Equal(customErr, err)
})

t.Run("no body", func(t *testing.T) {
Expand Down Expand Up @@ -100,6 +100,7 @@ func TestETag(t *testing.T) {
})

t.Run("gen eTag", func(t *testing.T) {
assert := assert.New(t)
resp := httptest.NewRecorder()
c := elton.NewContext(resp, nil)
c.Next = func() error {
Expand All @@ -110,12 +111,8 @@ func TestETag(t *testing.T) {
return nil
}
err := fn(c)
if err != nil {
t.Fatalf("eTag middleware fail, %v", err)
}
if c.GetHeader(elton.HeaderETag) != `"13-yo9YroUOjW1obRvVoXfrCiL2JGE="` {
t.Fatalf("gen eTag fail")
}
assert.Nil(err)
assert.Equal(`"13-yo9YroUOjW1obRvVoXfrCiL2JGE="`, c.GetHeader(elton.HeaderETag))
})
}

Expand Down
10 changes: 5 additions & 5 deletions middleware/fresh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func TestFresh(t *testing.T) {
}
fn := NewFresh(FreshConfig{})
err := fn(c)
assert.Equal(err, customErr, "custom error should be return")
assert.Equal(customErr, err, "custom error should be return")
})

t.Run("not modified", func(t *testing.T) {
Expand All @@ -87,7 +87,7 @@ func TestFresh(t *testing.T) {
assert.Nil(err)
assert.True(done)

assert.Equal(c.StatusCode, 304, "status code should be 304")
assert.Equal(304, c.StatusCode, "status code should be 304")
assert.Nil(c.Body, "body should be nil")
assert.Nil(c.BodyBuffer, "body buffer should be nil")
})
Expand All @@ -105,7 +105,7 @@ func TestFresh(t *testing.T) {
c.NoContent()
err := fn(c)
assert.Nil(err)
assert.Equal(c.StatusCode, 204, "no body should be passed by fresh")
assert.Equal(204, c.StatusCode, "no body should be passed by fresh")
})

t.Run("post method", func(t *testing.T) {
Expand All @@ -130,7 +130,7 @@ func TestFresh(t *testing.T) {
assert.Nil(err)
assert.True(done)

assert.Equal(c.StatusCode, 200, "post requset should be passed by fresh")
assert.Equal(200, c.StatusCode, "post requset should be passed by fresh")
assert.NotNil(c.Body, "post requset should be passed by fresh")
assert.NotNil(c.BodyBuffer, "post requset should be passed by fresh")
})
Expand All @@ -157,7 +157,7 @@ func TestFresh(t *testing.T) {
assert.Nil(err)
assert.True(done)

assert.Equal(c.StatusCode, http.StatusBadRequest, "error response should be passed by fresh")
assert.Equal(http.StatusBadRequest, c.StatusCode, "error response should be passed by fresh")
assert.NotNil(c.Body, "error response should be passed by fresh")
assert.NotNil(c.BodyBuffer, "error response should be passed by fresh")
})
Expand Down
38 changes: 10 additions & 28 deletions middleware/logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,12 @@ import (
)

func TestGetHumanReadableSize(t *testing.T) {
if getHumanReadableSize(1024*1024) != "1MB" {
t.Fatalf("1024 * 1024 should be 1MB")
}
if getHumanReadableSize(1024*1024+500*1024) != "1.49MB" {
t.Fatalf("1024*1024+500*1024 should be 1.49MB")
}

if getHumanReadableSize(1024) != "1KB" {
t.Fatalf("1024 should be 1KB")
}
if getHumanReadableSize(1024+500) != "1.49KB" {
t.Fatalf("1024+500 should be 1.49KB")
}
if getHumanReadableSize(500) != "500B" {
t.Fatalf("500 should be 500B")
}
assert := assert.New(t)
assert.Equal("1MB", getHumanReadableSize(1024*1024))
assert.Equal("1.49MB", getHumanReadableSize(1024*1024+500*1024))
assert.Equal("1KB", getHumanReadableSize(1024))
assert.Equal("1.49KB", getHumanReadableSize(1024+500))
assert.Equal("500B", getHumanReadableSize(500))
}

func TestLogger(t *testing.T) {
Expand Down Expand Up @@ -84,9 +74,7 @@ func TestLogger(t *testing.T) {
config := LoggerConfig{
Format: "{latency} {latency-ms}",
OnLog: func(log string, _ *elton.Context) {
if len(strings.Split(log, " ")) != 2 {
t.Fatalf("get latency fail")
}
assert.Equal(2, len(strings.Split(log, " ")))
},
}
m := NewLogger(config)
Expand All @@ -105,9 +93,7 @@ func TestLogger(t *testing.T) {
config := LoggerConfig{
Format: "{when} {when-iso} {when-utc-iso} {when-unix} {when-iso-ms} {when-utc-iso-ms}",
OnLog: func(log string, _ *elton.Context) {
if len(strings.Split(log, " ")) != 6 {
t.Fatalf("get when fail")
}
assert.Equal(6, len(strings.Split(log, " ")))
},
}
m := NewLogger(config)
Expand All @@ -125,9 +111,7 @@ func TestLogger(t *testing.T) {
config := LoggerConfig{
Format: "{~jt}",
OnLog: func(log string, _ *elton.Context) {
if log != "abc" {
t.Fatalf("get cookie value fail")
}
assert.Equal("abc", log, "get cookie value fail")
},
}
m := NewLogger(config)
Expand All @@ -149,9 +133,7 @@ func TestLogger(t *testing.T) {
config := LoggerConfig{
Format: "{>X-Token} {<X-Response-Id} place-holder",
OnLog: func(log string, _ *elton.Context) {
if log != "abc def place-holder" {
t.Fatalf("get header value fail")
}
assert.Equal("abc def place-holder", log, "get header value fail")
},
}
m := NewLogger(config)
Expand Down
20 changes: 10 additions & 10 deletions middleware/proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestNoTargetPanic(t *testing.T) {
assert := assert.New(t)
defer func() {
r := recover()
assert.Equal(r.(error), ErrProxyNoTargetFunction)
assert.Equal(ErrProxyNoTargetFunction, r.(error))
}()
NewProxy(ProxyConfig{})
}
Expand All @@ -63,13 +63,13 @@ func TestGenerateRewrites(t *testing.T) {
"a:b:c",
})
assert.Nil(err)
assert.Equal(len(regs), 0, "rewrite regexp map should be 0")
assert.Equal(0, len(regs), "rewrite regexp map should be 0")

regs, err = generateRewrites([]string{
"/(d/:a",
})
assert.NotNil(err)
assert.Equal(len(regs), 0, "regexp map should be 0 when error occur")
assert.Equal(0, len(regs), "regexp map should be 0 when error occur")
}

func TestProxy(t *testing.T) {
Expand Down Expand Up @@ -98,11 +98,11 @@ func TestProxy(t *testing.T) {
}
err := fn(c)
assert.Nil(err)
assert.Equal(c.GetHeader("Content-Encoding"), "gzip")
assert.Equal(c.Request.URL.Path, originalPath)
assert.Equal(req.Host, originalHost)
assert.Equal("gzip", c.GetHeader("Content-Encoding"))
assert.Equal(originalPath, c.Request.URL.Path)
assert.Equal(originalHost, req.Host)
assert.True(done)
assert.Equal(c.StatusCode, http.StatusOK)
assert.Equal(http.StatusOK, c.StatusCode)
})

t.Run("target picker", func(t *testing.T) {
Expand Down Expand Up @@ -131,7 +131,7 @@ func TestProxy(t *testing.T) {
assert.Nil(err)
assert.True(done)
assert.True(callBackDone)
assert.Equal(c.StatusCode, http.StatusOK)
assert.Equal(http.StatusOK, c.StatusCode)
})

t.Run("target picker error", func(t *testing.T) {
Expand All @@ -148,7 +148,7 @@ func TestProxy(t *testing.T) {
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
err := fn(c)
assert.Equal(err.Error(), "abcd")
assert.Equal("abcd", err.Error())
})

t.Run("no target", func(t *testing.T) {
Expand All @@ -165,7 +165,7 @@ func TestProxy(t *testing.T) {
resp := httptest.NewRecorder()
c := elton.NewContext(resp, req)
err := fn(c)
assert.Equal(err.Error(), "category=elton-proxy, message=target can not be nil")
assert.Equal("category=elton-proxy, message=target can not be nil", err.Error())
})

t.Run("proxy error", func(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions middleware/recover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func TestRecover(t *testing.T) {
})

e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusInternalServerError)
assert.Equal(resp.Body.String(), "category=elton-recover, message=abc")
assert.Equal(http.StatusInternalServerError, resp.Code)
assert.Equal("category=elton-recover, message=abc", resp.Body.String())
assert.True(ctx.Committed)
assert.True(catchError)
for _, key := range keys {
Expand Down
18 changes: 9 additions & 9 deletions middleware/responder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ import (

func checkResponse(t *testing.T, resp *httptest.ResponseRecorder, code int, data string) {
assert := assert.New(t)
assert.Equal(resp.Body.String(), data)
assert.Equal(resp.Code, code)
assert.Equal(data, resp.Body.String())
assert.Equal(code, resp.Code)
}

func checkJSON(t *testing.T, resp *httptest.ResponseRecorder) {
assert := assert.New(t)
assert.Equal(resp.Header().Get(elton.HeaderContentType), elton.MIMEApplicationJSON)
assert.Equal(elton.MIMEApplicationJSON, resp.Header().Get(elton.HeaderContentType))
}

func checkContentType(t *testing.T, resp *httptest.ResponseRecorder, contentType string) {
assert := assert.New(t)
assert.Equal(resp.Header().Get(elton.HeaderContentType), contentType)
assert.Equal(contentType, resp.Header().Get(elton.HeaderContentType))
}

func TestResponder(t *testing.T) {
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestResponder(t *testing.T) {
}
fn := NewDefaultResponder()
err := fn(c)
assert.Equal(err, customErr)
assert.Equal(customErr, err)
assert.True(done)
})

Expand Down Expand Up @@ -195,8 +195,8 @@ func TestResponder(t *testing.T) {
})
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, 200)
assert.Equal(resp.Body.String(), "abcd")
assert.Equal(200, resp.Code)
assert.Equal("abcd", resp.Body.String())
})

t.Run("no content", func(t *testing.T) {
Expand All @@ -209,7 +209,7 @@ func TestResponder(t *testing.T) {
})
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Equal(resp.Code, http.StatusNoContent)
assert.Equal(http.StatusNoContent, resp.Code)
})

t.Run("accepted(202)", func(t *testing.T) {
Expand All @@ -223,6 +223,6 @@ func TestResponder(t *testing.T) {
resp := httptest.NewRecorder()
e.ServeHTTP(resp, req)
assert.Empty(resp.Body)
assert.Equal(resp.Code, http.StatusAccepted)
assert.Equal(http.StatusAccepted, resp.Code)
})
}
Loading

0 comments on commit 1a5da37

Please sign in to comment.