Modify bind() method that users do not pass "Content Type"
This commit is contained in:
parent
8b92ed6504
commit
89c2b85471
@ -151,12 +151,6 @@ type ControllerInterface interface {
|
|||||||
CheckXSRFCookie() bool
|
CheckXSRFCookie() bool
|
||||||
HandlerFunc(fn string) bool
|
HandlerFunc(fn string) bool
|
||||||
URLMapping()
|
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.
|
// 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) URLMapping() {}
|
||||||
|
|
||||||
func (c *Controller) Bind(obj interface{}) error {
|
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])
|
i, l := 0, len(ct[0])
|
||||||
for ; i < l && ct[0][i] != ';'; i++ {
|
for ; i < l && ct[0][i] != ';'; i++ {
|
||||||
}
|
}
|
||||||
|
|||||||
@ -198,6 +198,21 @@ func TestBindJson(t *testing.T) {
|
|||||||
assert.Equal(t, "FOO", s.Foo)
|
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) {
|
func TestBindXML(t *testing.T) {
|
||||||
|
|
||||||
var s struct {
|
var s struct {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user