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
Provenance
- Commit
c358d56- Tree
762dc2b- Author
Milad <miladev95@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
771dcc6
1 file changed+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.4+5+package ginS6+7+import (8+ "html/template"9+ "net/http"10+ "net/http/httptest"11+ "testing"12+13+ "github.com/gin-gonic/gin"14+ "github.com/stretchr/testify/assert"15+)16+17+func init() {18+ gin.SetMode(gin.TestMode)19+}20+21+func TestGET(t *testing.T) {22+ GET("/test", func(c *gin.Context) {23+ c.String(http.StatusOK, "test")24+ })25+26+ req := httptest.NewRequest(http.MethodGet, "/test", nil)27+ w := httptest.NewRecorder()28+ engine().ServeHTTP(w, req)29+30+ assert.Equal(t, http.StatusOK, w.Code)31+ assert.Equal(t, "test", w.Body.String())32+}33+34+func TestPOST(t *testing.T) {35+ POST("/post", func(c *gin.Context) {36+ c.String(http.StatusCreated, "created")37+ })38+39+ req := httptest.NewRequest(http.MethodPost, "/post", nil)40+ w := httptest.NewRecorder()41+ engine().ServeHTTP(w, req)42+43+ assert.Equal(t, http.StatusCreated, w.Code)44+ assert.Equal(t, "created", w.Body.String())45+}46+47+func TestPUT(t *testing.T) {48+ PUT("/put", func(c *gin.Context) {49+ c.String(http.StatusOK, "updated")50+ })51+52+ req := httptest.NewRequest(http.MethodPut, "/put", nil)53+ w := httptest.NewRecorder()54+ engine().ServeHTTP(w, req)55+56+ assert.Equal(t, http.StatusOK, w.Code)57+ assert.Equal(t, "updated", w.Body.String())58+}59+60+func TestDELETE(t *testing.T) {61+ DELETE("/delete", func(c *gin.Context) {62+ c.String(http.StatusOK, "deleted")63+ })64+65+ req := httptest.NewRequest(http.MethodDelete, "/delete", nil)66+ w := httptest.NewRecorder()67+ engine().ServeHTTP(w, req)68+69+ assert.Equal(t, http.StatusOK, w.Code)70+ assert.Equal(t, "deleted", w.Body.String())71+}72+73+func TestPATCH(t *testing.T) {74+ PATCH("/patch", func(c *gin.Context) {75+ c.String(http.StatusOK, "patched")76+ })77+78+ req := httptest.NewRequest(http.MethodPatch, "/patch", nil)79+ w := httptest.NewRecorder()80+ engine().ServeHTTP(w, req)81+82+ assert.Equal(t, http.StatusOK, w.Code)83+ assert.Equal(t, "patched", w.Body.String())84+}85+86+func TestOPTIONS(t *testing.T) {87+ OPTIONS("/options", func(c *gin.Context) {88+ c.String(http.StatusOK, "options")89+ })90+91+ req := httptest.NewRequest(http.MethodOptions, "/options", nil)92+ w := httptest.NewRecorder()93+ engine().ServeHTTP(w, req)94+95+ assert.Equal(t, http.StatusOK, w.Code)96+ assert.Equal(t, "options", w.Body.String())97+}98+99+func TestHEAD(t *testing.T) {100+ HEAD("/head", func(c *gin.Context) {101+ c.String(http.StatusOK, "head")102+ })103+104+ req := httptest.NewRequest(http.MethodHead, "/head", nil)105+ w := httptest.NewRecorder()106+ engine().ServeHTTP(w, req)107+108+ assert.Equal(t, http.StatusOK, w.Code)109+}110+111+func TestAny(t *testing.T) {112+ Any("/any", func(c *gin.Context) {113+ c.String(http.StatusOK, "any")114+ })115+116+ req := httptest.NewRequest(http.MethodGet, "/any", nil)117+ w := httptest.NewRecorder()118+ engine().ServeHTTP(w, req)119+120+ assert.Equal(t, http.StatusOK, w.Code)121+ assert.Equal(t, "any", w.Body.String())122+}123+124+func TestHandle(t *testing.T) {125+ Handle(http.MethodGet, "/handle", func(c *gin.Context) {126+ c.String(http.StatusOK, "handle")127+ })128+129+ req := httptest.NewRequest(http.MethodGet, "/handle", nil)130+ w := httptest.NewRecorder()131+ engine().ServeHTTP(w, req)132+133+ assert.Equal(t, http.StatusOK, w.Code)134+ assert.Equal(t, "handle", w.Body.String())135+}136+137+func TestGroup(t *testing.T) {138+ group := Group("/group")139+ group.GET("/test", func(c *gin.Context) {140+ c.String(http.StatusOK, "group test")141+ })142+143+ req := httptest.NewRequest(http.MethodGet, "/group/test", nil)144+ w := httptest.NewRecorder()145+ engine().ServeHTTP(w, req)146+147+ assert.Equal(t, http.StatusOK, w.Code)148+ assert.Equal(t, "group test", w.Body.String())149+}150+151+func TestUse(t *testing.T) {152+ var middlewareExecuted bool153+ Use(func(c *gin.Context) {154+ middlewareExecuted = true155+ c.Next()156+ })157+158+ GET("/middleware-test", func(c *gin.Context) {159+ c.String(http.StatusOK, "ok")160+ })161+162+ req := httptest.NewRequest(http.MethodGet, "/middleware-test", nil)163+ w := httptest.NewRecorder()164+ engine().ServeHTTP(w, req)165+166+ assert.True(t, middlewareExecuted)167+ assert.Equal(t, http.StatusOK, w.Code)168+}169+170+func TestNoRoute(t *testing.T) {171+ NoRoute(func(c *gin.Context) {172+ c.String(http.StatusNotFound, "custom 404")173+ })174+175+ req := httptest.NewRequest(http.MethodGet, "/nonexistent", nil)176+ w := httptest.NewRecorder()177+ engine().ServeHTTP(w, req)178+179+ assert.Equal(t, http.StatusNotFound, w.Code)180+ assert.Equal(t, "custom 404", w.Body.String())181+}182+183+func TestNoMethod(t *testing.T) {184+ NoMethod(func(c *gin.Context) {185+ c.String(http.StatusMethodNotAllowed, "method not allowed")186+ })187+188+ // This just verifies that NoMethod is callable189+ // Testing the actual behavior would require a separate engine instance190+ assert.NotNil(t, engine())191+}192+193+func TestRoutes(t *testing.T) {194+ GET("/routes-test", func(c *gin.Context) {})195+196+ routes := Routes()197+ assert.NotEmpty(t, routes)198+199+ 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+}208+209+func TestSetHTMLTemplate(t *testing.T) {210+ tmpl := template.Must(template.New("test").Parse("Hello {{.}}"))211+ SetHTMLTemplate(tmpl)212+213+ // Verify engine has template set214+ assert.NotNil(t, engine())215+}216+217+func TestStaticFile(t *testing.T) {218+ StaticFile("/static-file", "../testdata/test_file.txt")219+220+ req := httptest.NewRequest(http.MethodGet, "/static-file", nil)221+ w := httptest.NewRecorder()222+ engine().ServeHTTP(w, req)223+224+ assert.Equal(t, http.StatusOK, w.Code)225+}226+227+func TestStatic(t *testing.T) {228+ Static("/static-dir", "../testdata")229+230+ req := httptest.NewRequest(http.MethodGet, "/static-dir/test_file.txt", nil)231+ w := httptest.NewRecorder()232+ engine().ServeHTTP(w, req)233+234+ assert.Equal(t, http.StatusOK, w.Code)235+}236+237+func TestStaticFS(t *testing.T) {238+ fs := http.Dir("../testdata")239+ StaticFS("/static-fs", fs)240+241+ req := httptest.NewRequest(http.MethodGet, "/static-fs/test_file.txt", nil)242+ w := httptest.NewRecorder()243+ engine().ServeHTTP(w, req)244+245+ assert.Equal(t, http.StatusOK, w.Code)246+}