mirror of
https://github.com/gin-gonic/gin.git
synced 2025-07-15 04:00:21 +08:00
chore: use http.Status* instead of hard code (#1482)
This commit is contained in:
@ -6,6 +6,7 @@ package gin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@ -37,7 +38,7 @@ func TestMiddlewareGeneralCase(t *testing.T) {
|
||||
w := performRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, 200, w.Code)
|
||||
assert.Equal(t, http.StatusOK, w.Code)
|
||||
assert.Equal(t, "ACDB", signature)
|
||||
}
|
||||
|
||||
@ -73,7 +74,7 @@ func TestMiddlewareNoRoute(t *testing.T) {
|
||||
w := performRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, 404, w.Code)
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
assert.Equal(t, "ACEGHFDB", signature)
|
||||
}
|
||||
|
||||
@ -110,7 +111,7 @@ func TestMiddlewareNoMethodEnabled(t *testing.T) {
|
||||
w := performRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, 405, w.Code)
|
||||
assert.Equal(t, http.StatusMethodNotAllowed, w.Code)
|
||||
assert.Equal(t, "ACEGHFDB", signature)
|
||||
}
|
||||
|
||||
@ -147,7 +148,7 @@ func TestMiddlewareNoMethodDisabled(t *testing.T) {
|
||||
w := performRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, 404, w.Code)
|
||||
assert.Equal(t, http.StatusNotFound, w.Code)
|
||||
assert.Equal(t, "AC X DB", signature)
|
||||
}
|
||||
|
||||
@ -159,7 +160,7 @@ func TestMiddlewareAbort(t *testing.T) {
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
signature += "C"
|
||||
c.AbortWithStatus(401)
|
||||
c.AbortWithStatus(http.StatusUnauthorized)
|
||||
c.Next()
|
||||
signature += "D"
|
||||
})
|
||||
@ -173,7 +174,7 @@ func TestMiddlewareAbort(t *testing.T) {
|
||||
w := performRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, 401, w.Code)
|
||||
assert.Equal(t, http.StatusUnauthorized, w.Code)
|
||||
assert.Equal(t, "ACD", signature)
|
||||
}
|
||||
|
||||
@ -183,7 +184,7 @@ func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
||||
router.Use(func(c *Context) {
|
||||
signature += "A"
|
||||
c.Next()
|
||||
c.AbortWithStatus(410)
|
||||
c.AbortWithStatus(http.StatusGone)
|
||||
signature += "B"
|
||||
|
||||
})
|
||||
@ -195,7 +196,7 @@ func TestMiddlewareAbortHandlersChainAndNext(t *testing.T) {
|
||||
w := performRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, 410, w.Code)
|
||||
assert.Equal(t, http.StatusGone, w.Code)
|
||||
assert.Equal(t, "ACB", signature)
|
||||
}
|
||||
|
||||
@ -207,7 +208,7 @@ func TestMiddlewareFailHandlersChain(t *testing.T) {
|
||||
router := New()
|
||||
router.Use(func(context *Context) {
|
||||
signature += "A"
|
||||
context.AbortWithError(500, errors.New("foo"))
|
||||
context.AbortWithError(http.StatusInternalServerError, errors.New("foo"))
|
||||
})
|
||||
router.Use(func(context *Context) {
|
||||
signature += "B"
|
||||
@ -218,25 +219,25 @@ func TestMiddlewareFailHandlersChain(t *testing.T) {
|
||||
w := performRequest(router, "GET", "/")
|
||||
|
||||
// TEST
|
||||
assert.Equal(t, 500, w.Code)
|
||||
assert.Equal(t, http.StatusInternalServerError, w.Code)
|
||||
assert.Equal(t, "A", signature)
|
||||
}
|
||||
|
||||
func TestMiddlewareWrite(t *testing.T) {
|
||||
router := New()
|
||||
router.Use(func(c *Context) {
|
||||
c.String(400, "hola\n")
|
||||
c.String(http.StatusBadRequest, "hola\n")
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
c.XML(400, H{"foo": "bar"})
|
||||
c.XML(http.StatusBadRequest, H{"foo": "bar"})
|
||||
})
|
||||
router.Use(func(c *Context) {
|
||||
c.JSON(400, H{"foo": "bar"})
|
||||
c.JSON(http.StatusBadRequest, H{"foo": "bar"})
|
||||
})
|
||||
router.GET("/", func(c *Context) {
|
||||
c.JSON(400, H{"foo": "bar"})
|
||||
c.JSON(http.StatusBadRequest, H{"foo": "bar"})
|
||||
}, func(c *Context) {
|
||||
c.Render(400, sse.Event{
|
||||
c.Render(http.StatusBadRequest, sse.Event{
|
||||
Event: "test",
|
||||
Data: "message",
|
||||
})
|
||||
@ -244,6 +245,6 @@ func TestMiddlewareWrite(t *testing.T) {
|
||||
|
||||
w := performRequest(router, "GET", "/")
|
||||
|
||||
assert.Equal(t, 400, w.Code)
|
||||
assert.Equal(t, http.StatusBadRequest, w.Code)
|
||||
assert.Equal(t, strings.Replace("hola\n<map><foo>bar</foo></map>{\"foo\":\"bar\"}{\"foo\":\"bar\"}event:test\ndata:message\n\n", " ", "", -1), strings.Replace(w.Body.String(), " ", "", -1))
|
||||
}
|
||||
|
Reference in New Issue
Block a user