mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-04 01:38:12 +08:00
test: improve coverage to meet 99% threshold
- Add TestContextGetRawDataNilBody to cover nil body error case - Add SameSiteDefaultMode test case in SetCookieData - Add 400ms latency test case for LatencyColor - Add TestMarshalXMLforHSuccess for successful XML marshaling Coverage improved from 99.1% to 99.2%
This commit is contained in:
parent
65c72a8059
commit
1eb3a54158
@ -2948,6 +2948,16 @@ func TestContextGetRawData(t *testing.T) {
|
||||
assert.Equal(t, "Fetch binary post data", string(data))
|
||||
}
|
||||
|
||||
func TestContextGetRawDataNilBody(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)
|
||||
|
||||
data, err := c.GetRawData()
|
||||
assert.Nil(t, data)
|
||||
assert.Error(t, err)
|
||||
assert.Equal(t, "cannot read nil body", err.Error())
|
||||
}
|
||||
|
||||
func TestContextRenderDataFromReader(t *testing.T) {
|
||||
w := httptest.NewRecorder()
|
||||
c, _ := CreateTestContext(w)
|
||||
@ -3536,6 +3546,24 @@ func TestContextSetCookieData(t *testing.T) {
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "SameSite=None")
|
||||
})
|
||||
|
||||
// Test that SameSiteDefaultMode inherits from context's sameSite
|
||||
t.Run("SameSiteDefaultMode inherits context sameSite", func(t *testing.T) {
|
||||
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||
c.SetSameSite(http.SameSiteStrictMode)
|
||||
cookie := &http.Cookie{
|
||||
Name: "user",
|
||||
Value: "gin",
|
||||
Path: "/",
|
||||
Domain: "localhost",
|
||||
Secure: true,
|
||||
HttpOnly: true,
|
||||
SameSite: http.SameSiteDefaultMode,
|
||||
}
|
||||
c.SetCookieData(cookie)
|
||||
setCookie := c.Writer.Header().Get("Set-Cookie")
|
||||
assert.Contains(t, setCookie, "SameSite=Strict")
|
||||
})
|
||||
}
|
||||
|
||||
func TestGetMapFromFormData(t *testing.T) {
|
||||
|
||||
@ -329,6 +329,7 @@ func TestColorForLatency(t *testing.T) {
|
||||
assert.Equal(t, white, colorForLatency(time.Millisecond*20), "20ms should be white")
|
||||
assert.Equal(t, green, colorForLatency(time.Millisecond*150), "150ms should be green")
|
||||
assert.Equal(t, cyan, colorForLatency(time.Millisecond*250), "250ms should be cyan")
|
||||
assert.Equal(t, blue, colorForLatency(time.Millisecond*400), "400ms should be blue")
|
||||
assert.Equal(t, yellow, colorForLatency(time.Millisecond*600), "600ms should be yellow")
|
||||
assert.Equal(t, magenta, colorForLatency(time.Millisecond*1500), "1.5s should be magenta")
|
||||
assert.Equal(t, red, colorForLatency(time.Second*3), "other things should be red")
|
||||
|
||||
@ -145,6 +145,17 @@ func TestMarshalXMLforH(t *testing.T) {
|
||||
assert.Error(t, e)
|
||||
}
|
||||
|
||||
func TestMarshalXMLforHSuccess(t *testing.T) {
|
||||
h := H{
|
||||
"key1": "value1",
|
||||
"key2": 123,
|
||||
}
|
||||
data, err := xml.Marshal(h)
|
||||
assert.NoError(t, err)
|
||||
assert.Contains(t, string(data), "<key1>value1</key1>")
|
||||
assert.Contains(t, string(data), "<key2>123</key2>")
|
||||
}
|
||||
|
||||
func TestIsASCII(t *testing.T) {
|
||||
assert.True(t, isASCII("test"))
|
||||
assert.False(t, isASCII("🧡💛💚💙💜"))
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user