From 2c4220634589039aff032ceaaeb07c7c2930da77 Mon Sep 17 00:00:00 2001 From: shahariaz Date: Tue, 2 Jun 2026 21:15:55 +0600 Subject: [PATCH] test: increase coverage to 98.9% by covering previously untested code paths - ginS: add TestLoadHTMLGlob, TestLoadHTMLFiles, TestLoadHTMLFS covering the three HTML template loader wrappers (73.1% -> 100%) - ginS: add TestRunError, TestRunTLSError, TestRunUnixError, TestRunFdError covering all four server-start wrappers via fast-failing error paths - binding: add TestPlainBindingBody covering plainBinding.BindBody (string, []byte, unsupported type, and nil receiver branches) - render: add TestRedirectWriteContentType covering the Redirect no-op method Overall project coverage: 98.5% -> 98.9% Patch coverage: 100% --- binding/binding_test.go | 20 ++++++++++++++++++++ ginS/gins_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ render/render_test.go | 11 +++++++++++ 3 files changed, 72 insertions(+) diff --git a/binding/binding_test.go b/binding/binding_test.go index f90488cd..78302fdf 100644 --- a/binding/binding_test.go +++ b/binding/binding_test.go @@ -1432,3 +1432,23 @@ func requestWithBody(method, path, body string) (req *http.Request) { req, _ = http.NewRequest(method, path, bytes.NewBufferString(body)) return } + +func TestPlainBindingBody(t *testing.T) { + p := Plain + + var s string + require.NoError(t, p.BindBody([]byte("hello body"), &s)) + assert.Equal(t, "hello body", s) + + var bs []byte + require.NoError(t, p.BindBody([]byte("bytes body"), &bs)) + assert.Equal(t, []byte("bytes body"), bs) + + var i int + require.Error(t, p.BindBody([]byte("fail"), &i)) + + require.NoError(t, p.BindBody([]byte(""), nil)) + + var ptr *string + require.NoError(t, p.BindBody([]byte(""), ptr)) +} diff --git a/ginS/gins_test.go b/ginS/gins_test.go index ffde85d2..995c6da1 100644 --- a/ginS/gins_test.go +++ b/ginS/gins_test.go @@ -244,3 +244,44 @@ func TestStaticFS(t *testing.T) { assert.Equal(t, http.StatusOK, w.Code) } + +func TestLoadHTMLGlob(t *testing.T) { + assert.NotPanics(t, func() { + LoadHTMLGlob("../testdata/template/*.tmpl") + }) + assert.NotNil(t, engine()) +} + +func TestLoadHTMLFiles(t *testing.T) { + assert.NotPanics(t, func() { + LoadHTMLFiles("../testdata/template/hello.tmpl", "../testdata/template/raw.tmpl") + }) + assert.NotNil(t, engine()) +} + +func TestLoadHTMLFS(t *testing.T) { + assert.NotPanics(t, func() { + LoadHTMLFS(http.Dir("../testdata/template"), "*.tmpl") + }) + assert.NotNil(t, engine()) +} + +func TestRunError(t *testing.T) { + err := Run("not-valid-address") + assert.Error(t, err) +} + +func TestRunTLSError(t *testing.T) { + err := RunTLS(":0", "/nonexistent.cert", "/nonexistent.key") + assert.Error(t, err) +} + +func TestRunUnixError(t *testing.T) { + err := RunUnix("/nonexistent/deep/path/gin-test.sock") + assert.Error(t, err) +} + +func TestRunFdError(t *testing.T) { + err := RunFd(99999) + assert.Error(t, err) +} diff --git a/render/render_test.go b/render/render_test.go index f63878b9..0236abca 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -805,3 +805,14 @@ func TestRenderWriteError(t *testing.T) { require.Error(t, err) assert.Equal(t, "write error", err.Error()) } + +func TestRedirectWriteContentType(t *testing.T) { + w := httptest.NewRecorder() + req, _ := http.NewRequest(http.MethodGet, "/", nil) + r := Redirect{Code: http.StatusFound, Request: req, Location: "/new"} + // WriteContentType is a no-op for Redirect; verify it doesn't panic + assert.NotPanics(t, func() { + r.WriteContentType(w) + }) + assert.Empty(t, w.Header().Get("Content-Type")) +}