Skip to content
Myr

test(context): use http.StatusContinue constant instead of magic number 100 (#4542)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* refactor(context): use http.StatusContinue constant instead of magic number 100 Replace magic number 100 with http.StatusContinue constant for better code clarity and maintainability in bodyAllowedForStatus function. Also fix typo in logger_test.go: colorForLantency -> colorForLatency Fixes #4489 * 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% * fix: use require for error assertions (testifylint)

authored by Mehrdad Banikian · committed by GitHub · parent 6f1d5fe · Browse files · 4 files+51 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

4 files+51 added−8 removed

  • other:+51 added−8 removed

4 files+51 added−8 removed

modified:context_test.go+29 added
1029 unmodified lines
1030 require.Error(t, err)1031}10321033func TestContextBodyAllowedForStatus(t *testing.T) {1034 assert.False(t, bodyAllowedForStatus(http.StatusContinue))1035 assert.False(t, bodyAllowedForStatus(http.StatusProcessing))1036 assert.False(t, bodyAllowedForStatus(http.StatusNoContent))1037 assert.False(t, bodyAllowedForStatus(http.StatusNotModified))1038 assert.True(t, bodyAllowedForStatus(http.StatusInternalServerError))
1908 unmodified lines
2947 require.NoError(t, err)2948 assert.Equal(t, "Fetch binary post data", string(data))2949}29502951func TestContextGetRawDataNilBody(t *testing.T) {2952 c, _ := CreateTestContext(httptest.NewRecorder())2953 c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)29542955 data, err := c.GetRawData()2956 assert.Nil(t, data)2957 require.Error(t, err)2958 assert.Equal(t, "cannot read nil body", err.Error())2959}29602961func TestContextRenderDataFromReader(t *testing.T) {2962 w := httptest.NewRecorder()2963 c, _ := CreateTestContext(w)2964
580 unmodified lines
3545 c.SetCookieData(cookie)3546 setCookie := c.Writer.Header().Get("Set-Cookie")3547 assert.Contains(t, setCookie, "SameSite=None")3548 })35493550 // Test that SameSiteDefaultMode inherits from context's sameSite3551 t.Run("SameSiteDefaultMode inherits context sameSite", func(t *testing.T) {3552 c, _ := CreateTestContext(httptest.NewRecorder())3553 c.SetSameSite(http.SameSiteStrictMode)3554 cookie := &http.Cookie{3555 Name: "user",3556 Value: "gin",3557 Path: "/",3558 Domain: "localhost",3559 Secure: true,3560 HttpOnly: true,3561 SameSite: http.SameSiteDefaultMode,3562 }3563 c.SetCookieData(cookie)3564 setCookie := c.Writer.Header().Get("Set-Cookie")3565 assert.Contains(t, setCookie, "SameSite=Strict")3566 })3567}35683569func TestGetMapFromFormData(t *testing.T) {3570 testCases := []struct {
213 unmodified lines
modified:context.go+1 added
1054 unmodified lines
1055/******** RESPONSE RENDERING ********/1056/************************************/10571058// bodyAllowedForStatus is a copy of http.bodyAllowedForStatus non-exported function.1059// Uses http.StatusContinue constant for better code clarity.1060func bodyAllowedForStatus(status int) bool {1061 switch {1062 case status >= http.StatusContinue && status < http.StatusOK:1063 return false
420 unmodified lines
modified:logger_test.go+9 added-8 removed
316 unmodified lines
317 assert.Equal(t, red, colorForStatus(2), "other things should be red")318}319320func TestColorForLatency(t *testing.T) {321 colorForLantency := func(latency time.Duration) string {321 colorForLatency := func(latency time.Duration) string {322 p := LogFormatterParams{323 Latency: latency,324 }325 return p.LatencyColor()326 }327328 assert.Equal(t, white, colorForLantency(time.Duration(0)), "0 should be white")329 assert.Equal(t, white, colorForLantency(time.Millisecond*20), "20ms should be white")330 assert.Equal(t, green, colorForLantency(time.Millisecond*150), "150ms should be green")331 assert.Equal(t, cyan, colorForLantency(time.Millisecond*250), "250ms should be cyan")332 assert.Equal(t, yellow, colorForLantency(time.Millisecond*600), "600ms should be yellow")333 assert.Equal(t, magenta, colorForLantency(time.Millisecond*1500), "1.5s should be magenta")334 assert.Equal(t, red, colorForLantency(time.Second*3), "other things should be red")328 assert.Equal(t, white, colorForLatency(time.Duration(0)), "0 should be white")329 assert.Equal(t, white, colorForLatency(time.Millisecond*20), "20ms should be white")330 assert.Equal(t, green, colorForLatency(time.Millisecond*150), "150ms should be green")331 assert.Equal(t, cyan, colorForLatency(time.Millisecond*250), "250ms should be cyan")332 assert.Equal(t, blue, colorForLatency(time.Millisecond*400), "400ms should be blue")333 assert.Equal(t, yellow, colorForLatency(time.Millisecond*600), "600ms should be yellow")334 assert.Equal(t, magenta, colorForLatency(time.Millisecond*1500), "1.5s should be magenta")335 assert.Equal(t, red, colorForLatency(time.Second*3), "other things should be red")336}337338func TestResetColor(t *testing.T) {339 p := LogFormatterParams{}
149 unmodified lines
modified:utils_test.go+12 added
11 unmodified lines
12 "net/http"13 "testing"1415 "github.com/stretchr/testify/assert"16 "github.com/stretchr/testify/require"17)1819func init() {20 SetMode(TestMode)
124 unmodified lines
145 e := h.MarshalXML(enc, x)146 assert.Error(t, e)147}148149func TestMarshalXMLforHSuccess(t *testing.T) {150 h := H{151 "key1": "value1",152 "key2": 123,153 }154 data, err := xml.Marshal(h)155 require.NoError(t, err)156 assert.Contains(t, string(data), "<key1>value1</key1>")157 assert.Contains(t, string(data), "<key2>123</key2>")158}159160func TestIsASCII(t *testing.T) {161 assert.True(t, isASCII("test"))162 assert.False(t, isASCII("🧡💛💚💙💜"))163}
10 unmodified lines