diff --git a/context.go b/context.go index c724daf3..34b47940 100644 --- a/context.go +++ b/context.go @@ -1272,7 +1272,9 @@ func (c *Context) NegotiateFormat(offered ...string) string { assert1(len(offered) > 0, "you must provide at least one offer") if c.Accepted == nil { - c.Accepted = parseAccept(c.requestHeader("Accept")) + for _, a := range c.Request.Header.Values("Accept") { + c.Accepted = append(c.Accepted, parseAccept(a)...) + } } if len(c.Accepted) == 0 { return offered[0] diff --git a/context_test.go b/context_test.go index ef0cfccd..970ac47c 100644 --- a/context_test.go +++ b/context_test.go @@ -1532,6 +1532,18 @@ func TestContextNegotiationFormatWithAccept(t *testing.T) { assert.Empty(t, c.NegotiateFormat(MIMEJSON)) } +func TestContextNegotiationFormatWithMultipleAccept(t *testing.T) { + c, _ := CreateTestContext(httptest.NewRecorder()) + c.Request, _ = http.NewRequest("POST", "/", nil) + c.Request.Header.Add("Accept", "text/html") + c.Request.Header.Add("Accept", "application/xhtml+xml") + c.Request.Header.Add("Accept", "application/xml;q=0.9;q=0.8") + + assert.Equal(t, MIMEXML, c.NegotiateFormat(MIMEJSON, MIMEXML)) + assert.Equal(t, MIMEHTML, c.NegotiateFormat(MIMEXML, MIMEHTML)) + assert.Empty(t, c.NegotiateFormat(MIMEJSON)) +} + func TestContextNegotiationFormatWithWildcardAccept(t *testing.T) { c, _ := CreateTestContext(httptest.NewRecorder()) c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)