forked from ivpusic/httpcheck
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tester_multipart.go
67 lines (59 loc) · 1.6 KB
/
tester_multipart.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package httpcheck
import (
"bytes"
"fmt"
"mime/multipart"
"os"
"github.com/stretchr/testify/require"
)
// Parter is the interface that create a multipart part.
type Parter interface {
Part(mw *multipart.Writer) error
}
// FieldPart represents a multipart part of the field type part.
type FieldPart struct {
FieldName string
Value string
}
// Part returns a multipart part.
func (p FieldPart) Part(mw *multipart.Writer) error {
w, err := mw.CreateFormField(p.FieldName)
if err != nil {
return fmt.Errorf("failed to creat form field: %w", err)
}
if _, err := w.Write([]byte(p.Value)); err != nil {
return fmt.Errorf("write error: %w", err)
}
return nil
}
// FilePart represents a multipart part of the file type part.
type FilePart struct {
FieldName string
FileName string
}
// Part returns a multipart part.
func (p FilePart) Part(mw *multipart.Writer) error {
w, err := mw.CreateFormFile(p.FieldName, p.FileName)
if err != nil {
return fmt.Errorf("failed to create form file: %w", err)
}
b, err := os.ReadFile(p.FileName)
if err != nil {
return fmt.Errorf("failed to open file: %w", err)
}
if _, err := w.Write(b); err != nil {
return fmt.Errorf("failed to write: %w", err)
}
return nil
}
// WithMultipart add a multipart data to the body.
func (tt *Tester) WithMultipart(part Parter, parts ...Parter) *Tester {
var b bytes.Buffer
mw := multipart.NewWriter(&b)
require.NoError(tt.t, part.Part(mw))
for _, v := range parts {
require.NoError(tt.t, v.Part(mw))
}
require.NoError(tt.t, mw.Close())
return tt.WithHeader("Content-Type", mw.FormDataContentType()).WithBody(b.Bytes())
}