Skip to content
Myr

test(gin): Add comprehensive test coverage for ginS package (#4442)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* test(ginS): add comprehensive test coverage for ginS package Improve test coverage for ginS package by adding 18 test functions covering HTTP methods, routing, middleware, static files, and templates. * use http.Method* constants instead of raw strings in gins_test.go * copyright updated in gins_test.go ---------

authored by Milad · committed by GitHub · parent 771dcc6 · Browse files · 1 file+246 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

1 file+246 added−0 removed

  • other:+246 added−0 removed

1 file+246 added−0 removed

added:ginS/gins_test.go+246 added
1// Copyright 2025 Gin Core Team. All rights reserved.2// Use of this source code is governed by a MIT style3// license that can be found in the LICENSE file.45package ginS67import (8 "html/template"9 "net/http"10 "net/http/httptest"11 "testing"1213 "github.com/gin-gonic/gin"14 "github.com/stretchr/testify/assert"15)1617func init() {18 gin.SetMode(gin.TestMode)19}2021func TestGET(t *testing.T) {22 GET("/test", func(c *gin.Context) {23 c.String(http.StatusOK, "test")24 })2526 req := httptest.NewRequest(http.MethodGet, "/test", nil)27 w := httptest.NewRecorder()28 engine().ServeHTTP(w, req)2930 assert.Equal(t, http.StatusOK, w.Code)31 assert.Equal(t, "test", w.Body.String())32}3334func TestPOST(t *testing.T) {35 POST("/post", func(c *gin.Context) {36 c.String(http.StatusCreated, "created")37 })3839 req := httptest.NewRequest(http.MethodPost, "/post", nil)40 w := httptest.NewRecorder()41 engine().ServeHTTP(w, req)4243 assert.Equal(t, http.StatusCreated, w.Code)44 assert.Equal(t, "created", w.Body.String())45}4647func TestPUT(t *testing.T) {48 PUT("/put", func(c *gin.Context) {49 c.String(http.StatusOK, "updated")50 })5152 req := httptest.NewRequest(http.MethodPut, "/put", nil)53 w := httptest.NewRecorder()54 engine().ServeHTTP(w, req)5556 assert.Equal(t, http.StatusOK, w.Code)57 assert.Equal(t, "updated", w.Body.String())58}5960func TestDELETE(t *testing.T) {61 DELETE("/delete", func(c *gin.Context) {62 c.String(http.StatusOK, "deleted")63 })6465 req := httptest.NewRequest(http.MethodDelete, "/delete", nil)66 w := httptest.NewRecorder()67 engine().ServeHTTP(w, req)6869 assert.Equal(t, http.StatusOK, w.Code)70 assert.Equal(t, "deleted", w.Body.String())71}7273func TestPATCH(t *testing.T) {74 PATCH("/patch", func(c *gin.Context) {75 c.String(http.StatusOK, "patched")76 })7778 req := httptest.NewRequest(http.MethodPatch, "/patch", nil)79 w := httptest.NewRecorder()80 engine().ServeHTTP(w, req)8182 assert.Equal(t, http.StatusOK, w.Code)83 assert.Equal(t, "patched", w.Body.String())84}8586func TestOPTIONS(t *testing.T) {87 OPTIONS("/options", func(c *gin.Context) {88 c.String(http.StatusOK, "options")89 })9091 req := httptest.NewRequest(http.MethodOptions, "/options", nil)92 w := httptest.NewRecorder()93 engine().ServeHTTP(w, req)9495 assert.Equal(t, http.StatusOK, w.Code)96 assert.Equal(t, "options", w.Body.String())97}9899func TestHEAD(t *testing.T) {100 HEAD("/head", func(c *gin.Context) {101 c.String(http.StatusOK, "head")102 })103104 req := httptest.NewRequest(http.MethodHead, "/head", nil)105 w := httptest.NewRecorder()106 engine().ServeHTTP(w, req)107108 assert.Equal(t, http.StatusOK, w.Code)109}110111func TestAny(t *testing.T) {112 Any("/any", func(c *gin.Context) {113 c.String(http.StatusOK, "any")114 })115116 req := httptest.NewRequest(http.MethodGet, "/any", nil)117 w := httptest.NewRecorder()118 engine().ServeHTTP(w, req)119120 assert.Equal(t, http.StatusOK, w.Code)121 assert.Equal(t, "any", w.Body.String())122}123124func TestHandle(t *testing.T) {125 Handle(http.MethodGet, "/handle", func(c *gin.Context) {126 c.String(http.StatusOK, "handle")127 })128129 req := httptest.NewRequest(http.MethodGet, "/handle", nil)130 w := httptest.NewRecorder()131 engine().ServeHTTP(w, req)132133 assert.Equal(t, http.StatusOK, w.Code)134 assert.Equal(t, "handle", w.Body.String())135}136137func TestGroup(t *testing.T) {138 group := Group("/group")139 group.GET("/test", func(c *gin.Context) {140 c.String(http.StatusOK, "group test")141 })142143 req := httptest.NewRequest(http.MethodGet, "/group/test", nil)144 w := httptest.NewRecorder()145 engine().ServeHTTP(w, req)146147 assert.Equal(t, http.StatusOK, w.Code)148 assert.Equal(t, "group test", w.Body.String())149}150151func TestUse(t *testing.T) {152 var middlewareExecuted bool153 Use(func(c *gin.Context) {154 middlewareExecuted = true155 c.Next()156 })157158 GET("/middleware-test", func(c *gin.Context) {159 c.String(http.StatusOK, "ok")160 })161162 req := httptest.NewRequest(http.MethodGet, "/middleware-test", nil)163 w := httptest.NewRecorder()164 engine().ServeHTTP(w, req)165166 assert.True(t, middlewareExecuted)167 assert.Equal(t, http.StatusOK, w.Code)168}169170func TestNoRoute(t *testing.T) {171 NoRoute(func(c *gin.Context) {172 c.String(http.StatusNotFound, "custom 404")173 })174175 req := httptest.NewRequest(http.MethodGet, "/nonexistent", nil)176 w := httptest.NewRecorder()177 engine().ServeHTTP(w, req)178179 assert.Equal(t, http.StatusNotFound, w.Code)180 assert.Equal(t, "custom 404", w.Body.String())181}182183func TestNoMethod(t *testing.T) {184 NoMethod(func(c *gin.Context) {185 c.String(http.StatusMethodNotAllowed, "method not allowed")186 })187188 // This just verifies that NoMethod is callable189 // Testing the actual behavior would require a separate engine instance190 assert.NotNil(t, engine())191}192193func TestRoutes(t *testing.T) {194 GET("/routes-test", func(c *gin.Context) {})195196 routes := Routes()197 assert.NotEmpty(t, routes)198199 found := false200 for _, route := range routes {201 if route.Path == "/routes-test" && route.Method == http.MethodGet {202 found = true203 break204 }205 }206 assert.True(t, found)207}208209func TestSetHTMLTemplate(t *testing.T) {210 tmpl := template.Must(template.New("test").Parse("Hello {{.}}"))211 SetHTMLTemplate(tmpl)212213 // Verify engine has template set214 assert.NotNil(t, engine())215}216217func TestStaticFile(t *testing.T) {218 StaticFile("/static-file", "../testdata/test_file.txt")219220 req := httptest.NewRequest(http.MethodGet, "/static-file", nil)221 w := httptest.NewRecorder()222 engine().ServeHTTP(w, req)223224 assert.Equal(t, http.StatusOK, w.Code)225}226227func TestStatic(t *testing.T) {228 Static("/static-dir", "../testdata")229230 req := httptest.NewRequest(http.MethodGet, "/static-dir/test_file.txt", nil)231 w := httptest.NewRecorder()232 engine().ServeHTTP(w, req)233234 assert.Equal(t, http.StatusOK, w.Code)235}236237func TestStaticFS(t *testing.T) {238 fs := http.Dir("../testdata")239 StaticFS("/static-fs", fs)240241 req := httptest.NewRequest(http.MethodGet, "/static-fs/test_file.txt", nil)242 w := httptest.NewRecorder()243 engine().ServeHTTP(w, req)244245 assert.Equal(t, http.StatusOK, w.Code)246}