From 03df4383e252c8258edace85c5e670952bc7dbac Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Fri, 27 May 2022 16:28:19 +0200 Subject: [PATCH 1/2] look through all Accept headers when negotiating --- context.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/context.go b/context.go index 5e53aaf0..5ce608ee 100644 --- a/context.go +++ b/context.go @@ -1100,7 +1100,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] From 4787b8203b79012877ac98d7806422da3a678ba2 Mon Sep 17 00:00:00 2001 From: tsmethurst Date: Fri, 27 May 2022 16:28:31 +0200 Subject: [PATCH 2/2] test multiple Accept header negotiation --- context_test.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/context_test.go b/context_test.go index 20038e93..ff8e8bf8 100644 --- a/context_test.go +++ b/context_test.go @@ -1231,6 +1231,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("POST", "/", nil)