forked from deoxxa/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhook_test.go
81 lines (64 loc) · 1.91 KB
/
webhook_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
package slack
import (
"net/http"
"net/http/httptest"
"testing"
"time"
)
func TestSendWebhookSuccessful(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(sendWebhookHandlerOK))
defer server.Close()
testClient := NewWebhookClient(server.URL, 250*time.Millisecond)
err := testClient.SendMessage(&Payload{Text: testText})
if err != nil {
t.Error(err)
}
}
func sendWebhookHandlerOK(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte("ok"))
}
func TestSendWebhookdMissingArgument(t *testing.T) {
t.Parallel()
testClient := NewWebhookClient("http://www.example.com", 250*time.Millisecond)
err := testClient.SendMessage(nil)
if err == nil {
t.Error("should have failed, payload missing")
}
}
func TestSendWebhookBadResponse(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(sendWebhookHandlerBad))
defer server.Close()
slackAddr = server.URL
testClient := NewWebhookClient(server.URL, 250*time.Millisecond)
err := testClient.SendMessage(&Payload{Text: testText})
if err == nil {
t.Error("should have failed, invalid channel")
}
if err.Error() != "some_error" {
t.Error("received unexpected error")
}
}
func sendWebhookHandlerBad(rw http.ResponseWriter, req *http.Request) {
rw.Write([]byte("some_error"))
}
func TestWebhookMessageTimeout(t *testing.T) {
t.Parallel()
server := httptest.NewServer(http.HandlerFunc(sendMessageHandlerWait))
defer server.Close()
slackAddr = server.URL
testClient := NewWebhookClient(server.URL, 250*time.Millisecond)
err := testClient.SendMessage(&Payload{Text: testText})
if err == nil {
t.Error("should have failed, timeout")
}
}
func TestIt(t *testing.T) {
t.Parallel()
testClient := NewWebhookClient("https://hooks.slack.com/services/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", DefaultTimeout)
err := testClient.SendMessage(&Payload{Text: "This is a test"})
if err != nil {
t.Error(err)
}
}