forked from mirror/gin
This commit is contained in:
parent
971fe21876
commit
55e27f1246
|
@ -146,7 +146,7 @@ func TestSaveUploadedCreateFailed(t *testing.T) {
|
||||||
|
|
||||||
func TestContextReset(t *testing.T) {
|
func TestContextReset(t *testing.T) {
|
||||||
router := New()
|
router := New()
|
||||||
c := router.allocateContext()
|
c := router.allocateContext(0)
|
||||||
assert.Equal(t, c.engine, router)
|
assert.Equal(t, c.engine, router)
|
||||||
|
|
||||||
c.index = 2
|
c.index = 2
|
||||||
|
@ -2354,3 +2354,17 @@ func TestContextAddParam(t *testing.T) {
|
||||||
assert.Equal(t, ok, true)
|
assert.Equal(t, ok, true)
|
||||||
assert.Equal(t, value, v)
|
assert.Equal(t, value, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCreateTestContextWithRouteParams(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
engine := New()
|
||||||
|
engine.GET("/:action/:name", func(ctx *Context) {
|
||||||
|
ctx.String(http.StatusOK, "%s %s", ctx.Param("action"), ctx.Param("name"))
|
||||||
|
})
|
||||||
|
c := CreateTestContextOnly(w, engine)
|
||||||
|
c.Request, _ = http.NewRequest(http.MethodGet, "/hello/gin", nil)
|
||||||
|
engine.HandleContext(c)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusOK, w.Code)
|
||||||
|
assert.Equal(t, "hello gin", w.Body.String())
|
||||||
|
}
|
||||||
|
|
6
gin.go
6
gin.go
|
@ -203,7 +203,7 @@ func New() *Engine {
|
||||||
}
|
}
|
||||||
engine.RouterGroup.engine = engine
|
engine.RouterGroup.engine = engine
|
||||||
engine.pool.New = func() any {
|
engine.pool.New = func() any {
|
||||||
return engine.allocateContext()
|
return engine.allocateContext(engine.maxParams)
|
||||||
}
|
}
|
||||||
return engine
|
return engine
|
||||||
}
|
}
|
||||||
|
@ -225,8 +225,8 @@ func (engine *Engine) Handler() http.Handler {
|
||||||
return h2c.NewHandler(engine, h2s)
|
return h2c.NewHandler(engine, h2s)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (engine *Engine) allocateContext() *Context {
|
func (engine *Engine) allocateContext(maxParams uint16) *Context {
|
||||||
v := make(Params, 0, engine.maxParams)
|
v := make(Params, 0, maxParams)
|
||||||
skippedNodes := make([]skippedNode, 0, engine.maxSections)
|
skippedNodes := make([]skippedNode, 0, engine.maxSections)
|
||||||
return &Context{engine: engine, params: &v, skippedNodes: &skippedNodes}
|
return &Context{engine: engine, params: &v, skippedNodes: &skippedNodes}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,15 @@ import "net/http"
|
||||||
// CreateTestContext returns a fresh engine and context for testing purposes
|
// CreateTestContext returns a fresh engine and context for testing purposes
|
||||||
func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine) {
|
func CreateTestContext(w http.ResponseWriter) (c *Context, r *Engine) {
|
||||||
r = New()
|
r = New()
|
||||||
c = r.allocateContext()
|
c = r.allocateContext(0)
|
||||||
|
c.reset()
|
||||||
|
c.writermem.reset(w)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// CreateTestContextOnly returns a fresh context base on the engine for testing purposes
|
||||||
|
func CreateTestContextOnly(w http.ResponseWriter, r *Engine) (c *Context) {
|
||||||
|
c = r.allocateContext(r.maxParams)
|
||||||
c.reset()
|
c.reset()
|
||||||
c.writermem.reset(w)
|
c.writermem.reset(w)
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue