mirror of https://github.com/gin-gonic/gin.git
add yaml negotitation (#2220)
Co-authored-by: thinkerou <thinkerou@gmail.com>
This commit is contained in:
parent
0e4d8eaf07
commit
731c827892
|
@ -970,6 +970,7 @@ type Negotiate struct {
|
||||||
HTMLData interface{}
|
HTMLData interface{}
|
||||||
JSONData interface{}
|
JSONData interface{}
|
||||||
XMLData interface{}
|
XMLData interface{}
|
||||||
|
YAMLData interface{}
|
||||||
Data interface{}
|
Data interface{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -988,6 +989,10 @@ func (c *Context) Negotiate(code int, config Negotiate) {
|
||||||
data := chooseData(config.XMLData, config.Data)
|
data := chooseData(config.XMLData, config.Data)
|
||||||
c.XML(code, data)
|
c.XML(code, data)
|
||||||
|
|
||||||
|
case binding.MIMEYAML:
|
||||||
|
data := chooseData(config.YAMLData, config.Data)
|
||||||
|
c.YAML(code, data)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server")) // nolint: errcheck
|
c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server")) // nolint: errcheck
|
||||||
}
|
}
|
||||||
|
|
|
@ -1114,7 +1114,7 @@ func TestContextNegotiationWithJSON(t *testing.T) {
|
||||||
c.Request, _ = http.NewRequest("POST", "", nil)
|
c.Request, _ = http.NewRequest("POST", "", nil)
|
||||||
|
|
||||||
c.Negotiate(http.StatusOK, Negotiate{
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
Offered: []string{MIMEJSON, MIMEXML},
|
Offered: []string{MIMEJSON, MIMEXML, MIMEYAML},
|
||||||
Data: H{"foo": "bar"},
|
Data: H{"foo": "bar"},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -1129,7 +1129,7 @@ func TestContextNegotiationWithXML(t *testing.T) {
|
||||||
c.Request, _ = http.NewRequest("POST", "", nil)
|
c.Request, _ = http.NewRequest("POST", "", nil)
|
||||||
|
|
||||||
c.Negotiate(http.StatusOK, Negotiate{
|
c.Negotiate(http.StatusOK, Negotiate{
|
||||||
Offered: []string{MIMEXML, MIMEJSON},
|
Offered: []string{MIMEXML, MIMEJSON, MIMEYAML},
|
||||||
Data: H{"foo": "bar"},
|
Data: H{"foo": "bar"},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue