-
Notifications
You must be signed in to change notification settings - Fork 0
/
formdata_test.go
49 lines (40 loc) · 1.02 KB
/
formdata_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
package formdata
import (
"bytes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"io"
"log"
"net/http"
"os"
"testing"
)
type MockedClient struct {
mock.Mock
HttpClientable
buffer *bytes.Buffer
}
func (m MockedClient) Do(req *http.Request) (*http.Response, error) {
_, err := io.Copy(m.buffer, req.Body)
return nil, err
}
func Test_Sample(t *testing.T) {
client := &MockedClient{
buffer: &bytes.Buffer{},
}
formData := NewFormData()
formData.AddDataField("aaaa", "text/plain", []byte("hello world"))
a, _ := os.OpenFile("test/sample-1.txt", os.O_RDONLY, 0)
formData.AddFileField("files[]", "application/octet-stream", a)
b, _ := os.OpenFile("test/sample-2.txt", os.O_RDONLY, 0)
formData.AddFileField("files[]", "application/octet-stream", b)
req, err := http.NewRequest("POST", "test://dummy", formData)
if err != nil {
log.Fatalln(err)
}
_, err = formData.Do(client, req)
if err != nil {
t.Fatal(err)
}
assert.Equal(t, len(client.buffer.Bytes()), int(req.ContentLength))
}