Skip to content
Myr

fix(engine): missing route params for CreateTestContext (#2778) (#2803)

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

authored by RoCry · committed by GitHub · parent 971fe21 · Browse files · 3 files+27 added−5 removed
This commit

State

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

Diff stats

3 files+27 added−5 removed

  • other:+27 added−5 removed

3 files+27 added−5 removed

modified:context_test.go+15 added-1 removed
144 unmodified lines
145}146147func TestContextReset(t *testing.T) {148 router := New()149 c := router.allocateContext()149 c := router.allocateContext(0)150 assert.Equal(t, c.engine, router)151152 c.index = 2153 c.Writer = &responseWriter{ResponseWriter: httptest.NewRecorder()}
2199 unmodified lines
2353 v, ok := c.Params.Get(id)2354 assert.Equal(t, ok, true)2355 assert.Equal(t, value, v)2356}23572358func TestCreateTestContextWithRouteParams(t *testing.T) {2359 w := httptest.NewRecorder()2360 engine := New()2361 engine.GET("/:action/:name", func(ctx *Context) {2362 ctx.String(http.StatusOK, "%s %s", ctx.Param("action"), ctx.Param("name"))2363 })2364 c := CreateTestContextOnly(w, engine)2365 c.Request, _ = http.NewRequest(http.MethodGet, "/hello/gin", nil)2366 engine.HandleContext(c)23672368 assert.Equal(t, http.StatusOK, w.Code)2369 assert.Equal(t, "hello gin", w.Body.String())2370}
modified:gin.go+3 added-3 removed
201 unmodified lines
202 trustedCIDRs: defaultTrustedCIDRs,203 }204 engine.RouterGroup.engine = engine205 engine.pool.New = func() any {206 return engine.allocateContext()206 return engine.allocateContext(engine.maxParams)207 }208 return engine209}210
13 unmodified lines
224 h2s := &http2.Server{}225 return h2c.NewHandler(engine, h2s)226}227228func (engine *Engine) allocateContext() *Context {229 v := make(Params, 0, engine.maxParams)228func (engine *Engine) allocateContext(maxParams uint16) *Context {229 v := make(Params, 0, maxParams)230 skippedNodes := make([]skippedNode, 0, engine.maxSections)231 return &Context{engine: engine, params: &v, skippedNodes: &skippedNodes}232}233
471 unmodified lines
modified:test_helpers.go+9 added-1 removed
7 unmodified lines
89// CreateTestContext returns a fresh engine and context for testing purposes10func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine) {11 r = New()12 c = r.allocateContext()12 c = r.allocateContext(0)13 c.reset()14 c.writermem.reset(w)15 return16}1718// CreateTestContextOnly returns a fresh context base on the engine for testing purposes19func CreateTestContextOnly(w http.ResponseWriter, r *Engine) (c *Context) {20 c = r.allocateContext(r.maxParams)21 c.reset()22 c.writermem.reset(w)23 return24}