forked from ivpusic/httpcheck
-
Notifications
You must be signed in to change notification settings - Fork 3
/
tester_multipart_test.go
54 lines (51 loc) · 1.37 KB
/
tester_multipart_test.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
package httpcheck
import (
"io"
"mime"
"mime/multipart"
"net/http"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestTester_WithMultipart(t *testing.T) {
mux := http.NewServeMux()
mux.HandleFunc("/multipart", func(w http.ResponseWriter, r *http.Request) {
mediaType, params, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
require.NoError(t, err)
assert.Equal(t, "multipart/form-data", mediaType)
if strings.HasPrefix(mediaType, "multipart/") {
mr := multipart.NewReader(r.Body, params["boundary"])
for {
p, err := mr.NextPart()
if err != nil {
assert.ErrorIs(t, err, io.EOF)
break
}
got, err := io.ReadAll(p)
require.NoError(t, err)
switch n := p.FormName(); n {
case "part_1":
assert.Equal(t, "part_1", p.FormName())
assert.Equal(t, "value_1", string(got))
case "part_2":
assert.Equal(t, "neko_small.png", p.FileName())
b, err := os.ReadFile("./testdata/neko_small.png")
require.NoError(t, err)
assert.Equal(t, b, got)
}
p.Close()
}
}
w.WriteHeader(http.StatusOK)
})
checker := New(mux)
checker.Test(t, "POST", "/multipart").
WithMultipart(
FieldPart{FieldName: "part_1", Value: "value_1"},
FilePart{FieldName: "part_2", FileName: "./testdata/neko_small.png"},
).
Check().HasStatus(http.StatusOK)
}