forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
webhooks_test.go
65 lines (49 loc) · 1.3 KB
/
webhooks_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
package slack
import (
"encoding/json"
"net/http"
"reflect"
"testing"
)
func TestPostWebhook_OK(t *testing.T) {
once.Do(startServer)
var receivedPayload WebhookMessage
http.HandleFunc("/webhook", func(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
decoder := json.NewDecoder(r.Body)
err := decoder.Decode(&receivedPayload)
if err != nil {
t.Errorf("Request contained invalid JSON, %s", err)
}
response := []byte(`{}`)
rw.Write(response)
})
url := "http://" + serverAddr + "/webhook"
payload := &WebhookMessage{
Text: "Test Text",
Attachments: []Attachment{
{
Text: "Foo",
},
},
}
err := PostWebhook(url, payload)
if err != nil {
t.Errorf("Expected not to receive error: %s", err)
}
if !reflect.DeepEqual(payload, &receivedPayload) {
t.Errorf("Payload did not match\nwant: %#v\n got: %#v", payload, receivedPayload)
}
}
func TestPostWebhook_NotOK(t *testing.T) {
once.Do(startServer)
http.HandleFunc("/webhook2", func(rw http.ResponseWriter, r *http.Request) {
rw.WriteHeader(http.StatusInternalServerError)
rw.Write([]byte("500 - Something bad happened!"))
})
url := "http://" + serverAddr + "/webhook2"
err := PostWebhook(url, &WebhookMessage{})
if err == nil {
t.Errorf("Expected to receive error")
}
}