mirror of
https://github.com/gin-gonic/gin.git
synced 2025-10-16 05:16:35 +08:00
Merge 24943ac009a7cd372bbd3c9eb586b66c94f8368f into a481ee2897af1e368de5c919fbeb21b89aa26fc7
This commit is contained in:
commit
a482779382
@ -175,6 +175,15 @@ func setByForm(value reflect.Value, field reflect.StructField, form map[string][
|
|||||||
if !ok {
|
if !ok {
|
||||||
vs = []string{opt.defaultValue}
|
vs = []string{opt.defaultValue}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch value.Interface().(type) {
|
||||||
|
case []byte:
|
||||||
|
if len(vs) > 0 {
|
||||||
|
value.Set(reflect.ValueOf([]byte(vs[0])))
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return true, setSlice(vs, value, field)
|
return true, setSlice(vs, value, field)
|
||||||
case reflect.Array:
|
case reflect.Array:
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -269,6 +269,15 @@ func TestMappingStructField(t *testing.T) {
|
|||||||
assert.Equal(t, 9, s.J.I)
|
assert.Equal(t, 9, s.J.I)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestByteArray(t *testing.T) {
|
||||||
|
var s struct {
|
||||||
|
B []byte
|
||||||
|
}
|
||||||
|
err := mappingByPtr(&s, formSource{"B": {"hello"}}, "form")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, []byte("hello"), s.B)
|
||||||
|
}
|
||||||
|
|
||||||
func TestMappingMapField(t *testing.T) {
|
func TestMappingMapField(t *testing.T) {
|
||||||
var s struct {
|
var s struct {
|
||||||
M map[string]int
|
M map[string]int
|
||||||
|
@ -6,6 +6,7 @@ package binding
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
"io/ioutil"
|
||||||
"mime/multipart"
|
"mime/multipart"
|
||||||
"net/http"
|
"net/http"
|
||||||
"reflect"
|
"reflect"
|
||||||
@ -14,6 +15,7 @@ import (
|
|||||||
type multipartRequest http.Request
|
type multipartRequest http.Request
|
||||||
|
|
||||||
var _ setter = (*multipartRequest)(nil)
|
var _ setter = (*multipartRequest)(nil)
|
||||||
|
var ConstructionFailure = false
|
||||||
|
|
||||||
var (
|
var (
|
||||||
// ErrMultiFileHeader multipart.FileHeader invalid
|
// ErrMultiFileHeader multipart.FileHeader invalid
|
||||||
@ -47,6 +49,27 @@ func setByMultipartFormFile(value reflect.Value, field reflect.StructField, file
|
|||||||
return true, nil
|
return true, nil
|
||||||
}
|
}
|
||||||
case reflect.Slice:
|
case reflect.Slice:
|
||||||
|
switch value.Interface().(type) {
|
||||||
|
case []byte:
|
||||||
|
fd, err := files[0].Open()
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
defer fd.Close()
|
||||||
|
c, err := ioutil.ReadAll(fd)
|
||||||
|
|
||||||
|
if ConstructionFailure {
|
||||||
|
err = errors.New("test use")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
|
||||||
|
value.Set(reflect.ValueOf(c))
|
||||||
|
return true, nil
|
||||||
|
}
|
||||||
|
|
||||||
slice := reflect.MakeSlice(value.Type(), len(files), len(files))
|
slice := reflect.MakeSlice(value.Type(), len(files), len(files))
|
||||||
isSet, err = setArrayOfMultipartFormFiles(slice, field, files)
|
isSet, err = setArrayOfMultipartFormFiles(slice, field, files)
|
||||||
if err != nil || !isSet {
|
if err != nil || !isSet {
|
||||||
|
@ -14,6 +14,55 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestFormMultipartBindingOneFileToBytesFail1(t *testing.T) {
|
||||||
|
var test struct {
|
||||||
|
Voice []byte `form:"voice"`
|
||||||
|
}
|
||||||
|
|
||||||
|
file := testFile{"voice", "test.pcm", []byte("pcm pcm pcm")}
|
||||||
|
req := createRequestMultipartFiles(t, file)
|
||||||
|
|
||||||
|
err := req.ParseMultipartForm(3)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
err = req.MultipartForm.RemoveAll()
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
err = mappingByPtr(&test, (*multipartRequest)(req), "form")
|
||||||
|
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"`
|
||||||
|
}
|
||||||
|
|
||||||
|
file := testFile{"voice", "test.pcm", []byte("pcm pcm pcm")}
|
||||||
|
req := createRequestMultipartFiles(t, file)
|
||||||
|
err := FormMultipart.Bind(req, &test)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
|
||||||
|
assert.Equal(t, test.Voice, []byte("pcm pcm pcm"))
|
||||||
|
}
|
||||||
|
|
||||||
func TestFormMultipartBindingBindOneFile(t *testing.T) {
|
func TestFormMultipartBindingBindOneFile(t *testing.T) {
|
||||||
var s struct {
|
var s struct {
|
||||||
FileValue multipart.FileHeader `form:"file"`
|
FileValue multipart.FileHeader `form:"file"`
|
||||||
|
Loading…
x
Reference in New Issue
Block a user