Skip to content
Myr

fix(context): check handler is nil (#3413)

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

* fixed #3404 2022-11-23 * up 2022-11-23 * refactor: refactor context handling and nil checks - Refactor nil checks to improve readability in `context.go` - Modify the control flow in `HandlerNames` and `Next` methods to continue on nil values before appending or invoking handlers in `context.go` Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> * test: refactor context_test.go for clarity and efficiency - Insert a `nil` value into the `HandlersChain` array in `context_test.go` - Remove empty test functions in `context_test.go` Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com> ---------

authored by 51pwn · committed by GitHub · parent 3f5b0af · Browse files · 2 files+8 added−3 removed
This commit

State

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

Trailers

  • Signed-off-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>

Diff stats

2 files+8 added−3 removed

  • other:+8 added−3 removed

2 files+8 added−3 removed

modified:context_test.go+2 added-3 removed
360 unmodified lines
361}362363func TestContextHandlerNames(t *testing.T) {364 c, _ := CreateTestContext(httptest.NewRecorder())365 c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest, func(c *Context) {}, handlerNameTest2}365 c.handlers = HandlersChain{func(c *Context) {}, nil, handlerNameTest, func(c *Context) {}, handlerNameTest2}366367 names := c.HandlerNames()368369 assert.True(t, len(names) == 4)
1300 unmodified lines
1670 assert.Equal(t, 0, w.Body.Len())1671}16721673func TestContextBindPlain(t *testing.T) {16741674 // string1675 w := httptest.NewRecorder()1676 c, _ := CreateTestContext(w)1677 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString(`test string`))
183 unmodified lines
18611862 assert.NoError(t, c.ShouldBindPlain(&bs))1863 assert.Equal(t, []byte("test []byte"), bs)1864 assert.Equal(t, 0, w.Body.Len())18661865}18661867func TestContextShouldBindHeader(t *testing.T) {1868 w := httptest.NewRecorder()
499 unmodified lines
2368 assert.Equal(t, typeJSON{}, objJSON)2369 }2370 }2371}23722373func TestContextGolangContext(t *testing.T) {2374 c, _ := CreateTestContext(httptest.NewRecorder())2375 c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))2376 assert.NoError(t, c.Err())
473 unmodified lines
modified:context.go+6 added
150 unmodified lines
151// following the semantics of HandlerName()152func (c *Context) HandlerNames() []string {153 hn := make([]string, 0, len(c.handlers))154 for _, val := range c.handlers {155 if val == nil {156 continue157 }158 hn = append(hn, nameOfFunction(val))159 }160 return hn161}
22 unmodified lines
184// See example in GitHub.185func (c *Context) Next() {186 c.index++187 for c.index < int8(len(c.handlers)) {188 if c.handlers[c.index] == nil {189 continue190 }191 c.handlers[c.index](c)192 c.index++193 }194}
1094 unmodified lines