mirror of
https://github.com/labstack/echo.git
synced 2024-12-22 20:06:21 +02:00
🎨 : add some test case for multipart file
This commit is contained in:
parent
d1ab8e8544
commit
fb769d71b5
31
bind.go
31
bind.go
@ -219,30 +219,26 @@ func (b *DefaultBinder) bindData(destination interface{}, data map[string][]stri
|
||||
}
|
||||
|
||||
// Handle multiple file uploads ([]*multipart.FileHeader, *multipart.FileHeader, []multipart.FileHeader)
|
||||
if len(files) > 0 {
|
||||
if len(files) > 0 && isMultipartFile(structField.Type()) {
|
||||
for _, fileMap := range files {
|
||||
fileHeaders, exists := fileMap[inputFieldName]
|
||||
if exists {
|
||||
if structField.Type() == reflect.TypeOf([]*multipart.FileHeader(nil)) {
|
||||
if exists && len(fileHeaders) > 0 {
|
||||
switch structField.Type() {
|
||||
case reflect.TypeOf([]*multipart.FileHeader(nil)):
|
||||
structField.Set(reflect.ValueOf(fileHeaders))
|
||||
continue
|
||||
} else if structField.Type() == reflect.TypeOf([]multipart.FileHeader(nil)) {
|
||||
var headers []multipart.FileHeader
|
||||
for _, fileHeader := range fileHeaders {
|
||||
headers = append(headers, *fileHeader)
|
||||
case reflect.TypeOf([]multipart.FileHeader(nil)):
|
||||
headers := make([]multipart.FileHeader, len(fileHeaders))
|
||||
for i, fileHeader := range fileHeaders {
|
||||
headers[i] = *fileHeader
|
||||
}
|
||||
structField.Set(reflect.ValueOf(headers))
|
||||
continue
|
||||
} else if structField.Type() == reflect.TypeOf(&multipart.FileHeader{}) {
|
||||
|
||||
if len(fileHeaders) > 0 {
|
||||
case reflect.TypeOf(&multipart.FileHeader{}):
|
||||
structField.Set(reflect.ValueOf(fileHeaders[0]))
|
||||
}
|
||||
continue
|
||||
} else if structField.Type() == reflect.TypeOf(multipart.FileHeader{}) {
|
||||
if len(fileHeaders) > 0 {
|
||||
case reflect.TypeOf(multipart.FileHeader{}):
|
||||
structField.Set(reflect.ValueOf(*fileHeaders[0]))
|
||||
}
|
||||
continue
|
||||
}
|
||||
}
|
||||
@ -434,3 +430,10 @@ func setFloatField(value string, bitSize int, field reflect.Value) error {
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func isMultipartFile(field reflect.Type) bool {
|
||||
return reflect.TypeOf(&multipart.FileHeader{}) == field ||
|
||||
reflect.TypeOf(multipart.FileHeader{}) == field ||
|
||||
reflect.TypeOf([]*multipart.FileHeader(nil)) == field ||
|
||||
reflect.TypeOf([]multipart.FileHeader(nil)) == field
|
||||
}
|
||||
|
40
bind_test.go
40
bind_test.go
@ -1177,6 +1177,46 @@ func TestFormMultipartBindTwoFiles(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormMultipartBindMultipleKeys(t *testing.T) {
|
||||
var args struct {
|
||||
Files []multipart.FileHeader `form:"files"`
|
||||
File multipart.FileHeader `form:"file"`
|
||||
}
|
||||
|
||||
files := []testFile{
|
||||
{
|
||||
Fieldname: "files",
|
||||
Filename: "file1.txt",
|
||||
Content: []byte("This is the content of file 1."),
|
||||
},
|
||||
{
|
||||
Fieldname: "files",
|
||||
Filename: "file2.txt",
|
||||
Content: []byte("This is the content of file 2."),
|
||||
},
|
||||
}
|
||||
file := testFile{
|
||||
Fieldname: "file",
|
||||
Filename: "file3.txt",
|
||||
Content: []byte("This is the content of file 3."),
|
||||
}
|
||||
|
||||
e := New()
|
||||
req := createRequestMultipartFiles(t, append(files, file)...)
|
||||
rec := httptest.NewRecorder()
|
||||
c := e.NewContext(req, rec)
|
||||
|
||||
err := c.Bind(&args)
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Len(t, args.Files, len(files))
|
||||
for idx, file := range files {
|
||||
argsFile := args.Files[idx]
|
||||
assertMultipartFileHeader(t, &argsFile, file)
|
||||
}
|
||||
assertMultipartFileHeader(t, &args.File, file)
|
||||
}
|
||||
|
||||
func TestFormMultipartBindOneFile(t *testing.T) {
|
||||
var args struct {
|
||||
File *multipart.FileHeader `form:"file"`
|
||||
|
Loading…
Reference in New Issue
Block a user