forked from dasrick/go-teams-notify
-
Notifications
You must be signed in to change notification settings - Fork 17
/
send_test.go
284 lines (259 loc) · 9.02 KB
/
send_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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright 2020 Enrico Hoffmann
// Copyright 2021 Adam Chalkley
//
// https://github.com/atc0005/go-teams-notify
//
// Licensed under the MIT License. See LICENSE file in the project root for
// full license information.
package goteamsnotify
import (
"bytes"
"errors"
"io/ioutil"
"net/http"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewClient(t *testing.T) {
client := NewClient()
assert.IsType(t, &teamsClient{}, client)
}
func TestTeamsClientSend(t *testing.T) {
// THX@Hassansin ... http:https://hassansin.github.io/Unit-Testing-http-client-in-Go
simpleMsgCard := NewMessageCard()
simpleMsgCard.Text = "Hello World"
var tests = []struct {
name string
reqURL string
reqMsg MessageCard
resBody string // httpClient response body text
resError error // httpClient error
error error // method error
validationURLPatterns []string
skipURLVal bool // whether webhook URL validation is applied (e.g., GH-68)
resStatus int // httpClient response status
}{
{
name: "invalid webhookURL - url.Parse error",
reqURL: "ht\ttp:https://",
reqMsg: simpleMsgCard,
resStatus: 0,
resBody: "invalid",
resError: nil,
error: &url.Error{},
skipURLVal: false,
},
{
name: "invalid webhookURL - missing prefix in webhook URL",
reqURL: "",
reqMsg: simpleMsgCard,
resStatus: 0,
resBody: "invalid",
resError: nil,
error: ErrWebhookURLUnexpected,
skipURLVal: false,
},
{
name: "invalid httpClient.Do call using outlook.office.com URL",
reqURL: "https://outlook.office.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 200,
resBody: http.StatusText(http.StatusOK),
resError: errors.New("pling"),
error: &url.Error{},
skipURLVal: false,
},
{
name: "invalid httpClient.Do call using outlook.office365.com URL",
reqURL: "https://outlook.office365.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 200,
resBody: http.StatusText(http.StatusOK),
resError: errors.New("pling"),
error: &url.Error{},
skipURLVal: false,
},
{
name: "invalid response status code using outlook.office.com URL",
reqURL: "https://outlook.office.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 400,
resBody: http.StatusText(http.StatusBadRequest),
resError: nil,
error: errors.New(""),
skipURLVal: false,
},
{
name: "invalid response status code using outlook.office365.com URL",
reqURL: "https://outlook.office365.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 400,
resBody: http.StatusText(http.StatusBadRequest),
resError: nil,
error: errors.New(""),
skipURLVal: false,
},
{
name: "valid values using outlook.office.com URL",
reqURL: "https://outlook.office.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 200,
resBody: ExpectedWebhookURLResponseText,
resError: nil,
error: nil,
skipURLVal: false,
},
{
name: "valid values using outlook.office365.com URL",
reqURL: "https://outlook.office365.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 200,
resBody: ExpectedWebhookURLResponseText,
resError: nil,
error: nil,
skipURLVal: false,
},
{
// This test case should not result in an actual client request
// going out as validation failure should occur.
name: "custom webhook domain without disabling validation",
reqURL: "https://example.webhook.office.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 0,
resBody: "",
resError: nil,
error: ErrWebhookURLUnexpected,
skipURLVal: false,
},
{
// This is expected to succeed, provided that the actual webhook
// URL is valid. GH-68 indicates that private webhook endpoints
// exist, but without knowing the names or valid patterns, this is
// about all we can do for now?
name: "custom webhook domain with validation disabled",
reqURL: "https://example.webhook.office.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 200,
resBody: ExpectedWebhookURLResponseText,
resError: nil,
error: nil,
skipURLVal: true,
},
{
name: "custom webhook domain with custom validation patterns matching requirements",
reqURL: "https://arbitrary.domain.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 200,
resBody: ExpectedWebhookURLResponseText,
resError: nil,
error: nil,
skipURLVal: false,
validationURLPatterns: []string{DefaultWebhookURLValidationPattern, "arbitrary.domain.com"},
},
{
name: "custom webhook domain with custom validation patterns not matching requirements",
reqURL: "https://arbitrary.test.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 200,
resBody: ExpectedWebhookURLResponseText,
resError: nil,
error: ErrWebhookURLUnexpected,
skipURLVal: false,
validationURLPatterns: []string{DefaultWebhookURLValidationPattern, "arbitrary.domain.com"},
},
{
name: "custom webhook domain with complex custom validation pattern matching requirements",
reqURL: "https://foo.domain.com/webhook/xxx",
reqMsg: simpleMsgCard,
resStatus: 200,
resBody: ExpectedWebhookURLResponseText,
resError: nil,
error: nil,
skipURLVal: false,
validationURLPatterns: []string{`^https://.*\.domain\.com/.*$`},
},
}
for idx, test := range tests {
// Create range scoped var for use within closure
test := test
t.Run(test.name, func(t *testing.T) {
client := NewTestClient(func(req *http.Request) (*http.Response, error) {
// Test request parameters
assert.Equal(t, req.URL.String(), test.reqURL)
// GH-46; fix contributed by @davecheney (thank you!)
//
// The RoundTripper documentation notes that nil must be
// returned as the error value if a response is received. A
// non-nil error should be returned for failure to obtain a
// response. Failure to obtain a response is indicated by the
// test table response error, so we represent that failure to
// obtain a response by returning nil and the test table
// response error explaining why a response could not be
// retrieved.
if test.resError != nil {
return nil, test.resError
}
// GH-46 (cont) If no table test response errors are provided,
// then the response was retrieved (provided below), so we are
// required to return nil as the error value along with the
// response.
return &http.Response{
StatusCode: test.resStatus,
// Send response to be tested
Body: ioutil.NopCloser(bytes.NewBufferString(test.resBody)),
// Must be set to non-nil value or it panics
Header: make(http.Header),
}, nil
})
c := &teamsClient{httpClient: client}
c.AddWebhookURLValidationPatterns(test.validationURLPatterns...)
// Disable webhook URL prefix validation if specified by table
// test entry. See GH-68 for additional details.
if test.skipURLVal {
t.Log("Calling SkipWebhookURLValidationOnSend")
c.SkipWebhookURLValidationOnSend(true)
}
err := c.Send(test.reqURL, test.reqMsg)
switch {
// An error occurred, but table test entry indicates no error expected.
case err != nil && test.error == nil:
t.Logf("FAIL: test %d; error occurred, but none expected!", idx)
t.Fatalf(
"\nFAIL: test %d\ngot %v\nwant %v",
idx,
err,
test.error,
)
// No error occurred, but table test entry indicates one expected.
case err == nil && test.error != nil:
t.Logf("FAIL: test %d; no error occurred, but one was expected!", idx)
t.Fatalf(
"\nFAIL: test %d\ngot %v\nwant %v",
idx,
err,
test.error,
)
// No error occurred and table test entry indicates no error expected.
case err == nil && test.error == nil:
t.Logf("OK: test %d; no error occurred and table test entry indicates no error expected.", idx)
// Error occurred and table test entry indicates one expected.
case err != nil && test.error != nil:
t.Logf("OK: test %d; error occurred and table test entry indicates one expected.", idx)
}
})
}
}
// helper for testing --------------------------------------------------------------------------------------------------
// RoundTripFunc .
type RoundTripFunc func(req *http.Request) (*http.Response, error)
// RoundTrip .
func (f RoundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) {
return f(req)
}
// NewTestClient returns *http.API with Transport replaced to avoid making real calls
func NewTestClient(fn RoundTripFunc) *http.Client {
return &http.Client{
Transport: fn,
}
}