2022-07-06 23:19:05 +02:00
|
|
|
package tests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"io"
|
|
|
|
"mime/multipart"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MockMultipartData creates a mocked multipart/form-data payload.
|
|
|
|
//
|
|
|
|
// Example
|
2023-02-23 21:51:42 +02:00
|
|
|
//
|
2022-07-06 23:19:05 +02:00
|
|
|
// data, mp, err := tests.MockMultipartData(
|
|
|
|
// map[string]string{"title": "new"},
|
|
|
|
// "file1",
|
|
|
|
// "file2",
|
|
|
|
// ...
|
|
|
|
// )
|
|
|
|
func MockMultipartData(data map[string]string, fileFields ...string) (*bytes.Buffer, *multipart.Writer, error) {
|
|
|
|
body := new(bytes.Buffer)
|
|
|
|
mp := multipart.NewWriter(body)
|
|
|
|
defer mp.Close()
|
|
|
|
|
|
|
|
// write data fields
|
|
|
|
for k, v := range data {
|
|
|
|
mp.WriteField(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
// write file fields
|
|
|
|
for _, fileField := range fileFields {
|
|
|
|
// create a test temporary file
|
2022-12-11 01:01:15 +02:00
|
|
|
err := func() error {
|
|
|
|
tmpFile, err := os.CreateTemp(os.TempDir(), "tmpfile-*.txt")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-07-06 23:19:05 +02:00
|
|
|
|
2022-12-11 01:01:15 +02:00
|
|
|
if _, err := tmpFile.Write([]byte("test")); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tmpFile.Seek(0, 0)
|
|
|
|
defer tmpFile.Close()
|
|
|
|
defer os.Remove(tmpFile.Name())
|
|
|
|
|
|
|
|
// stub uploaded file
|
|
|
|
w, err := mp.CreateFormFile(fileField, tmpFile.Name())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(w, tmpFile); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}()
|
2022-07-06 23:19:05 +02:00
|
|
|
if err != nil {
|
2022-12-11 01:01:15 +02:00
|
|
|
return nil, nil, err
|
2022-07-06 23:19:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return body, mp, nil
|
|
|
|
}
|