-
Notifications
You must be signed in to change notification settings - Fork 0
/
matterhook_test.go
82 lines (74 loc) · 1.8 KB
/
matterhook_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
package matterhook
import (
"io/ioutil"
"log"
"net/http"
"net/http/httptest"
"testing"
)
func catchErr(err error) {
if err != nil {
log.Fatalf("error occurred: %s", err)
}
}
func serve(v *string) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
catchErr(err)
*v = string(b)
}))
}
func TestSend(t *testing.T) {
expt := ""
srv := serve(&expt)
tt := []struct {
payload Message
expt string
}{
{
payload: Message{
Text: "hello matterhook",
},
expt: `{"text":"hello matterhook"}`,
},
{
payload: Message{
Text: "Hello matterhook.\nAnother hello matterhook.",
Username: "nafis",
IconEmoji: "none",
Channel: "thanksmessage",
},
expt: `{"text":"Hello matterhook.\nAnother hello matterhook.","username":"nafis","icon_emoji":"none","channel":"thanksmessage"}`,
},
{
payload: Message{
Text: "Message text",
Username: "haname",
IconEmoji: ":papa:",
Channel: "thanksmessage",
Attachments: []Attachment{
{
Text: "Attach Text",
AuthorName: "nafisfaysal",
AuthorLink: "https://www.github.com/nafisfaysal",
Fields: []Field{
{
Title: "Field Title",
Value: "I know",
Short: true,
},
},
},
},
},
expt: `{"text":"Message text","username":"haname","icon_emoji":":papa:","channel":"thanksmessage","attachments":[{"text":"Attach Text","author_name":"nafisfaysal","author_link":"https://www.github.com/nafisfaysal","fields":[{"title":"Field Title","value":"I know","short":true}]}]}`,
},
}
for i, tc := range tt {
err := Send(srv.URL, tc.payload)
catchErr(err)
if expt != tc.expt {
t.Fatalf("#%d: expected = %q, got %q", i+1, tc.expt, expt)
}
}
}