mirror of
https://github.com/gin-gonic/gin.git
synced 2025-11-15 16:02:15 +08:00
feat(context): add Protocol Buffers support to content negotiation (#4423)
Co-authored-by: 1911860538 <alxps1911@gmail.com>
This commit is contained in:
parent
0c0e99d253
commit
acc55e049e
@ -39,6 +39,7 @@ const (
|
|||||||
MIMEYAML = binding.MIMEYAML
|
MIMEYAML = binding.MIMEYAML
|
||||||
MIMEYAML2 = binding.MIMEYAML2
|
MIMEYAML2 = binding.MIMEYAML2
|
||||||
MIMETOML = binding.MIMETOML
|
MIMETOML = binding.MIMETOML
|
||||||
|
MIMEPROTOBUF = binding.MIMEPROTOBUF
|
||||||
)
|
)
|
||||||
|
|
||||||
// BodyBytesKey indicates a default body bytes key.
|
// BodyBytesKey indicates a default body bytes key.
|
||||||
@ -1288,6 +1289,7 @@ type Negotiate struct {
|
|||||||
YAMLData any
|
YAMLData any
|
||||||
Data any
|
Data any
|
||||||
TOMLData any
|
TOMLData any
|
||||||
|
PROTOBUFData any
|
||||||
}
|
}
|
||||||
|
|
||||||
// Negotiate calls different Render according to acceptable Accept format.
|
// Negotiate calls different Render according to acceptable Accept format.
|
||||||
@ -1313,6 +1315,10 @@ func (c *Context) Negotiate(code int, config Negotiate) {
|
|||||||
data := chooseData(config.TOMLData, config.Data)
|
data := chooseData(config.TOMLData, config.Data)
|
||||||
c.TOML(code, data)
|
c.TOML(code, data)
|
||||||
|
|
||||||
|
case binding.MIMEPROTOBUF:
|
||||||
|
data := chooseData(config.PROTOBUFData, config.Data)
|
||||||
|
c.ProtoBuf(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
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1628,6 +1628,32 @@ func TestContextNegotiationWithHTML(t *testing.T) {
|
|||||||
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
assert.Equal(t, "text/html; charset=utf-8", w.Header().Get("Content-Type"))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestContextNegotiationWithPROTOBUF(t *testing.T) {
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
c, _ := CreateTestContext(w)
|
||||||
|
c.Request = httptest.NewRequest(http.MethodPost, "/", nil)
|
||||||
|
|
||||||
|
reps := []int64{int64(1), int64(2)}
|
||||||
|
label := "test"
|
||||||
|
data := &testdata.Test{
|
||||||
|
Label: &label,
|
||||||
|
Reps: reps,
|
||||||
|
}
|
||||||
|
|
||||||
|
c.Negotiate(http.StatusCreated, Negotiate{
|
||||||
|
Offered: []string{MIMEPROTOBUF, MIMEJSON, MIMEXML},
|
||||||
|
Data: data,
|
||||||
|
})
|
||||||
|
|
||||||
|
// Marshal original data for comparison
|
||||||
|
protoData, err := proto.Marshal(data)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusCreated, w.Code)
|
||||||
|
assert.Equal(t, string(protoData), w.Body.String())
|
||||||
|
assert.Equal(t, "application/x-protobuf", w.Header().Get("Content-Type"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestContextNegotiationNotSupport(t *testing.T) {
|
func TestContextNegotiationNotSupport(t *testing.T) {
|
||||||
w := httptest.NewRecorder()
|
w := httptest.NewRecorder()
|
||||||
c, _ := CreateTestContext(w)
|
c, _ := CreateTestContext(w)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user