Skip to content

Commit

Permalink
add yaml negotitation (gin-gonic#2220)
Browse files Browse the repository at this point in the history
Co-authored-by: thinkerou <[email protected]>
  • Loading branch information
2 people authored and ThomasObenaus committed Feb 19, 2020
1 parent 79f6cec commit e619a88
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 2 deletions.
5 changes: 5 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,7 @@ type Negotiate struct {
HTMLData interface{}
JSONData interface{}
XMLData interface{}
YAMLData interface{}
Data interface{}
}

Expand All @@ -988,6 +989,10 @@ func (c *Context) Negotiate(code int, config Negotiate) {
data := chooseData(config.XMLData, config.Data)
c.XML(code, data)

case binding.MIMEYAML:
data := chooseData(config.YAMLData, config.Data)
c.YAML(code, data)

default:
c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server")) // nolint: errcheck
}
Expand Down
4 changes: 2 additions & 2 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ func TestContextNegotiationWithJSON(t *testing.T) {
c.Request, _ = http.NewRequest("POST", "", nil)

c.Negotiate(http.StatusOK, Negotiate{
Offered: []string{MIMEJSON, MIMEXML},
Offered: []string{MIMEJSON, MIMEXML, MIMEYAML},
Data: H{"foo": "bar"},
})

Expand All @@ -1129,7 +1129,7 @@ func TestContextNegotiationWithXML(t *testing.T) {
c.Request, _ = http.NewRequest("POST", "", nil)

c.Negotiate(http.StatusOK, Negotiate{
Offered: []string{MIMEXML, MIMEJSON},
Offered: []string{MIMEXML, MIMEJSON, MIMEYAML},
Data: H{"foo": "bar"},
})

Expand Down

0 comments on commit e619a88

Please sign in to comment.