-
Notifications
You must be signed in to change notification settings - Fork 6
/
webhook_client_test.go
80 lines (66 loc) · 1.72 KB
/
webhook_client_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
package bearychat
import (
"net/http"
"testing"
)
const (
testWebhook = "http://localhost:3927"
)
func TestWebhookResponse_IsOk(t *testing.T) {
var resp WebhookResponse
resp = WebhookResponse{Code: 0}
if !resp.IsOk() {
t.Errorf("response should be ok when code is 0")
}
resp = WebhookResponse{Code: 1}
if resp.IsOk() {
t.Errorf("response should not be ok when code is not 0")
}
}
func TestIncomingWebhookClient_SetWebhook(t *testing.T) {
h := NewIncomingWebhookClient("")
if h.SetWebhook(testWebhook) == nil {
t.Errorf("should return webhook client")
}
if h.Webhook != testWebhook {
t.Errorf("should set webhook")
}
}
func TestIncomingWebhookClient_SetHTTPClient(t *testing.T) {
h := NewIncomingWebhookClient(testWebhook)
if h.httpClient != http.DefaultClient {
t.Errorf("should use `http.DefaultClient` by default")
}
testHTTPClient := &http.Client{}
if h.SetHTTPClient(testHTTPClient) == nil {
t.Errorf("should return webhook client")
}
if h.httpClient != testHTTPClient {
t.Errorf("should set http client")
}
}
func TestIncomingWebhookClient_Send_WithoutWebhook(t *testing.T) {
h := NewIncomingWebhookClient("")
_, err := h.Send(nil)
if err == nil {
t.Errorf("should not send when webhook is not set")
}
}
func TestIncomingWebhookClient_Send_WithoutHTTPClient(t *testing.T) {
h := NewIncomingWebhookClient(testWebhook)
h.SetHTTPClient(nil)
_, err := h.Send(nil)
if err == nil {
t.Errorf("should not send when http client is not set")
}
}
func ExampleNewIncomingWebhookClient() {
m := Incoming{Text: "Hello, BearyChat"}
payload, _ := m.Build()
resp, _ := NewIncomingWebhookClient("YOUR WEBHOOK URL").Send(payload)
if resp.IsOk() {
// parse resp result
} else {
// parse resp error
}
}