forked from contentful-labs/contentful-go
-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathwebhook_call_test.go
148 lines (110 loc) · 3.71 KB
/
webhook_call_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
137
138
139
140
141
142
143
144
145
146
147
148
package contentful
import (
"fmt"
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
)
func TestWebhookCallsService_List(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertions.Equal(r.Method, "GET")
assertions.Equal(r.URL.Path, "/spaces/"+spaceID+"/webhooks/0KzM2HxYr5O1pZ4SaUzK8h/calls")
checkHeaders(r, assertions)
w.WriteHeader(200)
_, _ = fmt.Fprintln(w, readTestData("webhook_call.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
collection, err := cma.WebhookCalls.List(spaceID, "0KzM2HxYr5O1pZ4SaUzK8h").Next()
assertions.Nil(err)
spaces := collection.ToWebhookCall()
assertions.Equal(1, len(spaces))
assertions.Equal("bar", spaces[0].Sys.ID)
}
func TestWebhookCallsService_Get(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertions.Equal(r.Method, "GET")
assertions.Equal(r.URL.Path, "/spaces/"+spaceID+"/webhooks/0KzM2HxYr5O1pZ4SaUzK8h/calls/bar")
checkHeaders(r, assertions)
w.WriteHeader(200)
_, _ = fmt.Fprintln(w, readTestData("webhook_call_detail.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
callDetails, err := cma.WebhookCalls.Get(spaceID, "0KzM2HxYr5O1pZ4SaUzK8h", "bar")
assertions.Nil(err)
assertions.Equal("bar", callDetails.Sys.ID)
assertions.Equal("https://webhooks.example.com/endpoint", callDetails.Request.URL)
}
func TestWebhookCallsService_Get_2(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertions.Equal(r.Method, "GET")
assertions.Equal(r.URL.Path, "/spaces/"+spaceID+"/webhooks/0KzM2HxYr5O1pZ4SaUzK8h/calls/bar")
checkHeaders(r, assertions)
w.WriteHeader(400)
_, _ = fmt.Fprintln(w, readTestData("webhook_call_detail.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
_, err = cma.WebhookCalls.Get(spaceID, "0KzM2HxYr5O1pZ4SaUzK8h", "bar")
assertions.Empty(err)
}
func TestWebhookCallsService_Health(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertions.Equal(r.Method, "GET")
assertions.Equal(r.URL.Path, "/spaces/"+spaceID+"/webhooks/0KzM2HxYr5O1pZ4SaUzK8h/health")
checkHeaders(r, assertions)
w.WriteHeader(200)
_, _ = fmt.Fprintln(w, readTestData("webhook_health.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
health, err := cma.WebhookCalls.Health(spaceID, "0KzM2HxYr5O1pZ4SaUzK8h")
assertions.Nil(err)
assertions.Equal("bar", health.Sys.ID)
assertions.Equal(233, health.Calls.Total)
}
func TestWebhookCallsService_Health_2(t *testing.T) {
var err error
assertions := assert.New(t)
handler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assertions.Equal(r.Method, "GET")
assertions.Equal(r.URL.Path, "/spaces/"+spaceID+"/webhooks/0KzM2HxYr5O1pZ4SaUzK8h/health")
checkHeaders(r, assertions)
w.WriteHeader(400)
_, _ = fmt.Fprintln(w, readTestData("webhook_health.json"))
})
// test server
server := httptest.NewServer(handler)
defer server.Close()
// cma client
cma = NewCMA(CMAToken)
cma.BaseURL = server.URL
_, err = cma.WebhookCalls.Health(spaceID, "0KzM2HxYr5O1pZ4SaUzK8h")
assertions.Nil(err)
}