mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-14 20:22:20 +08:00
Merge 4787b8203b79012877ac98d7806422da3a678ba2 into 3b28645dc95d58e0df36b8aff7a6c64f7c0ca5e9
This commit is contained in:
commit
e9fbaa6d57
@ -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]
|
||||||
|
@ -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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user