Modify bind() method that users do not pass "Content Type"

This commit is contained in:
byene0923 2021-04-18 23:59:14 +08:00
parent 8b92ed6504
commit 89c2b85471
2 changed files with 19 additions and 7 deletions

View File

@ -151,12 +151,6 @@ type ControllerInterface interface {
CheckXSRFCookie() bool
HandlerFunc(fn string) bool
URLMapping()
Bind(obj interface{}) error
BindJson(obj interface{}) error
BindXML(obj interface{}) error
BindForm(obj interface{}) error
BindProtobuf(obj interface{}) error
BindYAML(obj interface{}) error
}
// Init generates default values of controller operations.
@ -250,7 +244,10 @@ func (c *Controller) HandlerFunc(fnname string) bool {
func (c *Controller) URLMapping() {}
func (c *Controller) Bind(obj interface{}) error {
ct := c.Ctx.Request.Header["Content-Type"]
ct, exist := c.Ctx.Request.Header["Content-Type"]
if exist == false || len(ct) == 0 {
return c.BindJson(obj)
}
i, l := 0, len(ct[0])
for ; i < l && ct[0][i] != ';'; i++ {
}

View File

@ -198,6 +198,21 @@ func TestBindJson(t *testing.T) {
assert.Equal(t, "FOO", s.Foo)
}
func TestBindNoContentType(t *testing.T) {
var s struct {
Foo string `json:"foo"`
}
header := map[string][]string{}
request := &http.Request{Header: header}
input := &context.BeegoInput{RequestBody: []byte(`{"foo": "FOO"}`)}
ctx := &context.Context{Request: request, Input: input}
ctrlr := Controller{Ctx: ctx}
err := ctrlr.Bind(&s)
require.NoError(t, err)
assert.Equal(t, "FOO", s.Foo)
}
func TestBindXML(t *testing.T) {
var s struct {