diff --git a/binding/multipart_form_mapping.go b/binding/multipart_form_mapping.go index 1b2ad001..c830359e 100644 --- a/binding/multipart_form_mapping.go +++ b/binding/multipart_form_mapping.go @@ -15,6 +15,7 @@ import ( type multipartRequest http.Request var _ setter = (*multipartRequest)(nil) +var ConstructionFailure = false // TrySet tries to set a value by the multipart request with the binding a form file func (r *multipartRequest) TrySet(value reflect.Value, field reflect.StructField, key string, opt setOptions) (isSetted bool, err error) { @@ -48,6 +49,11 @@ func setByMultipartFormFile(value reflect.Value, field reflect.StructField, file } defer fd.Close() c, err := ioutil.ReadAll(fd) + + if ConstructionFailure { + err = errors.New("test use") + } + if err != nil { return false, err } diff --git a/binding/multipart_form_mapping_test.go b/binding/multipart_form_mapping_test.go index 1888862f..08c9fc9e 100644 --- a/binding/multipart_form_mapping_test.go +++ b/binding/multipart_form_mapping_test.go @@ -32,6 +32,24 @@ func TestFormMultipartBindingOneFileToBytesFail1(t *testing.T) { assert.Error(t, err) } +func TestFormMultipartBindingOneFileToBytesFail2(t *testing.T) { + var test struct { + Voice []byte `form:"voice"` + } + + file := testFile{"voice", "test.pcm", []byte("pcm pcm pcm")} + req := createRequestMultipartFiles(t, file) + + ConstructionFailure = true + + err := req.ParseMultipartForm(3) + assert.NoError(t, err) + + err = mappingByPtr(&test, (*multipartRequest)(req), "form") + assert.Error(t, err) + ConstructionFailure = false +} + func TestFormMultipartBindingOneFileToBytesArray(t *testing.T) { var test struct { Voice []byte `form:"voice"`