handle nil body for JSON binding

This commit is contained in:
mllu 2018-11-10 23:44:49 -08:00
parent 37854ee10f
commit be38d2880d
2 changed files with 11 additions and 0 deletions

View File

@ -195,6 +195,13 @@ func TestBindingDefault(t *testing.T) {
assert.Equal(t, YAML, Default("PUT", MIMEYAML))
}
func TestBindingJSONNilBody(t *testing.T) {
var obj FooStruct
req, _ := http.NewRequest(http.MethodPost, "/", nil)
err := JSON.Bind(req, &obj)
assert.Error(t, err)
}
func TestBindingJSON(t *testing.T) {
testBodyBinding(t,
JSON, "json",

View File

@ -6,6 +6,7 @@ package binding
import (
"bytes"
"fmt"
"io"
"net/http"
@ -24,6 +25,9 @@ func (jsonBinding) Name() string {
}
func (jsonBinding) Bind(req *http.Request, obj interface{}) error {
if req == nil || req.Body == nil {
return fmt.Errorf("invalid request")
}
return decodeJSON(req.Body, obj)
}