Skip to content

Commit

Permalink
fix(context): panic on NegotiateFormat - index out of range (#3397)
Browse files Browse the repository at this point in the history
  • Loading branch information
adrianiacobghiula committed Jan 16, 2023
1 parent 3010cbd commit 7cb151b
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion context.go
Expand Up @@ -1147,7 +1147,7 @@ func (c *Context) NegotiateFormat(offered ...string) string {
// According to RFC 2616 and RFC 2396, non-ASCII characters are not allowed in headers,
// therefore we can just iterate over the string without casting it into []rune
i := 0
for ; i < len(accepted); i++ {
for ; i < len(accepted) && i < len(offer); i++ {
if accepted[i] == '*' || offer[i] == '*' {
return offer
}
Expand Down
8 changes: 8 additions & 0 deletions context_test.go
Expand Up @@ -1311,6 +1311,14 @@ func TestContextNegotiationFormatCustom(t *testing.T) {
assert.Equal(t, MIMEJSON, c.NegotiateFormat(MIMEJSON))
}

func TestContextNegotiationFormat2(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", nil)
c.Request.Header.Add("Accept", "image/tiff-fx")

assert.Equal(t, "", c.NegotiateFormat("image/tiff"))
}

func TestContextIsAborted(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder())
assert.False(t, c.IsAborted())
Expand Down

0 comments on commit 7cb151b

Please sign in to comment.