diff --git a/go.sum b/go.sum index 2c33c1b..42ba14f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/middleware/basic_auth_test.go b/middleware/basic_auth_test.go index 5e273b3..d408c47 100644 --- a/middleware/basic_auth_test.go +++ b/middleware/basic_auth_test.go @@ -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{}) @@ -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) { @@ -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) { @@ -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) { diff --git a/middleware/body_parser_test.go b/middleware/body_parser_test.go index 5bdc9fd..05633c7 100644 --- a/middleware/body_parser_test.go +++ b/middleware/body_parser_test.go @@ -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) { @@ -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) { @@ -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) { diff --git a/middleware/compress_test.go b/middleware/compress_test.go index 959aeb6..4fbc7ef 100644 --- a/middleware/compress_test.go +++ b/middleware/compress_test.go @@ -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) { diff --git a/middleware/concurrent_limiter_test.go b/middleware/concurrent_limiter_test.go index 1a14aa8..0ede2fa 100644 --- a/middleware/concurrent_limiter_test.go +++ b/middleware/concurrent_limiter_test.go @@ -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()) }) } diff --git a/middleware/etag_test.go b/middleware/etag_test.go index 6535662..d12c683 100644 --- a/middleware/etag_test.go +++ b/middleware/etag_test.go @@ -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) { @@ -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 { @@ -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)) }) } diff --git a/middleware/fresh_test.go b/middleware/fresh_test.go index 7def140..291c58d 100644 --- a/middleware/fresh_test.go +++ b/middleware/fresh_test.go @@ -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) { @@ -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") }) @@ -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) { @@ -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") }) @@ -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") }) diff --git a/middleware/logger_test.go b/middleware/logger_test.go index 479553a..45cc701 100644 --- a/middleware/logger_test.go +++ b/middleware/logger_test.go @@ -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) { @@ -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) @@ -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) @@ -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) @@ -149,9 +133,7 @@ func TestLogger(t *testing.T) { config := LoggerConfig{ Format: "{>X-Token} {