fix(context): render XML in Negotiate when text/xml is offered

This commit is contained in:
Dv2Corn 2026-11-12 13:53:03 +00:00
parent 0f09c3a9b4
commit ea2343fade
2 changed files with 18 additions and 1 deletions

View File

@ -1425,7 +1425,7 @@ func (c *Context) Negotiate(code int, config Negotiate) {
data := chooseData(config.HTMLData, config.Data)
c.HTML(code, config.HTMLName, data)
case binding.MIMEXML:
case binding.MIMEXML, binding.MIMEXML2:
data := chooseData(config.XMLData, config.Data)
c.XML(code, data)

View File

@ -1844,6 +1844,23 @@ func TestContextNegotiationWithXML(t *testing.T) {
assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
}
func TestContextNegotiationWithXML2(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)
c.Request, _ = http.NewRequest(http.MethodGet, "/", nil)
c.Request.Header.Set("Accept", MIMEXML2)
c.Negotiate(http.StatusOK, Negotiate{
Offered: []string{MIMEJSON, MIMEXML2},
Data: H{"foo": "bar"},
})
assert.Equal(t, http.StatusOK, w.Code)
assert.Equal(t, "<map><foo>bar</foo></map>", w.Body.String())
assert.Equal(t, "application/xml; charset=utf-8", w.Header().Get("Content-Type"))
assert.False(t, c.IsAborted())
}
func TestContextNegotiationWithYAML(t *testing.T) {
w := httptest.NewRecorder()
c, _ := CreateTestContext(w)