mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 12:12:12 +08:00
Merge f32f8cb6e5692ce26a0ca6a42c6fbdc10a868ae7 into 3b28645dc95d58e0df36b8aff7a6c64f7c0ca5e9
This commit is contained in:
commit
65ac95e387
2
gin.go
2
gin.go
@ -637,10 +637,12 @@ func (engine *Engine) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
// Disclaimer: You can loop yourself to deal with this, use wisely.
|
||||
func (engine *Engine) HandleContext(c *Context) {
|
||||
oldIndexValue := c.index
|
||||
oldHandlers := c.handlers
|
||||
c.reset()
|
||||
engine.handleHTTPRequest(c)
|
||||
|
||||
c.index = oldIndexValue
|
||||
c.handlers = oldHandlers
|
||||
}
|
||||
|
||||
func (engine *Engine) handleHTTPRequest(c *Context) {
|
||||
|
40
gin_test.go
40
gin_test.go
@ -573,6 +573,46 @@ func TestEngineHandleContextManyReEntries(t *testing.T) {
|
||||
assert.Equal(t, int64(expectValue), middlewareCounter)
|
||||
}
|
||||
|
||||
func TestEngineHandleContextReEntriesWithDifferentMiddlewares(t *testing.T) {
|
||||
var (
|
||||
expectedResp = "ok"
|
||||
v1MiddlewareCounter1, v2MiddlewareCounter1, v2MiddlewareCounter2 int64
|
||||
)
|
||||
|
||||
r := New()
|
||||
|
||||
r.GET("/v1",
|
||||
func(c *Context) { /* V1middleware1 */
|
||||
atomic.AddInt64(&v1MiddlewareCounter1, 1)
|
||||
},
|
||||
func(c *Context) {
|
||||
c.Request.URL.Path = "/v2"
|
||||
r.HandleContext(c)
|
||||
})
|
||||
|
||||
r.GET("/v2",
|
||||
func(c *Context) { /* V2middleware1 */
|
||||
atomic.AddInt64(&v2MiddlewareCounter1, 1)
|
||||
},
|
||||
func(c *Context) { /* v2middleware2 */
|
||||
atomic.AddInt64(&v2MiddlewareCounter2, 1)
|
||||
},
|
||||
func(c *Context) {
|
||||
c.String(200, expectedResp)
|
||||
})
|
||||
|
||||
assert.NotPanics(t, func() {
|
||||
w := PerformRequest(r, "GET", "/v1")
|
||||
assert.Equal(t, 200, w.Code)
|
||||
assert.Equal(t, expectedResp, w.Body.String())
|
||||
})
|
||||
|
||||
// all the middlewares should be called independently from each router
|
||||
assert.Equal(t, int64(1), v1MiddlewareCounter1)
|
||||
assert.Equal(t, int64(1), v2MiddlewareCounter1)
|
||||
assert.Equal(t, int64(1), v2MiddlewareCounter2)
|
||||
}
|
||||
|
||||
func TestPrepareTrustedCIRDsWith(t *testing.T) {
|
||||
r := New()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user