From 00cfe5aac2604e0632ee674b69a1e652a2461923 Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 15 Aug 2026 13:42:11 +0800 Subject: [PATCH 1/7] chore(deps): bump the actions group across 1 directory with 4 updates (#4787) Bumps the actions group with 4 updates in the / directory: [actions/checkout](https://github.com/actions/checkout), [actions/setup-go](https://github.com/actions/setup-go), [actions/cache](https://github.com/actions/cache) and [codecov/codecov-action](https://github.com/codecov/codecov-action). Updates `actions/checkout` from 6 to 7 - [Release notes](https://github.com/actions/checkout/releases) - [Changelog](https://github.com/actions/checkout/blob/main/CHANGELOG.md) - [Commits](https://github.com/actions/checkout/compare/v6...v7) Updates `actions/setup-go` from 6 to 7 - [Release notes](https://github.com/actions/setup-go/releases) - [Commits](https://github.com/actions/setup-go/compare/v6...v7) Updates `actions/cache` from 5 to 6 - [Release notes](https://github.com/actions/cache/releases) - [Changelog](https://github.com/actions/cache/blob/main/RELEASES.md) - [Commits](https://github.com/actions/cache/compare/v5...v6) Updates `codecov/codecov-action` from 6 to 7 - [Release notes](https://github.com/codecov/codecov-action/releases) - [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md) - [Commits](https://github.com/codecov/codecov-action/compare/v6...v7) --- updated-dependencies: - dependency-name: actions/checkout dependency-version: '7' dependency-type: direct:production update-type: version-update:semver-major dependency-group: actions - dependency-name: actions/setup-go dependency-version: '7' dependency-type: direct:production update-type: version-update:semver-major dependency-group: actions - dependency-name: actions/cache dependency-version: '6' dependency-type: direct:production update-type: version-update:semver-major dependency-group: actions - dependency-name: codecov/codecov-action dependency-version: '7' dependency-type: direct:production update-type: version-update:semver-major dependency-group: actions ... Signed-off-by: dependabot[bot] Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com> --- .github/workflows/codeql.yml | 2 +- .github/workflows/gin.yml | 12 ++++++------ .github/workflows/goreleaser.yml | 4 ++-- .github/workflows/trivy-scan.yml | 2 +- 4 files changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/codeql.yml b/.github/workflows/codeql.yml index f287c265..0ede2a05 100644 --- a/.github/workflows/codeql.yml +++ b/.github/workflows/codeql.yml @@ -33,7 +33,7 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v6 + uses: actions/checkout@v7 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL diff --git a/.github/workflows/gin.yml b/.github/workflows/gin.yml index 8857bebc..072312dd 100644 --- a/.github/workflows/gin.yml +++ b/.github/workflows/gin.yml @@ -16,11 +16,11 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v6 + uses: actions/checkout@v7 with: fetch-depth: 0 - name: Set up Go - uses: actions/setup-go@v6 + uses: actions/setup-go@v7 with: go-version: "^1" - name: Setup golangci-lint @@ -55,17 +55,17 @@ jobs: GOPROXY: https://proxy.golang.org steps: - name: Set up Go ${{ matrix.go }} - uses: actions/setup-go@v6 + uses: actions/setup-go@v7 with: go-version: ${{ matrix.go }} cache: false - name: Checkout Code - uses: actions/checkout@v6 + uses: actions/checkout@v7 with: ref: ${{ github.ref }} - - uses: actions/cache@v5 + - uses: actions/cache@v6 with: path: | ${{ matrix.go-build }} @@ -78,6 +78,6 @@ jobs: run: make test - name: Upload coverage to Codecov - uses: codecov/codecov-action@v6 + uses: codecov/codecov-action@v7 with: flags: ${{ matrix.os }},go-${{ matrix.go }},${{ matrix.test-tags }} diff --git a/.github/workflows/goreleaser.yml b/.github/workflows/goreleaser.yml index ea933e7e..6dd36ff1 100644 --- a/.github/workflows/goreleaser.yml +++ b/.github/workflows/goreleaser.yml @@ -13,11 +13,11 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout - uses: actions/checkout@v6 + uses: actions/checkout@v7 with: fetch-depth: 0 - name: Set up Go - uses: actions/setup-go@v6 + uses: actions/setup-go@v7 with: go-version: "^1" - name: Run GoReleaser diff --git a/.github/workflows/trivy-scan.yml b/.github/workflows/trivy-scan.yml index 9060e45c..751d0106 100644 --- a/.github/workflows/trivy-scan.yml +++ b/.github/workflows/trivy-scan.yml @@ -22,7 +22,7 @@ jobs: runs-on: ubuntu-latest steps: - name: Checkout code - uses: actions/checkout@v6 + uses: actions/checkout@v7 with: fetch-depth: 0 From 8dd20118ba9a9dfa18ebba99e33d6a59b0090436 Mon Sep 17 00:00:00 2001 From: Gaurav Patil <159000263+GP-09@users.noreply.github.com> Date: Sat, 15 Aug 2026 11:13:07 +0530 Subject: [PATCH 2/7] fix(deps): bump golang.org/x/net and golang.org/x/text to patched versions (#4807) The scheduled Trivy scan has been failing on master with two HIGH findings, and it fails every pull request along with it: - CVE-2026-56852, golang.org/x/text, denial of service, fixed in 0.39.0 - CVE-2026-46600, golang.org/x/net/dns/dnsmessage, denial of service, fixed in 0.56.0 Bump both to the first patched release. golang.org/x/crypto and golang.org/x/sys move with them as transitive requirements of x/net. Verified with the same settings the workflow uses: trivy fs --scanners vuln --severity CRITICAL,HIGH,MEDIUM \ --ignore-unfixed --exit-code 1 . which now exits 0. go build ./... and go test ./... pass. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Bo-Yi Wu --- go.mod | 8 ++++---- go.sum | 16 ++++++++-------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/go.mod b/go.mod index 4421641e..3f80e1c0 100644 --- a/go.mod +++ b/go.mod @@ -16,7 +16,7 @@ require ( github.com/stretchr/testify v1.11.1 github.com/ugorji/go/codec v1.3.1 go.mongodb.org/mongo-driver/v2 v2.5.0 - golang.org/x/net v0.55.0 + golang.org/x/net v0.56.0 google.golang.org/protobuf v1.36.11 ) @@ -39,7 +39,7 @@ require ( github.com/twitchyliquid64/golang-asm v0.15.1 // indirect go.uber.org/mock v0.6.0 // indirect golang.org/x/arch v0.25.0 // indirect - golang.org/x/crypto v0.52.0 // indirect - golang.org/x/sys v0.45.0 // indirect - golang.org/x/text v0.37.0 // indirect + golang.org/x/crypto v0.53.0 // indirect + golang.org/x/sys v0.46.0 // indirect + golang.org/x/text v0.39.0 // indirect ) diff --git a/go.sum b/go.sum index a83985ba..f04ab19c 100644 --- a/go.sum +++ b/go.sum @@ -79,15 +79,15 @@ go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= golang.org/x/arch v0.25.0 h1:qnk6Ksugpi5Bz32947rkUgDt9/s5qvqDPl/gBKdMJLE= golang.org/x/arch v0.25.0/go.mod h1:0X+GdSIP+kL5wPmpK7sdkEVTt2XoYP0cSjQSbZBwOi8= -golang.org/x/crypto v0.52.0 h1:RMs7fP2rXdep0CftQlK8Uf+kibLm7qkCcradZWYz988= -golang.org/x/crypto v0.52.0/go.mod h1:1QgfPxDqh0T2M/elOJtp9RvuR95kVjir0e6/BvEmGbc= -golang.org/x/net v0.55.0 h1:bcvxaJn3e1U6InsFWt1JUq1aSjnRxLzT2rtD2KfkDF8= -golang.org/x/net v0.55.0/go.mod h1:L5U2KuzuOe1lY7Z+aWVIKK6qEeJXnXV9yzGA+WCHJww= +golang.org/x/crypto v0.53.0 h1:QZ4Muo8THX6CizN2vPPd5fBGHyogrdK9fG4wLPFUsto= +golang.org/x/crypto v0.53.0/go.mod h1:DNLU434OwVakk9PzuwV8w62mAJpRJL3vsgcfp4Qnsio= +golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o= +golang.org/x/net v0.56.0/go.mod h1:D3Ku6r+V6JROoZK144D2XfMHFcMq/0zSfLelVTCFKec= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.45.0 h1:dO4czNzziLiiXplLQgBCEpCvXQ3dnkn0SdaZSYdQ+FY= -golang.org/x/sys v0.45.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= -golang.org/x/text v0.37.0 h1:Cqjiwd9eSg8e0QAkyCaQTNHFIIzWtidPahFWR83rTrc= -golang.org/x/text v0.37.0/go.mod h1:a5sjxXGs9hsn/AJVwuElvCAo9v8QYLzvavO5z2PiM38= +golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= +golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/text v0.39.0 h1:UbZz4pLOvn600D6Oh6GGEI6VAmndrEBLv8/6BEXzyus= +golang.org/x/text v0.39.0/go.mod h1:3UwRclnC2g0TU9x8PZiyfOajCd1zaUNHF9cvqcQZ+ZM= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From dcaa4296d111981ffb31ac3eba90bb63e1eb5ab9 Mon Sep 17 00:00:00 2001 From: Amirhf Date: Sat, 15 Aug 2026 09:14:19 +0330 Subject: [PATCH 3/7] docs(path): fix malformed comment in cleanPath (#4723) --- path.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/path.go b/path.go index 3b67caa9..672213d1 100644 --- a/path.go +++ b/path.go @@ -55,7 +55,7 @@ func cleanPath(p string) string { // A bit more clunky without a 'lazybuf' like the path package, but the loop // gets completely inlined (bufApp calls). - // loop has no expensive function calls (except 1x make) // So in contrast to the path package this loop has no expensive function + // So in contrast to the path package this loop has no expensive function // calls (except make, if needed). for r < n { From 2a1a14ff37a26ca6a76763ecc33727c23d9284e9 Mon Sep 17 00:00:00 2001 From: Jia-ben00 <2403877005@qq.com> Date: Wed, 16 Sep 2026 21:24:33 +0800 Subject: [PATCH 4/7] feat(router): add QUERY method shortcut for RFC 10008 (#4830) * feat(router): add QUERY method shortcut for RFC 10008 Add a QUERY() router method as a shortcut for Handle("QUERY", path, handlers), following the same pattern as GET, POST, HEAD and other HTTP method shortcuts. The QUERY method is defined by RFC 10008 and allows sending a query in the request body, similar to GET but with a body. Go's net/http does not yet expose an http.MethodQuery constant, so the literal string "QUERY" is used. Closes #4721 * style(router): format QUERY method test - Remove an extra blank line to satisfy gofmt. --------- Co-authored-by: Jia-ben00 <316397950+Jia-ben00@users.noreply.github.com> Co-authored-by: Bo-Yi Wu --- gin_test.go | 19 +++++++++++++++++++ routergroup.go | 7 +++++++ 2 files changed, 26 insertions(+) diff --git a/gin_test.go b/gin_test.go index a9cf1755..b488c446 100644 --- a/gin_test.go +++ b/gin_test.go @@ -1156,3 +1156,22 @@ func TestUpdateRouteTreesCalledOnce(t *testing.T) { assert.Equal(t, "ok", w.Body.String()) } } + +func TestRouterQueryMethod(t *testing.T) { + router := New() + router.QUERY("/test", func(c *Context) { + c.String(http.StatusOK, "query ok") + }) + + w := httptest.NewRecorder() + req, _ := http.NewRequest("QUERY", "/test", nil) + router.ServeHTTP(w, req) + assert.Equal(t, http.StatusOK, w.Code) + assert.Equal(t, "query ok", w.Body.String()) + + // Other methods should not match + w2 := httptest.NewRecorder() + req2, _ := http.NewRequest(http.MethodGet, "/test", nil) + router.ServeHTTP(w2, req2) + assert.Equal(t, http.StatusNotFound, w2.Code) +} diff --git a/routergroup.go b/routergroup.go index c01b917e..1ae0b7b4 100644 --- a/routergroup.go +++ b/routergroup.go @@ -142,6 +142,13 @@ func (group *RouterGroup) HEAD(relativePath string, handlers ...HandlerFunc) IRo return group.handle(http.MethodHead, relativePath, handlers) } +// QUERY is a shortcut for router.Handle("QUERY", path, handlers). +// The QUERY method is defined by RFC 10008 and is used to send a query +// in the request body, similar to GET but with a body. +func (group *RouterGroup) QUERY(relativePath string, handlers ...HandlerFunc) IRoutes { + return group.handle("QUERY", relativePath, handlers) +} + // Any registers a route that matches all the HTTP methods. // GET, POST, PUT, PATCH, HEAD, OPTIONS, DELETE, CONNECT, TRACE. func (group *RouterGroup) Any(relativePath string, handlers ...HandlerFunc) IRoutes { From b52df1fe77734c691acc1fce21647fe0ca955201 Mon Sep 17 00:00:00 2001 From: Ruben Pari <73421813+RubenPari@users.noreply.github.com> Date: Wed, 16 Sep 2026 15:26:35 +0200 Subject: [PATCH 5/7] docs: document panic conditions in Handle, StaticFS, and Bind (#4797) Handle panics on invalid HTTP method, StaticFS panics when relativePath contains URL parameters, and Bind panics when given a pointer. These panic conditions are reasonable startup-time guards but were not documented, making the crash risk undiscoverable without reading source. Fixes #4594 --- routergroup.go | 3 +++ utils.go | 1 + 2 files changed, 4 insertions(+) diff --git a/routergroup.go b/routergroup.go index 1ae0b7b4..84d3e00b 100644 --- a/routergroup.go +++ b/routergroup.go @@ -100,6 +100,8 @@ func (group *RouterGroup) handle(httpMethod, relativePath string, handlers Handl // This function is intended for bulk loading and to allow the usage of less // frequently used, non-standardized or custom methods (e.g. for internal // communication with a proxy). +// +// It panics if httpMethod is not a valid HTTP method (uppercase ASCII letters only). func (group *RouterGroup) Handle(httpMethod, relativePath string, handlers ...HandlerFunc) IRoutes { if matched := regEnLetter.MatchString(httpMethod); !matched { panic("http method " + httpMethod + " is not valid") @@ -207,6 +209,7 @@ func (group *RouterGroup) Static(relativePath, root string) IRoutes { // StaticFS works just like `Static()` but a custom `http.FileSystem` can be used instead. // Gin by default uses: gin.Dir() +// It panics if relativePath contains URL parameters (: or *). func (group *RouterGroup) StaticFS(relativePath string, fs http.FileSystem) IRoutes { if strings.Contains(relativePath, ":") || strings.Contains(relativePath, "*") { panic("URL parameters can not be used when serving a static folder") diff --git a/utils.go b/utils.go index 2fecce46..ac221def 100644 --- a/utils.go +++ b/utils.go @@ -26,6 +26,7 @@ const localhostIP = "127.0.0.1" const localhostIPv6 = "::1" // Bind is a helper function for given interface object and returns a Gin middleware. +// It panics if val is a pointer; pass the struct value directly (e.g., gin.Bind(Struct{}) not gin.Bind(&Struct{})). func Bind(val any) HandlerFunc { value := reflect.ValueOf(val) if value.Kind() == reflect.Ptr { From bc141b4dbf90d5bb5028fc68afe48782402bb2d4 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 16 Sep 2026 22:47:17 +0800 Subject: [PATCH 6/7] fix(deps)!: patch x/crypto vulnerabilities (#4832) - Upgrade x/crypto and its required dependency versions - Align documentation and CI with the required Go version BREAKING CHANGE: Go 1.26 or later is now required by x/crypto v0.56.0. Upgrade the Go toolchain before building Gin. --- .github/workflows/gin.yml | 2 +- README.md | 2 +- go.mod | 10 +++++----- go.sum | 16 ++++++++-------- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/.github/workflows/gin.yml b/.github/workflows/gin.yml index 072312dd..eb463fee 100644 --- a/.github/workflows/gin.yml +++ b/.github/workflows/gin.yml @@ -33,7 +33,7 @@ jobs: strategy: matrix: os: [ubuntu-latest, macos-latest] - go: ["1.25", "1.26"] + go: ["1.26", "1.27"] test-tags: [ "", diff --git a/README.md b/README.md index 96b0ae3c..10e627fc 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ Gin combines the simplicity of Express.js-style routing with Go's performance ch ### Prerequisites -- **Go version**: Gin requires [Go](https://go.dev/) version [1.25](https://go.dev/doc/devel/release#go1.25.0) or above +- **Go version**: Gin requires [Go](https://go.dev/) version [1.26](https://go.dev/doc/devel/release#go1.26.0) or above - **Basic Go knowledge**: Familiarity with Go syntax and package management is helpful ### Installation diff --git a/go.mod b/go.mod index 3f80e1c0..5ede85b2 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/gin-gonic/gin -go 1.25.0 +go 1.26.0 require ( github.com/bytedance/sonic v1.15.0 @@ -16,7 +16,7 @@ require ( github.com/stretchr/testify v1.11.1 github.com/ugorji/go/codec v1.3.1 go.mongodb.org/mongo-driver/v2 v2.5.0 - golang.org/x/net v0.56.0 + golang.org/x/net v0.57.0 google.golang.org/protobuf v1.36.11 ) @@ -39,7 +39,7 @@ require ( github.com/twitchyliquid64/golang-asm v0.15.1 // indirect go.uber.org/mock v0.6.0 // indirect golang.org/x/arch v0.25.0 // indirect - golang.org/x/crypto v0.53.0 // indirect - golang.org/x/sys v0.46.0 // indirect - golang.org/x/text v0.39.0 // indirect + golang.org/x/crypto v0.56.0 // indirect + golang.org/x/sys v0.47.0 // indirect + golang.org/x/text v0.41.0 // indirect ) diff --git a/go.sum b/go.sum index f04ab19c..a714fd58 100644 --- a/go.sum +++ b/go.sum @@ -79,15 +79,15 @@ go.uber.org/mock v0.6.0 h1:hyF9dfmbgIX5EfOdasqLsWD6xqpNZlXblLB/Dbnwv3Y= go.uber.org/mock v0.6.0/go.mod h1:KiVJ4BqZJaMj4svdfmHM0AUx4NJYO8ZNpPnZn1Z+BBU= golang.org/x/arch v0.25.0 h1:qnk6Ksugpi5Bz32947rkUgDt9/s5qvqDPl/gBKdMJLE= golang.org/x/arch v0.25.0/go.mod h1:0X+GdSIP+kL5wPmpK7sdkEVTt2XoYP0cSjQSbZBwOi8= -golang.org/x/crypto v0.53.0 h1:QZ4Muo8THX6CizN2vPPd5fBGHyogrdK9fG4wLPFUsto= -golang.org/x/crypto v0.53.0/go.mod h1:DNLU434OwVakk9PzuwV8w62mAJpRJL3vsgcfp4Qnsio= -golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o= -golang.org/x/net v0.56.0/go.mod h1:D3Ku6r+V6JROoZK144D2XfMHFcMq/0zSfLelVTCFKec= +golang.org/x/crypto v0.56.0 h1:GUh5Ii4J5jtcseSMiRqr1jXCNHoxjeV9Fmekc2oLy6Y= +golang.org/x/crypto v0.56.0/go.mod h1:OMW5y6CY9l38uPLmxU6l6pwcXp1obtLo3e6gT7gQR2I= +golang.org/x/net v0.57.0 h1:K5+3DljvIuDG9/Jv9rvyMywYNFCQ9RSUY6OOTTkT+tE= +golang.org/x/net v0.57.0/go.mod h1:KpXc8iv+r3XplLAG/f7Jsf9RPszJzdR0f58q9vGOuEU= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw= -golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= -golang.org/x/text v0.39.0 h1:UbZz4pLOvn600D6Oh6GGEI6VAmndrEBLv8/6BEXzyus= -golang.org/x/text v0.39.0/go.mod h1:3UwRclnC2g0TU9x8PZiyfOajCd1zaUNHF9cvqcQZ+ZM= +golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs= +golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw= +golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8= +golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M= google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE= google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= From 5c6a15f8f9566612076bd209e623861bf92a6283 Mon Sep 17 00:00:00 2001 From: Langning Zhang Date: Wed, 16 Sep 2026 23:01:52 +0800 Subject: [PATCH 7/7] fix(context): handle missing HTML renderer (#4805) * fix(context): handle missing HTML renderer * test(render): cover missing HTML renderer error --------- Co-authored-by: Bo-Yi Wu --- context.go | 5 +++ context_test.go | 76 +++++++++++++++++++++++++++++++++++++++++++ render/html.go | 6 ++++ render/render_test.go | 10 ++++++ 4 files changed, 97 insertions(+) diff --git a/context.go b/context.go index 1dc730e3..788b00bb 100644 --- a/context.go +++ b/context.go @@ -1219,6 +1219,11 @@ func (c *Context) Render(code int, r render.Render) { // It also updates the HTTP code and sets the Content-Type as "text/html". // See http://golang.org/doc/articles/wiki/ func (c *Context) HTML(code int, name string, obj any) { + if c.engine.HTMLRender == nil { + c.Render(code, render.HTML{}) + return + } + instance := c.engine.HTMLRender.Instance(name, obj) c.Render(code, instance) } diff --git a/context_test.go b/context_test.go index e8d305e4..cef488c9 100644 --- a/context_test.go +++ b/context_test.go @@ -1345,6 +1345,82 @@ func TestContextRenderHTML(t *testing.T) { assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type")) } +func TestContextRenderHTMLWithoutRenderer(t *testing.T) { + tests := []struct { + name string + method string + status int + wantErrors []string + wantAborted bool + wantNextCalled bool + }{ + { + name: "body allowed", + method: http.MethodGet, + status: http.StatusAccepted, + wantErrors: []string{"html renderer is not configured"}, + wantAborted: true, + }, + { + name: "no content", + method: http.MethodGet, + status: http.StatusNoContent, + wantNextCalled: true, + }, + { + name: "not modified", + method: http.MethodGet, + status: http.StatusNotModified, + wantNextCalled: true, + }, + { + name: "head", + method: http.MethodHead, + status: http.StatusOK, + wantErrors: []string{"html renderer is not configured"}, + wantAborted: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + w := httptest.NewRecorder() + router := New() + var contextErrors []string + var contextAborted bool + nextHandlerCalled := false + + handlers := []HandlerFunc{ + func(c *Context) { + c.HTML(tt.status, "index.tmpl", H{"title": "Main website"}) + contextErrors = c.Errors.Errors() + contextAborted = c.IsAborted() + }, + func(_ *Context) { + nextHandlerCalled = true + }, + } + if tt.method == http.MethodHead { + router.HEAD("/", handlers...) + } else { + router.GET("/", handlers...) + } + + req := httptest.NewRequest(tt.method, "/", nil) + require.NotPanics(t, func() { + router.ServeHTTP(w, req) + }) + + assert.Equal(t, tt.status, w.Code) + assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type")) + assert.Equal(t, tt.wantErrors, contextErrors) + assert.Equal(t, tt.wantAborted, contextAborted) + assert.Equal(t, tt.wantNextCalled, nextHandlerCalled) + assert.Empty(t, w.Body.String()) + }) + } +} + func TestContextRenderHTML2(t *testing.T) { w := httptest.NewRecorder() c, router := CreateTestContext(w) diff --git a/render/html.go b/render/html.go index 965d84c6..36c8f079 100644 --- a/render/html.go +++ b/render/html.go @@ -5,6 +5,7 @@ package render import ( + "errors" "html/template" "net/http" @@ -50,6 +51,8 @@ type HTML struct { var htmlContentType = []string{"text/html; charset=utf-8"} +var errHTMLRendererNotConfigured = errors.New("html renderer is not configured") + // Instance (HTMLProduction) returns an HTML instance which it realizes Render interface. func (r HTMLProduction) Instance(name string, data any) Render { return HTML{ @@ -88,6 +91,9 @@ func (r HTMLDebug) loadTemplate() *template.Template { // Render (HTML) executes template and writes its result with custom ContentType for response. func (r HTML) Render(w http.ResponseWriter) error { r.WriteContentType(w) + if r.Template == nil { + return errHTMLRendererNotConfigured + } if r.Name == "" { return r.Template.Execute(w, r.Data) diff --git a/render/render_test.go b/render/render_test.go index f63878b9..24815452 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -630,6 +630,16 @@ func TestRenderHTMLTemplate(t *testing.T) { assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type")) } +func TestRenderHTMLWithoutTemplate(t *testing.T) { + w := httptest.NewRecorder() + + err := (HTML{}).Render(w) + + require.EqualError(t, err, "html renderer is not configured") + assert.Empty(t, w.Body.String()) + assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type")) +} + func TestRenderHTMLTemplateEmptyName(t *testing.T) { w := httptest.NewRecorder() templ := template.Must(template.New("").Parse(`Hello {{.name}}`))