Skip to content
Myr

feat(context): add Protocol Buffers support to content negotiation (#4423)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Name · committed by GitHub · parent 0c0e99d · Browse files · 2 files+40 added−8 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: 1911860538 <alxps1911@gmail.com>

Diff stats

2 files+40 added−8 removed

  • other:+40 added−8 removed

2 files+40 added−8 removed

modified:context_test.go+26 added
1626 unmodified lines
1627 assert.Equal(t, "Hello gin", w.Body.String())1628 assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))1629}16301631func TestContextNegotiationWithPROTOBUF(t *testing.T) {1632 w := httptest.NewRecorder()1633 c, _ := CreateTestContext(w)1634 c.Request = httptest.NewRequest(http.MethodPost, "/", nil)16351636 reps := []int64{int64(1), int64(2)}1637 label := "test"1638 data := &testdata.Test{1639 Label: &label,1640 Reps: reps,1641 }16421643 c.Negotiate(http.StatusCreated, Negotiate{1644 Offered: []string{MIMEPROTOBUF, MIMEJSON, MIMEXML},1645 Data: data,1646 })16471648 // Marshal original data for comparison1649 protoData, err := proto.Marshal(data)1650 require.NoError(t, err)16511652 assert.Equal(t, http.StatusCreated, w.Code)1653 assert.Equal(t, string(protoData), w.Body.String())1654 assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))1655}16561657func TestContextNegotiationNotSupport(t *testing.T) {1658 w := httptest.NewRecorder()1659 c, _ := CreateTestContext(w)1660 c.Request, _ = http.NewRequest(http.MethodPost, "", nil)
2019 unmodified lines
modified:context.go+14 added-8 removed
37 unmodified lines
38 MIMEMultipartPOSTForm = binding.MIMEMultipartPOSTForm39 MIMEYAML = binding.MIMEYAML40 MIMEYAML2 = binding.MIMEYAML241 MIMETOML = binding.MIMETOML42 MIMEPROTOBUF = binding.MIMEPROTOBUF43)4445// BodyBytesKey indicates a default body bytes key.46const BodyBytesKey = "_gin-gonic/gin/bodybyteskey"
1233 unmodified lines
1280/************************************/12811282// Negotiate contains all negotiations data.1283type Negotiate struct {1283 Offered []string1284 HTMLName string1285 HTMLData any1286 JSONData any1287 XMLData any1288 YAMLData any1289 Data any1290 TOMLData any1284 Offered []string1285 HTMLName string1286 HTMLData any1287 JSONData any1288 XMLData any1289 YAMLData any1290 Data any1291 TOMLData any1292 PROTOBUFData any1293}12941295// Negotiate calls different Render according to acceptable Accept format.1296func (c *Context) Negotiate(code int, config Negotiate) {
17 unmodified lines
1314 case binding.MIMETOML:1315 data := chooseData(config.TOMLData, config.Data)1316 c.TOML(code, data)13171318 case binding.MIMEPROTOBUF:1319 data := chooseData(config.PROTOBUFData, config.Data)1320 c.ProtoBuf(code, data)13211322 default:1323 c.AbortWithError(http.StatusNotAcceptable, errors.New("the accepted formats are not offered by the server")) //nolint: errcheck1324 }1325}
92 unmodified lines