mirror of
https://github.com/labstack/echo.git
synced 2025-01-07 23:01:56 +02:00
🎨 : add some test case for multipart file
This commit is contained in:
parent
d1ab8e8544
commit
fb769d71b5
35
bind.go
35
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)
|
// 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 {
|
for _, fileMap := range files {
|
||||||
fileHeaders, exists := fileMap[inputFieldName]
|
fileHeaders, exists := fileMap[inputFieldName]
|
||||||
if exists {
|
if exists && len(fileHeaders) > 0 {
|
||||||
if structField.Type() == reflect.TypeOf([]*multipart.FileHeader(nil)) {
|
switch structField.Type() {
|
||||||
|
case reflect.TypeOf([]*multipart.FileHeader(nil)):
|
||||||
structField.Set(reflect.ValueOf(fileHeaders))
|
structField.Set(reflect.ValueOf(fileHeaders))
|
||||||
continue
|
continue
|
||||||
} else if structField.Type() == reflect.TypeOf([]multipart.FileHeader(nil)) {
|
case reflect.TypeOf([]multipart.FileHeader(nil)):
|
||||||
var headers []multipart.FileHeader
|
headers := make([]multipart.FileHeader, len(fileHeaders))
|
||||||
for _, fileHeader := range fileHeaders {
|
for i, fileHeader := range fileHeaders {
|
||||||
headers = append(headers, *fileHeader)
|
headers[i] = *fileHeader
|
||||||
}
|
}
|
||||||
structField.Set(reflect.ValueOf(headers))
|
structField.Set(reflect.ValueOf(headers))
|
||||||
continue
|
continue
|
||||||
} else if structField.Type() == reflect.TypeOf(&multipart.FileHeader{}) {
|
case reflect.TypeOf(&multipart.FileHeader{}):
|
||||||
|
structField.Set(reflect.ValueOf(fileHeaders[0]))
|
||||||
if len(fileHeaders) > 0 {
|
|
||||||
structField.Set(reflect.ValueOf(fileHeaders[0]))
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
} else if structField.Type() == reflect.TypeOf(multipart.FileHeader{}) {
|
case reflect.TypeOf(multipart.FileHeader{}):
|
||||||
if len(fileHeaders) > 0 {
|
structField.Set(reflect.ValueOf(*fileHeaders[0]))
|
||||||
structField.Set(reflect.ValueOf(*fileHeaders[0]))
|
|
||||||
}
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -434,3 +430,10 @@ func setFloatField(value string, bitSize int, field reflect.Value) error {
|
|||||||
}
|
}
|
||||||
return err
|
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) {
|
func TestFormMultipartBindOneFile(t *testing.T) {
|
||||||
var args struct {
|
var args struct {
|
||||||
File *multipart.FileHeader `form:"file"`
|
File *multipart.FileHeader `form:"file"`
|
||||||
|
Loading…
Reference in New Issue
Block a user