From 6d97ab9c023c6f67b1621b871a092e43eceeef3d Mon Sep 17 00:00:00 2001 From: heliang <454636197@qq.com> Date: Sat, 16 May 2026 15:03:27 +0800 Subject: [PATCH] Add coverage tests for routing PR --- binding/binding_test.go | 20 +++++++++++ codec/json/json_test.go | 79 +++++++++++++++++++++++++++++++++++++++++ ginS/gins_test.go | 48 +++++++++++++++++++++++++ tree_test.go | 34 ++++++++++++++++++ 4 files changed, 181 insertions(+) create mode 100644 codec/json/json_test.go diff --git a/binding/binding_test.go b/binding/binding_test.go index f90488cd..a4e8151f 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(nil, nil)) + + var ptr *string + require.NoError(t, p.BindBody(nil, ptr)) +} + func testProtoBodyBindingFail(t *testing.T, b Binding, name, path, badPath, body, badBody string) { assert.Equal(t, name, b.Name()) diff --git a/codec/json/json_test.go b/codec/json/json_test.go new file mode 100644 index 00000000..09b6b9e6 --- /dev/null +++ b/codec/json/json_test.go @@ -0,0 +1,79 @@ +// Copyright 2026 Gin Core Team. All rights reserved. +// Use of this source code is governed by a MIT style +// license that can be found in the LICENSE file. + +package json + +import ( + "bytes" + "encoding/json" + "strings" + "testing" +) + +func TestAPI(t *testing.T) { + if API == nil { + t.Fatal("API is nil") + } + if Package == "" { + t.Fatal("Package is empty") + } +} + +func TestAPIMarshalAndUnmarshal(t *testing.T) { + type payload struct { + Name string `json:"name"` + } + + data, err := API.Marshal(payload{Name: "gin"}) + if err != nil { + t.Fatal(err) + } + if string(data) != `{"name":"gin"}` { + t.Fatalf("unexpected marshal output: %s", data) + } + + var decoded payload + if err := API.Unmarshal(data, &decoded); err != nil { + t.Fatal(err) + } + if decoded.Name != "gin" { + t.Fatalf("unexpected decoded payload: %#v", decoded) + } +} + +func TestAPIMarshalIndent(t *testing.T) { + data, err := API.MarshalIndent(map[string]string{"name": "gin"}, "", " ") + if err != nil { + t.Fatal(err) + } + if !bytes.Contains(data, []byte("\n ")) { + t.Fatalf("expected indented JSON, got %q", data) + } +} + +func TestAPIEncoder(t *testing.T) { + var buf bytes.Buffer + encoder := API.NewEncoder(&buf) + encoder.SetEscapeHTML(false) + + if err := encoder.Encode(""); err != nil { + t.Fatal(err) + } + if got := buf.String(); got != "\"\"\n" { + t.Fatalf("unexpected encoded JSON: %q", got) + } +} + +func TestAPIDecoder(t *testing.T) { + decoder := API.NewDecoder(strings.NewReader(`{"known": 1, "extra": 2}`)) + decoder.UseNumber() + decoder.DisallowUnknownFields() + + var dst struct { + Known json.Number `json:"known"` + } + if err := decoder.Decode(&dst); err == nil { + t.Fatal("expected unknown field error") + } +} diff --git a/ginS/gins_test.go b/ginS/gins_test.go index ffde85d2..ef0671ad 100644 --- a/ginS/gins_test.go +++ b/ginS/gins_test.go @@ -8,10 +8,13 @@ import ( "html/template" "net/http" "net/http/httptest" + "os" + "path/filepath" "testing" "github.com/gin-gonic/gin" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" ) func init() { @@ -214,6 +217,24 @@ func TestSetHTMLTemplate(t *testing.T) { assert.NotNil(t, engine()) } +func TestLoadHTMLGlob(t *testing.T) { + LoadHTMLGlob("../testdata/template/*") + + assert.NotNil(t, engine()) +} + +func TestLoadHTMLFiles(t *testing.T) { + LoadHTMLFiles("../testdata/template/hello.tmpl", "../testdata/template/raw.tmpl") + + assert.NotNil(t, engine()) +} + +func TestLoadHTMLFS(t *testing.T) { + LoadHTMLFS(http.Dir("../testdata"), "template/hello.tmpl", "template/raw.tmpl") + + assert.NotNil(t, engine()) +} + func TestStaticFile(t *testing.T) { StaticFile("/static-file", "../testdata/test_file.txt") @@ -224,6 +245,33 @@ func TestStaticFile(t *testing.T) { assert.Equal(t, http.StatusOK, w.Code) } +func TestRunReturnsListenError(t *testing.T) { + err := Run("127.0.0.1:bad-port") + + require.Error(t, err) +} + +func TestRunTLSReturnsListenError(t *testing.T) { + err := RunTLS("127.0.0.1:bad-port", "../testdata/certificate/cert.pem", "../testdata/certificate/key.pem") + + require.Error(t, err) +} + +func TestRunUnixReturnsListenError(t *testing.T) { + err := RunUnix(filepath.Join(t.TempDir(), "missing", "gin.sock")) + + require.Error(t, err) +} + +func TestRunFdReturnsListenerError(t *testing.T) { + file, err := os.CreateTemp(t.TempDir(), "gin-fd") + require.NoError(t, err) + + err = RunFd(int(file.Fd())) + + require.Error(t, err) +} + func TestStatic(t *testing.T) { Static("/static-dir", "../testdata") diff --git a/tree_test.go b/tree_test.go index 23339af4..f06c3791 100644 --- a/tree_test.go +++ b/tree_test.go @@ -939,6 +939,40 @@ func TestTreeExpandParamsCapacity(t *testing.T) { } } +func TestTreeFindCaseInsensitivePathWithWildcardChildAndBufferedRune(t *testing.T) { + tree := &node{ + path: "/", + indices: "x", + wildChild: true, + children: []*node{ + {path: "x", handlers: fakeHandler("/x"), fullPath: "/x"}, + {path: ":id", nType: param, handlers: fakeHandler("/:id"), fullPath: "/:id"}, + }, + } + + out := tree.findCaseInsensitivePathRec("/x", nil, [4]byte{0, 'x'}, false) + if string(out) != "/x" { + t.Fatalf("Wrong result: got %s, want /x", string(out)) + } +} + +func TestTreeFindCaseInsensitivePathWithWildcardChildAndUppercaseStatic(t *testing.T) { + tree := &node{ + path: "/", + indices: "A", + wildChild: true, + children: []*node{ + {path: "A", handlers: fakeHandler("/A"), fullPath: "/A"}, + {path: ":id", nType: param, handlers: fakeHandler("/:id"), fullPath: "/:id"}, + }, + } + + out := tree.findCaseInsensitivePathRec("/a", nil, [4]byte{}, false) + if string(out) != "/A" { + t.Fatalf("Wrong result: got %s, want /A", string(out)) + } +} + func TestTreeWildcardConflictEx(t *testing.T) { conflicts := [...]struct { route string