mirror of
https://github.com/gin-gonic/gin.git
synced 2026-06-07 04:38:19 +08:00
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%
This commit is contained in:
parent
e7ec17985d
commit
2c42206345
@ -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))
|
||||
}
|
||||
|
||||
@ -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)
|
||||
}
|
||||
|
||||
@ -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"))
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user