-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwebhooks_test.go
99 lines (83 loc) · 2.6 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
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
package devtogo
import (
"encoding/json"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/require"
)
func TestWebhooks(t *testing.T) {
var res Webhooks
b := unmarshalGoldenFileBytes(t, "webhooks.json", &res)
tests := []struct {
name string
arguments Arguments
expectedQueryParams string
}{
{"No params", Defaults(), ""},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/webhooks", r.URL.String())
w.WriteHeader(http.StatusOK)
w.Write(b)
}))
client := NewClient(withBaseURL(ts.URL))
articles, err := client.Webhooks()
require.NoError(t, err)
require.Equal(t, res, articles)
})
}
}
func TestWebhook(t *testing.T) {
var res Webhook
b := unmarshalGoldenFileBytes(t, "webhook.json", &res)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/webhooks/167919", r.URL.Path)
w.WriteHeader(http.StatusOK)
w.Write(b)
}))
client := NewClient(withBaseURL(ts.URL))
article, err := client.Webhook(167919)
require.NoError(t, err)
require.Equal(t, &res, article)
}
func TestDeleteWebhook(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/webhooks/12345", r.URL.Path)
require.Equal(t, http.MethodDelete, r.Method)
require.Equal(t, "myApiKey", r.Header.Get("api-key"))
w.WriteHeader(http.StatusOK)
}))
client := NewClient(withBaseURL(ts.URL), WithApiKey("myApiKey"))
err := client.DeleteWebhook(12345)
require.NoError(t, err)
}
func TestCreateWebhook(t *testing.T) {
var res Webhook
b := unmarshalGoldenFileBytes(t, "webhook.json", &res)
whr := CreateWebhookReq{
Source: "DEV",
Events: []string{"inserted", "updated"},
TargetURL: "my targetURL",
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/webhooks", r.URL.Path)
require.Equal(t, http.MethodPost, r.Method)
require.Equal(t, "myApiKey", r.Header.Get("api-key"))
require.Equal(t, "application/json", r.Header.Get("Content-Type"))
rb, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
var car webhookReq
require.NoError(t, json.Unmarshal(rb, &car))
require.Equal(t, webhookReq{Webhook: whr}, car)
w.WriteHeader(http.StatusOK)
w.Write(b)
}))
client := NewClient(withBaseURL(ts.URL), WithApiKey("myApiKey"))
articles, err := client.CreateWebhook(whr)
require.NoError(t, err)
require.Equal(t, res, articles)
}