add test function

This commit is contained in:
guonaihong 2019-08-19 12:58:29 +08:00
parent 27a8f77311
commit 24943ac009
2 changed files with 24 additions and 0 deletions

View File

@ -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
}

View File

@ -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"`