Skip to content
Myr

routergroup_test.go

This file

Path

routergroup_test.go

245 lines · 6.5 KiB · Go

Last changed by

feat(routergroup): include HTTP QUERY method in Any (#4878)adam maguedbefore adoptione8f2496

History of this file

Take it

The walk stopped after 200 commits: lines older than cf32d2d point at it.

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 1–13why, one step back
1// Copyright 2014 Manu Martinez-Almeida. 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 gin67import (8 "net/http"9 "testing"1011 "github.com/stretchr/testify/assert"12)13

test(route): add some test for routergroup (#4291)chenhuiluobefore adoptiona4ac275

lines 14–15why, one step back
14var MaxHandlers = 32
15

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 16–44why, one step back
16func init() {
17 SetMode(TestMode)18}1920func TestRouterGroupBasic(t *testing.T) {21 router := New()22 group := router.Group("/hola", func(c *Context) {})23 group.Use(func(c *Context) {})2425 assert.Len(t, group.Handlers, 2)26 assert.Equal(t, "/hola", group.BasePath())27 assert.Equal(t, router, group.engine)2829 group2 := group.Group("manu")30 group2.Use(func(c *Context) {}, func(c *Context) {})3132 assert.Len(t, group2.Handlers, 4)33 assert.Equal(t, "/hola/manu", group2.BasePath())34 assert.Equal(t, router, group2.engine)35}3637func TestRouterGroupBasicHandle(t *testing.T) {38 performRequestInGroup(t, http.MethodGet)39 performRequestInGroup(t, http.MethodPost)40 performRequestInGroup(t, http.MethodPut)41 performRequestInGroup(t, http.MethodPatch)42 performRequestInGroup(t, http.MethodDelete)43 performRequestInGroup(t, http.MethodHead)44 performRequestInGroup(t, http.MethodOptions)

feat(gin): add support for the HTTP QUERY method (RFC 10008) (#4806)Gaurav Patilbefore adoptionb1f77cf

line 45why, one step back
45 performRequestInGroup(t, MethodQuery)

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 46–81why, one step back
46}
4748func performRequestInGroup(t *testing.T, method string) {49 router := New()50 v1 := router.Group("v1", func(c *Context) {})51 assert.Equal(t, "/v1", v1.BasePath())5253 login := v1.Group("/login/", func(c *Context) {}, func(c *Context) {})54 assert.Equal(t, "/v1/login/", login.BasePath())5556 handler := func(c *Context) {57 c.String(http.StatusBadRequest, "the method was %s and index %d", c.Request.Method, c.index)58 }5960 switch method {61 case http.MethodGet:62 v1.GET("/test", handler)63 login.GET("/test", handler)64 case http.MethodPost:65 v1.POST("/test", handler)66 login.POST("/test", handler)67 case http.MethodPut:68 v1.PUT("/test", handler)69 login.PUT("/test", handler)70 case http.MethodPatch:71 v1.PATCH("/test", handler)72 login.PATCH("/test", handler)73 case http.MethodDelete:74 v1.DELETE("/test", handler)75 login.DELETE("/test", handler)76 case http.MethodHead:77 v1.HEAD("/test", handler)78 login.HEAD("/test", handler)79 case http.MethodOptions:80 v1.OPTIONS("/test", handler)81 login.OPTIONS("/test", handler)

feat(gin): add support for the HTTP QUERY method (RFC 10008) (#4806)Gaurav Patilbefore adoptionb1f77cf

lines 82–84why, one step back
82 case MethodQuery:
83 v1.QUERY("/test", handler)84 login.QUERY("/test", handler)

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

lines 85–200why, one step back
85 default:
86 panic("unknown method")87 }8889 w := PerformRequest(router, method, "/v1/login/test")90 assert.Equal(t, http.StatusBadRequest, w.Code)91 assert.Equal(t, "the method was "+method+" and index 3", w.Body.String())9293 w = PerformRequest(router, method, "/v1/test")94 assert.Equal(t, http.StatusBadRequest, w.Code)95 assert.Equal(t, "the method was "+method+" and index 1", w.Body.String())96}9798func TestRouterGroupInvalidStatic(t *testing.T) {99 router := New()100 assert.Panics(t, func() {101 router.Static("/path/:param", "/")102 })103104 assert.Panics(t, func() {105 router.Static("/path/*param", "/")106 })107}108109func TestRouterGroupInvalidStaticFile(t *testing.T) {110 router := New()111 assert.Panics(t, func() {112 router.StaticFile("/path/:param", "favicon.ico")113 })114115 assert.Panics(t, func() {116 router.StaticFile("/path/*param", "favicon.ico")117 })118}119120func TestRouterGroupInvalidStaticFileFS(t *testing.T) {121 router := New()122 assert.Panics(t, func() {123 router.StaticFileFS("/path/:param", "favicon.ico", Dir(".", false))124 })125126 assert.Panics(t, func() {127 router.StaticFileFS("/path/*param", "favicon.ico", Dir(".", false))128 })129}130131func TestRouterGroupTooManyHandlers(t *testing.T) {132 const (133 panicValue = "too many handlers"134 maximumCnt = abortIndex135 )136 router := New()137 handlers1 := make([]HandlerFunc, maximumCnt-1)138 router.Use(handlers1...)139140 handlers2 := make([]HandlerFunc, maximumCnt+1)141 assert.PanicsWithValue(t, panicValue, func() {142 router.Use(handlers2...)143 })144 assert.PanicsWithValue(t, panicValue, func() {145 router.GET("/", handlers2...)146 })147}148149func TestRouterGroupBadMethod(t *testing.T) {150 router := New()151 assert.Panics(t, func() {152 router.Handle(http.MethodGet, "/")153 })154 assert.Panics(t, func() {155 router.Handle(" GET", "/")156 })157 assert.Panics(t, func() {158 router.Handle("GET ", "/")159 })160 assert.Panics(t, func() {161 router.Handle("", "/")162 })163 assert.Panics(t, func() {164 router.Handle("PO ST", "/")165 })166 assert.Panics(t, func() {167 router.Handle("1GET", "/")168 })169 assert.Panics(t, func() {170 router.Handle("PATCh", "/")171 })172}173174func TestRouterGroupPipeline(t *testing.T) {175 router := New()176 testRoutesInterface(t, router)177178 v1 := router.Group("/v1")179 testRoutesInterface(t, v1)180}181182func testRoutesInterface(t *testing.T, r IRoutes) {183 handler := func(c *Context) {}184 assert.Equal(t, r, r.Use(handler))185186 assert.Equal(t, r, r.Handle(http.MethodGet, "/handler", handler))187 assert.Equal(t, r, r.Any("/any", handler))188 assert.Equal(t, r, r.GET("/", handler))189 assert.Equal(t, r, r.POST("/", handler))190 assert.Equal(t, r, r.DELETE("/", handler))191 assert.Equal(t, r, r.PATCH("/", handler))192 assert.Equal(t, r, r.PUT("/", handler))193 assert.Equal(t, r, r.OPTIONS("/", handler))194 assert.Equal(t, r, r.HEAD("/", handler))195 assert.Equal(t, r, r.Match([]string{http.MethodPut, http.MethodPatch}, "/match", handler))196197 assert.Equal(t, r, r.StaticFile("/file", "."))198 assert.Equal(t, r, r.StaticFileFS("/static2", ".", Dir(".", false)))199 assert.Equal(t, r, r.Static("/static", "."))200 assert.Equal(t, r, r.StaticFS("/static2", Dir(".", false)))

test(route): add some test for routergroup (#4291)chenhuiluobefore adoptiona4ac275

lines 201–222why, one step back
201}
202203func TestRouterGroupCombineHandlersTooManyHandlers(t *testing.T) {204 group := &RouterGroup{205 Handlers: make(HandlersChain, MaxHandlers), // Assume group already has MaxHandlers middleware206 }207 tooManyHandlers := make(HandlersChain, MaxHandlers) // Add MaxHandlers more, total 2 * MaxHandlers208209 // This should trigger panic210 assert.Panics(t, func() {211 group.combineHandlers(tooManyHandlers)212 }, "should panic due to too many handlers")213}214215func TestRouterGroupCombineHandlersEmptySliceNotNil(t *testing.T) {216 group := &RouterGroup{217 Handlers: HandlersChain{},218 }219220 result := group.combineHandlers(HandlersChain{})221 assert.NotNil(t, result, "result should not be nil even with empty handlers")222 assert.Empty(t, result, "empty handlers should return empty chain")

chore(deps): bump github.com/pelletier/go-toml/v2 from 2.2.2 to 2.2.4 (#4212)dependabot[bot]before adoptioncf32d2d

line 223why, one step back
223}

feat(routergroup): include HTTP QUERY method in Any (#4878)adam maguedbefore adoptione8f2496

lines 224–245why, one step back
224
225func TestRouterGroupAny(t *testing.T) {226 router := New()227 router.Any("/any", func(c *Context) {228 c.String(http.StatusOK, c.Request.Method)229 })230231 v1 := router.Group("/v1")232 v1.Any("/any", func(c *Context) {233 c.String(http.StatusOK, "v1:"+c.Request.Method)234 })235236 for _, method := range anyMethods {237 w := PerformRequest(router, method, "/any")238 assert.Equal(t, http.StatusOK, w.Code)239 assert.Equal(t, method, w.Body.String())240241 w = PerformRequest(router, method, "/v1/any")242 assert.Equal(t, http.StatusOK, w.Code)243 assert.Equal(t, "v1:"+method, w.Body.String())244 }245}