From 295bca372b31f653594db008563daa214d533a1c Mon Sep 17 00:00:00 2001 From: mehrdadbn9 Date: Thu, 12 Feb 2026 22:51:02 +0330 Subject: [PATCH] test: improve coverage and fix codecov configuration --- binding/binding_test.go | 20 ++++++++++++++++++ codecov.yml | 3 ++- context_test.go | 46 +++++++++++++++++++++++++++++++++++++++++ gin_test.go | 36 ++++++++++++++++++++++++++++++++ logger_test.go | 1 + utils_test.go | 10 +++++++++ 6 files changed, 115 insertions(+), 1 deletion(-) diff --git a/binding/binding_test.go b/binding/binding_test.go index a9f8b9e3..e33bdbf6 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -1403,6 +1403,26 @@ func TestPlainBinding(t *testing.T) { require.NoError(t, p.Bind(req, ptr)) } +func TestPlainBindingBindBody(t *testing.T) { + p := Plain + + var s string + require.NoError(t, p.BindBody([]byte("test string"), &s)) + assert.Equal(t, "test string", s) + + var bs []byte + require.NoError(t, p.BindBody([]byte("test []byte"), &bs)) + assert.Equal(t, []byte("test []byte"), bs) + + var i int + require.Error(t, p.BindBody([]byte("test fail"), &i)) + + require.NoError(t, p.BindBody([]byte(""), nil)) + + var ptr *string + require.NoError(t, p.BindBody([]byte(""), ptr)) +} + func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) { assert.Equal(t, name, b.Name()) diff --git a/codecov.yml b/codecov.yml index 47782e50..26e33777 100644 --- a/codecov.yml +++ b/codecov.yml @@ -5,7 +5,8 @@ coverage: project: default: target: 99% - threshold: 99% + threshold: 1% + base: auto patch: default: diff --git a/context_test.go b/context_test.go index 775fb315..6f6f233d 100644 --- a/context_test.go +++ b/context_test.go @@ -40,6 +40,12 @@ var _ context.Context = (*Context)(nil) var errTestRender = errors.New("TestRender") +type errReader int + +func (errReader) Read(p []byte) (n int, err error) { + return 0, errors.New("test error") +} + // Unit tests TODO // func (c *Context) File(filepath string) { // func (c *Context) Negotiate(code int, config Negotiate) { @@ -3782,3 +3788,43 @@ func BenchmarkGetMapFromFormData(b *testing.B) { }) } } + +func TestInitFormCacheParseMultipartFormError(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Request, _ = http.NewRequest(http.MethodPost, "/", strings.NewReader("test")) + c.Request.Header.Set("Content-Type", "multipart/form-data; boundary=invalid") + c.engine.MaxMultipartMemory = -1 + c.initFormCache() + assert.NotNil(t, c.formCache) +} + +func TestFormFileParseMultipartFormError(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Request, _ = http.NewRequest(http.MethodPost, "/", strings.NewReader("test")) + c.Request.Header.Set("Content-Type", "multipart/form-data; boundary=invalid") + c.engine.MaxMultipartMemory = -1 + _, err := c.FormFile("file") + require.Error(t, err) +} + +func TestShouldBindBodyWithTypeAssertionFailure(t *testing.T) { + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + c.Request, _ = http.NewRequest(http.MethodPost, "http://example.com", strings.NewReader(`{"foo":"FOO"}`)) + c.Set(BodyBytesKey, "not a byte slice") + var obj struct { + Foo string `json:"foo"` + } + require.NoError(t, c.ShouldBindBodyWith(&obj, binding.JSON)) + assert.Equal(t, "FOO", obj.Foo) +} + +func TestShouldBindBodyWithReadError(t *testing.T) { + w := httptest.NewRecorder() + c, _ := CreateTestContext(w) + c.Request, _ = http.NewRequest(http.MethodPost, "http://example.com", errReader(0)) + var obj struct { + Foo string `json:"foo"` + } + require.Error(t, c.ShouldBindBodyWith(&obj, binding.JSON)) +} diff --git a/gin_test.go b/gin_test.go index 43c9494d..22d920bc 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1084,3 +1084,39 @@ func TestUpdateRouteTreesCalledOnce(t *testing.T) { assert.Equal(t, "ok", w.Body.String()) } } + +func TestServeErrorWritten(t *testing.T) { + SetMode(TestMode) + router := New() + router.Use(func(c *Context) { + c.Writer.WriteHeader(http.StatusNotFound) + c.Writer.Write([]byte("custom error")) + c.Next() + }) + router.NoRoute(func(c *Context) { + c.Next() + }) + + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodGet, "/notfound", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusNotFound, w.Code) + assert.Equal(t, "custom error", w.Body.String()) +} + +func TestServeErrorStatusMismatch(t *testing.T) { + SetMode(TestMode) + router := New() + router.Use(func(c *Context) { + c.Writer.WriteHeader(http.StatusInternalServerError) + c.Next() + }) + router.NoRoute(func(c *Context) { + c.Next() + }) + + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodGet, "/notfound", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusInternalServerError, w.Code) +} diff --git a/logger_test.go b/logger_test.go index 53d0df95..2d22c6e1 100644 --- a/logger_test.go +++ b/logger_test.go @@ -329,6 +329,7 @@ func TestColorForLatency(t *testing.T) { assert.Equal(t, white, colorForLantency(time.Millisecond*20), "20ms should be white") assert.Equal(t, green, colorForLantency(time.Millisecond*150), "150ms should be green") assert.Equal(t, cyan, colorForLantency(time.Millisecond*250), "250ms should be cyan") + assert.Equal(t, blue, colorForLantency(time.Millisecond*400), "400ms should be blue") assert.Equal(t, yellow, colorForLantency(time.Millisecond*600), "600ms should be yellow") assert.Equal(t, magenta, colorForLantency(time.Millisecond*1500), "1.5s should be magenta") assert.Equal(t, red, colorForLantency(time.Second*3), "other things should be red") diff --git a/utils_test.go b/utils_test.go index 893ebc88..168b1850 100644 --- a/utils_test.go +++ b/utils_test.go @@ -13,6 +13,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func init() { @@ -145,6 +146,15 @@ func TestMarshalXMLforH(t *testing.T) { assert.Error(t, e) } +func TestMarshalXMLforHSuccess(t *testing.T) { + h := H{ + "key": "value", + } + data, err := xml.Marshal(h) + require.NoError(t, err) + assert.Contains(t, string(data), "value") +} + func TestIsASCII(t *testing.T) { assert.True(t, isASCII("test")) assert.False(t, isASCII("๐Ÿงก๐Ÿ’›๐Ÿ’š๐Ÿ’™๐Ÿ’œ"))