Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(context): check handler is nil #3413

Merged
merged 4 commits into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@
func (c *Context) HandlerNames() []string {
hn := make([]string, 0, len(c.handlers))
for _, val := range c.handlers {
if val == nil {
continue
}
hn = append(hn, nameOfFunction(val))
}
return hn
Expand Down Expand Up @@ -182,6 +185,9 @@
func (c *Context) Next() {
c.index++
for c.index < int8(len(c.handlers)) {
if c.handlers[c.index] == nil {
continue

Check warning on line 189 in context.go

View check run for this annotation

Codecov / codecov/patch

context.go#L189

Added line #L189 was not covered by tests
}
c.handlers[c.index](c)
c.index++
}
Expand Down
5 changes: 2 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func TestContextHandlerName(t *testing.T) {

func TestContextHandlerNames(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.handlers = HandlersChain{func(c *Context) {}, handlerNameTest, func(c *Context) {}, handlerNameTest2}
c.handlers = HandlersChain{func(c *Context) {}, nil, handlerNameTest, func(c *Context) {}, handlerNameTest2}

names := c.HandlerNames()

Expand Down Expand Up @@ -1671,7 +1671,6 @@ func TestContextBindWithXML(t *testing.T) {
}

func TestContextBindPlain(t *testing.T) {

// string
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
Expand Down Expand Up @@ -1863,7 +1862,6 @@ func TestContextShouldBindPlain(t *testing.T) {
assert.NoError(t, c.ShouldBindPlain(&bs))
assert.Equal(t, []byte("test []byte"), bs)
assert.Equal(t, 0, w.Body.Len())

}

func TestContextShouldBindHeader(t *testing.T) {
Expand Down Expand Up @@ -2371,6 +2369,7 @@ func TestContextShouldBindBodyWithPlain(t *testing.T) {
}
}
}

func TestContextGolangContext(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", bytes.NewBufferString("{\"foo\":\"bar\", \"bar\":\"foo\"}"))
Expand Down
Loading