forked from go-pkgz/notify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook_test.go
93 lines (77 loc) · 2.86 KB
/
webhook_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
package notify
import (
"bytes"
"context"
"errors"
"io"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type funcWebhookClient func(*http.Request) (*http.Response, error)
func (c funcWebhookClient) Do(r *http.Request) (*http.Response, error) {
return c(r)
}
var okWebhookClient = funcWebhookClient(func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString("ok")),
}, nil
})
type errReader struct {
}
func (errReader) Read(_ []byte) (n int, err error) {
return 0, errors.New("test error")
}
func TestWebhook_Send(t *testing.T) {
// empty header to check wrong header handling case
wh := NewWebhook(WebhookParams{Headers: []string{"Content-Type:application/json,text/plain", ""}})
wh.webhookClient = funcWebhookClient(func(r *http.Request) (*http.Response, error) {
assert.Len(t, r.Header, 1)
assert.Equal(t, r.Header.Get("Content-Type"), "application/json,text/plain")
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString("")),
}, nil
})
assert.NotNil(t, wh)
err := wh.Send(context.Background(), "https://example.org/webhook", "some_text")
assert.NoError(t, err)
wh.webhookClient = okWebhookClient
assert.NoError(t, err)
err = wh.Send(nil, "https://example.org/webhook", "some_text") //nolint
require.Error(t, err)
assert.Contains(t, err.Error(), "unable to create webhook request")
wh.webhookClient = funcWebhookClient(func(*http.Request) (*http.Response, error) {
return nil, errors.New("request failed")
})
err = wh.Send(context.Background(), "https://not-existing-url.net", "some_text")
require.Error(t, err)
assert.Contains(t, err.Error(), "webhook request failed")
wh.webhookClient = funcWebhookClient(func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(bytes.NewBufferString("not found")),
}, nil
})
err = wh.Send(context.Background(), "http:/example.org/invalid-url", "some_text")
require.Error(t, err)
assert.Contains(t, err.Error(), "non-OK status code: 404, body: not found")
wh.webhookClient = funcWebhookClient(func(*http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusNotFound,
Body: io.NopCloser(errReader{}),
}, nil
})
err = wh.Send(context.Background(), "http:/example.org/invalid-url", "some_text")
require.Error(t, err)
assert.Contains(t, err.Error(), "non-OK status code: 404")
assert.NotContains(t, err.Error(), "body")
}
func TestWebhook_String(t *testing.T) {
wh := NewWebhook(WebhookParams{Headers: []string{"Content-Type:application/json,text/plain"}})
assert.NotNil(t, wh)
str := wh.String()
assert.Equal(t, "webhook notification with timeout 5s and headers [Content-Type:application/json,text/plain]", str)
}