mirror of https://github.com/gin-gonic/gin.git
refactor(test): unify assert.Equal usage (#1054)
This commit is contained in:
parent
4b54b86272
commit
81007d2ce0
20
auth_test.go
20
auth_test.go
|
@ -53,15 +53,15 @@ func TestBasicAuthSearchCredential(t *testing.T) {
|
||||||
})
|
})
|
||||||
|
|
||||||
user, found := pairs.searchCredential(authorizationHeader("admin", "password"))
|
user, found := pairs.searchCredential(authorizationHeader("admin", "password"))
|
||||||
assert.Equal(t, user, "admin")
|
assert.Equal(t, "admin", user)
|
||||||
assert.True(t, found)
|
assert.True(t, found)
|
||||||
|
|
||||||
user, found = pairs.searchCredential(authorizationHeader("foo", "bar"))
|
user, found = pairs.searchCredential(authorizationHeader("foo", "bar"))
|
||||||
assert.Equal(t, user, "foo")
|
assert.Equal(t, "foo", user)
|
||||||
assert.True(t, found)
|
assert.True(t, found)
|
||||||
|
|
||||||
user, found = pairs.searchCredential(authorizationHeader("bar", "foo"))
|
user, found = pairs.searchCredential(authorizationHeader("bar", "foo"))
|
||||||
assert.Equal(t, user, "bar")
|
assert.Equal(t, "bar", user)
|
||||||
assert.True(t, found)
|
assert.True(t, found)
|
||||||
|
|
||||||
user, found = pairs.searchCredential(authorizationHeader("admins", "password"))
|
user, found = pairs.searchCredential(authorizationHeader("admins", "password"))
|
||||||
|
@ -78,7 +78,7 @@ func TestBasicAuthSearchCredential(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBasicAuthAuthorizationHeader(t *testing.T) {
|
func TestBasicAuthAuthorizationHeader(t *testing.T) {
|
||||||
assert.Equal(t, authorizationHeader("admin", "password"), "Basic YWRtaW46cGFzc3dvcmQ=")
|
assert.Equal(t, "Basic YWRtaW46cGFzc3dvcmQ=", authorizationHeader("admin", "password"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBasicAuthSecureCompare(t *testing.T) {
|
func TestBasicAuthSecureCompare(t *testing.T) {
|
||||||
|
@ -101,8 +101,8 @@ func TestBasicAuthSucceed(t *testing.T) {
|
||||||
req.Header.Set("Authorization", authorizationHeader("admin", "password"))
|
req.Header.Set("Authorization", authorizationHeader("admin", "password"))
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
assert.Equal(t, w.Code, 200)
|
assert.Equal(t, 200, w.Code)
|
||||||
assert.Equal(t, w.Body.String(), "admin")
|
assert.Equal(t, "admin", w.Body.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBasicAuth401(t *testing.T) {
|
func TestBasicAuth401(t *testing.T) {
|
||||||
|
@ -121,8 +121,8 @@ func TestBasicAuth401(t *testing.T) {
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
assert.False(t, called)
|
assert.False(t, called)
|
||||||
assert.Equal(t, w.Code, 401)
|
assert.Equal(t, 401, w.Code)
|
||||||
assert.Equal(t, w.HeaderMap.Get("WWW-Authenticate"), "Basic realm=\"Authorization Required\"")
|
assert.Equal(t, "Basic realm=\"Authorization Required\"", w.HeaderMap.Get("WWW-Authenticate"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBasicAuth401WithCustomRealm(t *testing.T) {
|
func TestBasicAuth401WithCustomRealm(t *testing.T) {
|
||||||
|
@ -141,6 +141,6 @@ func TestBasicAuth401WithCustomRealm(t *testing.T) {
|
||||||
router.ServeHTTP(w, req)
|
router.ServeHTTP(w, req)
|
||||||
|
|
||||||
assert.False(t, called)
|
assert.False(t, called)
|
||||||
assert.Equal(t, w.Code, 401)
|
assert.Equal(t, 401, w.Code)
|
||||||
assert.Equal(t, w.HeaderMap.Get("WWW-Authenticate"), "Basic realm=\"My Custom \\\"Realm\\\"\"")
|
assert.Equal(t, "Basic realm=\"My Custom \\\"Realm\\\"\"", w.HeaderMap.Get("WWW-Authenticate"))
|
||||||
}
|
}
|
||||||
|
|
|
@ -42,7 +42,7 @@ func TestDebugPrint(t *testing.T) {
|
||||||
|
|
||||||
SetMode(DebugMode)
|
SetMode(DebugMode)
|
||||||
debugPrint("these are %d %s\n", 2, "error messages")
|
debugPrint("these are %d %s\n", 2, "error messages")
|
||||||
assert.Equal(t, w.String(), "[GIN-debug] these are 2 error messages\n")
|
assert.Equal(t, "[GIN-debug] these are 2 error messages\n", w.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDebugPrintError(t *testing.T) {
|
func TestDebugPrintError(t *testing.T) {
|
||||||
|
@ -55,7 +55,7 @@ func TestDebugPrintError(t *testing.T) {
|
||||||
assert.Empty(t, w.String())
|
assert.Empty(t, w.String())
|
||||||
|
|
||||||
debugPrintError(errors.New("this is an error"))
|
debugPrintError(errors.New("this is an error"))
|
||||||
assert.Equal(t, w.String(), "[GIN-debug] [ERROR] this is an error\n")
|
assert.Equal(t, "[GIN-debug] [ERROR] this is an error\n", w.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDebugPrintRoutes(t *testing.T) {
|
func TestDebugPrintRoutes(t *testing.T) {
|
||||||
|
@ -83,7 +83,7 @@ func TestDebugPrintWARNINGSetHTMLTemplate(t *testing.T) {
|
||||||
defer teardown()
|
defer teardown()
|
||||||
|
|
||||||
debugPrintWARNINGSetHTMLTemplate()
|
debugPrintWARNINGSetHTMLTemplate()
|
||||||
assert.Equal(t, w.String(), "[GIN-debug] [WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called\nat initialization. ie. before any route is registered or the router is listening in a socket:\n\n\trouter := gin.Default()\n\trouter.SetHTMLTemplate(template) // << good place\n\n")
|
assert.Equal(t, "[GIN-debug] [WARNING] Since SetHTMLTemplate() is NOT thread-safe. It should only be called\nat initialization. ie. before any route is registered or the router is listening in a socket:\n\n\trouter := gin.Default()\n\trouter.SetHTMLTemplate(template) // << good place\n\n", w.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
func setup(w io.Writer) {
|
func setup(w io.Writer) {
|
||||||
|
|
|
@ -60,7 +60,7 @@ func TestError(t *testing.T) {
|
||||||
data string
|
data string
|
||||||
}
|
}
|
||||||
err.SetMeta(customError{status: "200", data: "other data"})
|
err.SetMeta(customError{status: "200", data: "other data"})
|
||||||
assert.Equal(t, err.JSON(), customError{status: "200", data: "other data"})
|
assert.Equal(t, customError{status: "200", data: "other data"}, err.JSON())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestErrorSlice(t *testing.T) {
|
func TestErrorSlice(t *testing.T) {
|
||||||
|
@ -71,33 +71,33 @@ func TestErrorSlice(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
assert.Equal(t, errs, errs.ByType(ErrorTypeAny))
|
assert.Equal(t, errs, errs.ByType(ErrorTypeAny))
|
||||||
assert.Equal(t, errs.Last().Error(), "third")
|
assert.Equal(t, "third", errs.Last().Error())
|
||||||
assert.Equal(t, errs.Errors(), []string{"first", "second", "third"})
|
assert.Equal(t, []string{"first", "second", "third"}, errs.Errors())
|
||||||
assert.Equal(t, errs.ByType(ErrorTypePublic).Errors(), []string{"third"})
|
assert.Equal(t, []string{"third"}, errs.ByType(ErrorTypePublic).Errors())
|
||||||
assert.Equal(t, errs.ByType(ErrorTypePrivate).Errors(), []string{"first", "second"})
|
assert.Equal(t, []string{"first", "second"}, errs.ByType(ErrorTypePrivate).Errors())
|
||||||
assert.Equal(t, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors(), []string{"first", "second", "third"})
|
assert.Equal(t, []string{"first", "second", "third"}, errs.ByType(ErrorTypePublic|ErrorTypePrivate).Errors())
|
||||||
assert.Empty(t, errs.ByType(ErrorTypeBind))
|
assert.Empty(t, errs.ByType(ErrorTypeBind))
|
||||||
assert.Empty(t, errs.ByType(ErrorTypeBind).String())
|
assert.Empty(t, errs.ByType(ErrorTypeBind).String())
|
||||||
|
|
||||||
assert.Equal(t, errs.String(), `Error #01: first
|
assert.Equal(t, `Error #01: first
|
||||||
Error #02: second
|
Error #02: second
|
||||||
Meta: some data
|
Meta: some data
|
||||||
Error #03: third
|
Error #03: third
|
||||||
Meta: map[status:400]
|
Meta: map[status:400]
|
||||||
`)
|
`, errs.String())
|
||||||
assert.Equal(t, errs.JSON(), []interface{}{
|
assert.Equal(t, []interface{}{
|
||||||
H{"error": "first"},
|
H{"error": "first"},
|
||||||
H{"error": "second", "meta": "some data"},
|
H{"error": "second", "meta": "some data"},
|
||||||
H{"error": "third", "status": "400"},
|
H{"error": "third", "status": "400"},
|
||||||
})
|
}, errs.JSON())
|
||||||
jsonBytes, _ := json.Marshal(errs)
|
jsonBytes, _ := json.Marshal(errs)
|
||||||
assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))
|
assert.Equal(t, "[{\"error\":\"first\"},{\"error\":\"second\",\"meta\":\"some data\"},{\"error\":\"third\",\"status\":\"400\"}]", string(jsonBytes))
|
||||||
errs = errorMsgs{
|
errs = errorMsgs{
|
||||||
{Err: errors.New("first"), Type: ErrorTypePrivate},
|
{Err: errors.New("first"), Type: ErrorTypePrivate},
|
||||||
}
|
}
|
||||||
assert.Equal(t, errs.JSON(), H{"error": "first"})
|
assert.Equal(t, H{"error": "first"}, errs.JSON())
|
||||||
jsonBytes, _ = json.Marshal(errs)
|
jsonBytes, _ = json.Marshal(errs)
|
||||||
assert.Equal(t, string(jsonBytes), "{\"error\":\"first\"}")
|
assert.Equal(t, "{\"error\":\"first\"}", string(jsonBytes))
|
||||||
|
|
||||||
errs = errorMsgs{}
|
errs = errorMsgs{}
|
||||||
assert.Nil(t, errs.Last())
|
assert.Nil(t, errs.Last())
|
||||||
|
|
|
@ -37,8 +37,8 @@ func TestMiddlewareGeneralCase(t *testing.T) {
|
||||||
w := performRequest(router, "GET", "/")
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, w.Code, 200)
|
assert.Equal(t, 200, w.Code)
|
||||||
assert.Equal(t, signature, "ACDB")
|
assert.Equal(t, "ACDB", signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMiddlewareNoRoute(t *testing.T) {
|
func TestMiddlewareNoRoute(t *testing.T) {
|
||||||
|
@ -73,8 +73,8 @@ func TestMiddlewareNoRoute(t *testing.T) {
|
||||||
w := performRequest(router, "GET", "/")
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, w.Code, 404)
|
assert.Equal(t, 404, w.Code)
|
||||||
assert.Equal(t, signature, "ACEGHFDB")
|
assert.Equal(t, "ACEGHFDB", signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMiddlewareNoMethodEnabled(t *testing.T) {
|
func TestMiddlewareNoMethodEnabled(t *testing.T) {
|
||||||
|
@ -110,8 +110,8 @@ func TestMiddlewareNoMethodEnabled(t *testing.T) {
|
||||||
w := performRequest(router, "GET", "/")
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, w.Code, 405)
|
assert.Equal(t, 405, w.Code)
|
||||||
assert.Equal(t, signature, "ACEGHFDB")
|
assert.Equal(t, "ACEGHFDB", signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMiddlewareNoMethodDisabled(t *testing.T) {
|
func TestMiddlewareNoMethodDisabled(t *testing.T) {
|
||||||
|
@ -147,8 +147,8 @@ func TestMiddlewareNoMethodDisabled(t *testing.T) {
|
||||||
w := performRequest(router, "GET", "/")
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, w.Code, 404)
|
assert.Equal(t, 404, w.Code)
|
||||||
assert.Equal(t, signature, "AC X DB")
|
assert.Equal(t, "AC X DB", signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMiddlewareAbort(t *testing.T) {
|
func TestMiddlewareAbort(t *testing.T) {
|
||||||
|
@ -173,8 +173,8 @@ func TestMiddlewareAbort(t *testing.T) {
|
||||||
w := performRequest(router, "GET", "/")
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, w.Code, 401)
|
assert.Equal(t, 401, w.Code)
|
||||||
assert.Equal(t, signature, "ACD")
|
assert.Equal(t, "ACD", signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
||||||
|
@ -195,8 +195,8 @@ func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
||||||
w := performRequest(router, "GET", "/")
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, w.Code, 410)
|
assert.Equal(t, 410, w.Code)
|
||||||
assert.Equal(t, signature, "ACB")
|
assert.Equal(t, "ACB", signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
|
// TestFailHandlersChain - ensure that Fail interrupt used middleware in fifo order as
|
||||||
|
@ -218,8 +218,8 @@ func TestMiddlewareFailHandlersChain(t *testing.T) {
|
||||||
w := performRequest(router, "GET", "/")
|
w := performRequest(router, "GET", "/")
|
||||||
|
|
||||||
// TEST
|
// TEST
|
||||||
assert.Equal(t, w.Code, 500)
|
assert.Equal(t, 500, w.Code)
|
||||||
assert.Equal(t, signature, "A")
|
assert.Equal(t, "A", signature)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMiddlewareWrite(t *testing.T) {
|
func TestMiddlewareWrite(t *testing.T) {
|
||||||
|
|
|
@ -34,11 +34,11 @@ func TestResponseWriterReset(t *testing.T) {
|
||||||
var w ResponseWriter = writer
|
var w ResponseWriter = writer
|
||||||
|
|
||||||
writer.reset(testWritter)
|
writer.reset(testWritter)
|
||||||
assert.Equal(t, writer.size, -1)
|
assert.Equal(t, -1, writer.size)
|
||||||
assert.Equal(t, writer.status, 200)
|
assert.Equal(t, 200, writer.status)
|
||||||
assert.Equal(t, writer.ResponseWriter, testWritter)
|
assert.Equal(t, testWritter, writer.ResponseWriter)
|
||||||
assert.Equal(t, w.Size(), -1)
|
assert.Equal(t, -1, w.Size())
|
||||||
assert.Equal(t, w.Status(), 200)
|
assert.Equal(t, 200, w.Status())
|
||||||
assert.False(t, w.Written())
|
assert.False(t, w.Written())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,11 +50,11 @@ func TestResponseWriterWriteHeader(t *testing.T) {
|
||||||
|
|
||||||
w.WriteHeader(300)
|
w.WriteHeader(300)
|
||||||
assert.False(t, w.Written())
|
assert.False(t, w.Written())
|
||||||
assert.Equal(t, w.Status(), 300)
|
assert.Equal(t, 300, w.Status())
|
||||||
assert.NotEqual(t, testWritter.Code, 300)
|
assert.NotEqual(t, testWritter.Code, 300)
|
||||||
|
|
||||||
w.WriteHeader(-1)
|
w.WriteHeader(-1)
|
||||||
assert.Equal(t, w.Status(), 300)
|
assert.Equal(t, 300, w.Status())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseWriterWriteHeadersNow(t *testing.T) {
|
func TestResponseWriterWriteHeadersNow(t *testing.T) {
|
||||||
|
@ -67,12 +67,12 @@ func TestResponseWriterWriteHeadersNow(t *testing.T) {
|
||||||
w.WriteHeaderNow()
|
w.WriteHeaderNow()
|
||||||
|
|
||||||
assert.True(t, w.Written())
|
assert.True(t, w.Written())
|
||||||
assert.Equal(t, w.Size(), 0)
|
assert.Equal(t, 0, w.Size())
|
||||||
assert.Equal(t, testWritter.Code, 300)
|
assert.Equal(t, 300, testWritter.Code)
|
||||||
|
|
||||||
writer.size = 10
|
writer.size = 10
|
||||||
w.WriteHeaderNow()
|
w.WriteHeaderNow()
|
||||||
assert.Equal(t, w.Size(), 10)
|
assert.Equal(t, 10, w.Size())
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestResponseWriterWrite(t *testing.T) {
|
func TestResponseWriterWrite(t *testing.T) {
|
||||||
|
@ -82,17 +82,17 @@ func TestResponseWriterWrite(t *testing.T) {
|
||||||
w := ResponseWriter(writer)
|
w := ResponseWriter(writer)
|
||||||
|
|
||||||
n, err := w.Write([]byte("hola"))
|
n, err := w.Write([]byte("hola"))
|
||||||
assert.Equal(t, n, 4)
|
assert.Equal(t, 4, n)
|
||||||
assert.Equal(t, w.Size(), 4)
|
assert.Equal(t, 4, w.Size())
|
||||||
assert.Equal(t, w.Status(), 200)
|
assert.Equal(t, 200, w.Status())
|
||||||
assert.Equal(t, testWritter.Code, 200)
|
assert.Equal(t, 200, testWritter.Code)
|
||||||
assert.Equal(t, testWritter.Body.String(), "hola")
|
assert.Equal(t, "hola", testWritter.Body.String())
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
n, err = w.Write([]byte(" adios"))
|
n, err = w.Write([]byte(" adios"))
|
||||||
assert.Equal(t, n, 6)
|
assert.Equal(t, 6, n)
|
||||||
assert.Equal(t, w.Size(), 10)
|
assert.Equal(t, 10, w.Size())
|
||||||
assert.Equal(t, testWritter.Body.String(), "hola adios")
|
assert.Equal(t, "hola adios", testWritter.Body.String())
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue