-
Notifications
You must be signed in to change notification settings - Fork 12
/
notify_test.go
98 lines (83 loc) · 2.68 KB
/
notify_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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package linenotify
import (
"bytes"
"context"
"io"
"io/ioutil"
"net/http"
"strings"
"testing"
"golang.org/x/sync/errgroup"
)
type notifyRoundTripper struct {
resp *http.Response
err error
}
func (t *notifyRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
t.resp.Request = req
return t.resp, t.err
}
func TestClient_Notify(t *testing.T) {
c := NewClient()
statusOK := `{"status":200,"message":"ok"}`
statusUnauthorized := `{"status":401,"message":"invalid access token"}`
tests := []struct {
resp *http.Response
imageThumbnail string
imageFullsize string
image io.Reader
expectedErr error
explain string
}{
{&http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(strings.NewReader(statusOK))}, "", "", nil, nil, "ok: message"},
{&http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(strings.NewReader(statusOK))}, "image.jpg", "image.jpg", nil, nil, "ok: message image url"},
{&http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(strings.NewReader(statusOK))}, "", "", bytes.NewReader([]byte("image file")), nil, "ok: message image"},
{&http.Response{StatusCode: http.StatusUnauthorized, Body: ioutil.NopCloser(strings.NewReader(statusUnauthorized))}, "", "", nil, ErrNotifyInvalidAccessToken, "ng: message"},
}
for _, test := range tests {
c.HTTPClient.Transport = ¬ifyRoundTripper{resp: test.resp}
_, err := c.Notify(context.Background(), "token", "test", test.imageThumbnail, test.imageFullsize, test.image)
if err != test.expectedErr {
t.Errorf("%v err:%v", test.explain, err)
}
}
}
func TestClient_requestBodyWithImage(t *testing.T) {
c := NewClient()
c.HTTPClient.Transport = ¬ifyRoundTripper{
resp: &http.Response{StatusCode: http.StatusOK, Body: ioutil.NopCloser(strings.NewReader(""))},
err: nil,
}
body, contentType, err := c.requestBodyWithImage("test", bytes.NewReader([]byte("image file")))
if err != nil {
t.Fatal(err)
}
buf, err := ioutil.ReadAll(body)
if err != nil {
t.Fatal(err)
}
if !strings.Contains(string(buf), "image file") {
t.Errorf("Expected buffer image file, got %s", string(buf))
}
if !strings.Contains(contentType, "multipart/form-data;") {
t.Errorf("Expected contentType, got %s", contentType)
}
// for data race
eg := &errgroup.Group{}
c.HTTPClient.Transport = ¬ifyRoundTripper{
resp: &http.Response{Body: ioutil.NopCloser(strings.NewReader("image file"))},
err: nil,
}
for i := 0; i < 30; i++ {
eg.Go(func() error {
_, _, err := c.requestBodyWithImage("test", bytes.NewReader([]byte("image binary")))
if err != nil {
return err
}
return nil
})
}
if err := eg.Wait(); err != nil {
t.Fatal(err)
}
}