2022-05-28 05:42:28 +03:00
|
|
|
// Copyright 2019 Gin Core Team. All rights reserved.
|
2019-06-18 14:49:10 +03:00
|
|
|
// Use of this source code is governed by a MIT style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package binding
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2022-11-17 17:35:55 +03:00
|
|
|
"io"
|
2019-06-18 14:49:10 +03:00
|
|
|
"mime/multipart"
|
|
|
|
"net/http"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2024-07-14 15:33:08 +03:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-06-18 14:49:10 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestFormMultipartBindingBindOneFile(t *testing.T) {
|
|
|
|
var s struct {
|
|
|
|
FileValue multipart.FileHeader `form:"file"`
|
|
|
|
FilePtr *multipart.FileHeader `form:"file"`
|
|
|
|
SliceValues []multipart.FileHeader `form:"file"`
|
|
|
|
SlicePtrs []*multipart.FileHeader `form:"file"`
|
|
|
|
ArrayValues [1]multipart.FileHeader `form:"file"`
|
|
|
|
ArrayPtrs [1]*multipart.FileHeader `form:"file"`
|
|
|
|
}
|
|
|
|
file := testFile{"file", "file1", []byte("hello")}
|
|
|
|
|
|
|
|
req := createRequestMultipartFiles(t, file)
|
|
|
|
err := FormMultipart.Bind(req, &s)
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
|
|
|
|
assertMultipartFileHeader(t, &s.FileValue, file)
|
|
|
|
assertMultipartFileHeader(t, s.FilePtr, file)
|
|
|
|
assert.Len(t, s.SliceValues, 1)
|
|
|
|
assertMultipartFileHeader(t, &s.SliceValues[0], file)
|
|
|
|
assert.Len(t, s.SlicePtrs, 1)
|
|
|
|
assertMultipartFileHeader(t, s.SlicePtrs[0], file)
|
|
|
|
assertMultipartFileHeader(t, &s.ArrayValues[0], file)
|
|
|
|
assertMultipartFileHeader(t, s.ArrayPtrs[0], file)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormMultipartBindingBindTwoFiles(t *testing.T) {
|
|
|
|
var s struct {
|
|
|
|
SliceValues []multipart.FileHeader `form:"file"`
|
|
|
|
SlicePtrs []*multipart.FileHeader `form:"file"`
|
|
|
|
ArrayValues [2]multipart.FileHeader `form:"file"`
|
|
|
|
ArrayPtrs [2]*multipart.FileHeader `form:"file"`
|
|
|
|
}
|
|
|
|
files := []testFile{
|
|
|
|
{"file", "file1", []byte("hello")},
|
|
|
|
{"file", "file2", []byte("world")},
|
|
|
|
}
|
|
|
|
|
|
|
|
req := createRequestMultipartFiles(t, files...)
|
|
|
|
err := FormMultipart.Bind(req, &s)
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
|
|
|
|
assert.Len(t, s.SliceValues, len(files))
|
|
|
|
assert.Len(t, s.SlicePtrs, len(files))
|
|
|
|
assert.Len(t, s.ArrayValues, len(files))
|
|
|
|
assert.Len(t, s.ArrayPtrs, len(files))
|
|
|
|
|
|
|
|
for i, file := range files {
|
|
|
|
assertMultipartFileHeader(t, &s.SliceValues[i], file)
|
|
|
|
assertMultipartFileHeader(t, s.SlicePtrs[i], file)
|
|
|
|
assertMultipartFileHeader(t, &s.ArrayValues[i], file)
|
|
|
|
assertMultipartFileHeader(t, s.ArrayPtrs[i], file)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFormMultipartBindingBindError(t *testing.T) {
|
|
|
|
files := []testFile{
|
|
|
|
{"file", "file1", []byte("hello")},
|
|
|
|
{"file", "file2", []byte("world")},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tt := range []struct {
|
|
|
|
name string
|
2022-03-21 04:43:17 +03:00
|
|
|
s any
|
2019-06-18 14:49:10 +03:00
|
|
|
}{
|
|
|
|
{"wrong type", &struct {
|
|
|
|
Files int `form:"file"`
|
|
|
|
}{}},
|
|
|
|
{"wrong array size", &struct {
|
|
|
|
Files [1]*multipart.FileHeader `form:"file"`
|
|
|
|
}{}},
|
|
|
|
{"wrong slice type", &struct {
|
|
|
|
Files []int `form:"file"`
|
|
|
|
}{}},
|
|
|
|
} {
|
|
|
|
req := createRequestMultipartFiles(t, files...)
|
|
|
|
err := FormMultipart.Bind(req, tt.s)
|
2024-07-14 15:33:08 +03:00
|
|
|
require.Error(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type testFile struct {
|
|
|
|
Fieldname string
|
|
|
|
Filename string
|
|
|
|
Content []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
func createRequestMultipartFiles(t *testing.T, files ...testFile) *http.Request {
|
|
|
|
var body bytes.Buffer
|
|
|
|
|
|
|
|
mw := multipart.NewWriter(&body)
|
|
|
|
for _, file := range files {
|
|
|
|
fw, err := mw.CreateFormFile(file.Fieldname, file.Filename)
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
|
|
|
|
n, err := fw.Write(file.Content)
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
assert.Equal(t, len(file.Content), n)
|
|
|
|
}
|
|
|
|
err := mw.Close()
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/", &body)
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
|
|
|
|
req.Header.Set("Content-Type", MIMEMultipartPOSTForm+"; boundary="+mw.Boundary())
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
func assertMultipartFileHeader(t *testing.T, fh *multipart.FileHeader, file testFile) {
|
|
|
|
assert.Equal(t, file.Filename, fh.Filename)
|
2021-05-23 06:44:41 +03:00
|
|
|
assert.Equal(t, int64(len(file.Content)), fh.Size)
|
2019-06-18 14:49:10 +03:00
|
|
|
|
|
|
|
fl, err := fh.Open()
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
|
2022-11-17 17:35:55 +03:00
|
|
|
body, err := io.ReadAll(fl)
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
assert.Equal(t, string(file.Content), string(body))
|
|
|
|
|
|
|
|
err = fl.Close()
|
2024-07-14 15:33:08 +03:00
|
|
|
require.NoError(t, err)
|
2019-06-18 14:49:10 +03:00
|
|
|
}
|