Merge 4787b8203b79012877ac98d7806422da3a678ba2 into 3b28645dc95d58e0df36b8aff7a6c64f7c0ca5e9

This commit is contained in:
tobi 2025-02-13 22:42:49 +08:00 committed by GitHub
commit e9fbaa6d57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 1 deletions

View File

@ -1272,7 +1272,9 @@ func (c *Context) NegotiateFormat(offered ...string) string {
assert1(len(offered) > 0, "you must provide at least one offer") assert1(len(offered) > 0, "you must provide at least one offer")
if c.Accepted == nil { 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 { if len(c.Accepted) == 0 {
return offered[0] return offered[0]

View File

@ -1532,6 +1532,18 @@ func TestContextNegotiationFormatWithAccept(t *testing.T) {
assert.Empty(t, c.NegotiateFormat(MIMEJSON)) 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) { func TestContextNegotiationFormatWithWildcardAccept(t *testing.T) {
c, _ := CreateTestContext(httptest.NewRecorder()) c, _ := CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest(http.MethodPost, "/", nil) c.Request, _ = http.NewRequest(http.MethodPost, "/", nil)