-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhooks_test.go
136 lines (118 loc) · 3.24 KB
/
hooks_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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package main
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"reflect"
"testing"
"time"
"github.com/YaleSpinup/reaper/common"
)
var (
testHookToken = "xypdq"
testHookClient = NewMockClient([]byte("ok"), 200)
)
func TestNewWebhook(t *testing.T) {
testWebhook := Webhook{
Endpoint: "http://127.0.0.1/v1/hook",
Token: "xypdq",
Method: "POST",
Actions: []string{"party"},
Client: testHookClient,
}
actual, err := NewWebhook(common.Webhook{
Endpoint: "http://127.0.0.1/v1/hook",
Token: "xypdq",
Method: "POST",
Actions: []string{"party"},
})
actual.Client = testHookClient
if err != nil {
t.Errorf("got error %s, expected nil", err)
}
if !reflect.DeepEqual(testWebhook, actual) {
t.Errorf("Expected NewWebhook to return %+v, got %+v", testWebhook, actual)
}
}
func TestSendWebhook(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
event := Event{}
switch r.Method {
case http.MethodGet, http.MethodHead, http.MethodDelete, http.MethodOptions:
q := r.URL.Query()
event.ID = q.Get("id")
event.Action = q.Get("action")
case http.MethodPost, http.MethodPut, http.MethodPatch:
err := json.NewDecoder(r.Body).Decode(&event)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("unable to decode JSON request: " + err.Error()))
return
}
if ct := r.Header.Get("Content-Type"); ct != "application/json" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("missing content-type header '" + ct + "'"))
return
}
default:
w.WriteHeader(http.StatusMethodNotAllowed)
w.Write([]byte("unexpected method: " + r.Method))
return
}
t.Logf("received %s event from webhook: %+v", r.Method, event)
if event.ID != "i-123456789" {
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("bad id: " + event.ID + ", expected 'i-123456789'"))
return
}
}))
defer server.Close()
wh := Webhook{
Endpoint: server.URL,
Token: testHookToken,
Method: http.MethodPost,
Actions: []string{"party"},
Client: &http.Client{
Timeout: time.Second * 3,
},
}
// other action should just not send the hook
wh.Method = http.MethodPost
if err := wh.Send(context.TODO(), &Event{
Action: "dance",
ID: "i-123456789",
}); err != nil {
t.Errorf("expected error for bad method, got nil")
}
// post-like should expect a body
for _, m := range []string{http.MethodPost, http.MethodPut, http.MethodPatch} {
wh.Method = m
if err := wh.Send(context.TODO(), &Event{
Action: "party",
ID: "i-123456789",
}); err != nil {
t.Errorf("expected nil error for valid hook, got %s", err)
}
}
// get-like should expect a url
for _, m := range []string{http.MethodGet, http.MethodHead} {
wh.Method = m
if err := wh.Send(context.TODO(), &Event{
Action: "party",
ID: "i-123456789",
}); err != nil {
t.Errorf("expected nil error for valid hook, got %s", err)
}
}
// invalid methods should throw an error
for _, m := range []string{http.MethodTrace, http.MethodConnect} {
wh.Method = m
if err := wh.Send(context.TODO(), &Event{
Action: "party",
ID: "i-123456789",
}); err == nil {
t.Errorf("expected error for bad method, got nil")
}
}
}