mirror of https://github.com/gin-gonic/gin.git
chore(context): test context initialization and handler logic (#4087)
* enhance code imported by #3413 if it needs to check if the handler is nil, tie c.index shall always ++ * test: refactor test context initialization and handler logic - Remove an empty line in `TestContextInitQueryCache` - Add `TestContextNext` function with tests for `Next` method behavior with no handlers, one handler, and multiple handlers Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> --------- Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> Co-authored-by: zjj <zhong2plus@gmail.com>
This commit is contained in:
parent
c8a3adc657
commit
f875d87283
|
@ -186,10 +186,9 @@ func (c *Context) FullPath() string {
|
||||||
func (c *Context) Next() {
|
func (c *Context) Next() {
|
||||||
c.index++
|
c.index++
|
||||||
for c.index < int8(len(c.handlers)) {
|
for c.index < int8(len(c.handlers)) {
|
||||||
if c.handlers[c.index] == nil {
|
if c.handlers[c.index] != nil {
|
||||||
continue
|
|
||||||
}
|
|
||||||
c.handlers[c.index](c)
|
c.handlers[c.index](c)
|
||||||
|
}
|
||||||
c.index++
|
c.index++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -610,7 +610,6 @@ func TestContextInitQueryCache(t *testing.T) {
|
||||||
assert.Equal(t, test.expectedQueryCache, test.testContext.queryCache)
|
assert.Equal(t, test.expectedQueryCache, test.testContext.queryCache)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestContextDefaultQueryOnEmptyRequest(t *testing.T) {
|
func TestContextDefaultQueryOnEmptyRequest(t *testing.T) {
|
||||||
|
@ -3038,3 +3037,47 @@ func TestInterceptedHeader(t *testing.T) {
|
||||||
assert.Equal(t, "", w.Result().Header.Get("X-Test"))
|
assert.Equal(t, "", w.Result().Header.Get("X-Test"))
|
||||||
assert.Equal(t, "present", w.Result().Header.Get("X-Test-2"))
|
assert.Equal(t, "present", w.Result().Header.Get("X-Test-2"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextNext(t *testing.T) {
|
||||||
|
c, _ := CreateTestContext(httptest.NewRecorder())
|
||||||
|
|
||||||
|
// Test with no handlers
|
||||||
|
c.Next()
|
||||||
|
assert.Equal(t, int8(0), c.index)
|
||||||
|
|
||||||
|
// Test with one handler
|
||||||
|
c.index = -1
|
||||||
|
c.handlers = HandlersChain{func(c *Context) {
|
||||||
|
c.Set("key", "value")
|
||||||
|
}}
|
||||||
|
c.Next()
|
||||||
|
assert.Equal(t, int8(1), c.index)
|
||||||
|
value, exists := c.Get("key")
|
||||||
|
assert.True(t, exists)
|
||||||
|
assert.Equal(t, "value", value)
|
||||||
|
|
||||||
|
// Test with multiple handlers
|
||||||
|
c.handlers = HandlersChain{
|
||||||
|
func(c *Context) {
|
||||||
|
c.Set("key1", "value1")
|
||||||
|
c.Next()
|
||||||
|
c.Set("key2", "value2")
|
||||||
|
},
|
||||||
|
nil,
|
||||||
|
func(c *Context) {
|
||||||
|
c.Set("key3", "value3")
|
||||||
|
},
|
||||||
|
}
|
||||||
|
c.index = -1
|
||||||
|
c.Next()
|
||||||
|
assert.Equal(t, int8(4), c.index)
|
||||||
|
value, exists = c.Get("key1")
|
||||||
|
assert.True(t, exists)
|
||||||
|
assert.Equal(t, "value1", value)
|
||||||
|
value, exists = c.Get("key2")
|
||||||
|
assert.True(t, exists)
|
||||||
|
assert.Equal(t, "value2", value)
|
||||||
|
value, exists = c.Get("key3")
|
||||||
|
assert.True(t, exists)
|
||||||
|
assert.Equal(t, "value3", value)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue