forked from gemnasium/logrus-airbrake-hook
-
Notifications
You must be signed in to change notification settings - Fork 0
/
airbrake_test.go
203 lines (173 loc) · 5.23 KB
/
airbrake_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
package airbrake_hook
import (
"bytes"
"encoding/json"
"io/ioutil"
"net/http"
"testing"
"time"
"github.com/airbrake/gobrake"
"github.com/sirupsen/logrus"
)
type customErr struct {
msg string
}
func (e *customErr) Error() string {
return e.msg
}
const (
testAPIKey = "abcxyz"
testEnv = "development"
expectedClass = "*airbrake.customErr"
expectedMsg = "foo"
unintendedMsg = "Airbrake will not see this string"
)
var (
noticeChan = make(chan *gobrake.Notice, 1)
)
// TestLogEntryMessageReceived checks if invoking Logrus' log.Error
// method causes an XML payload containing the log entry message is received
// by a HTTP server emulating an Airbrake-compatible endpoint.
func TestLogEntryMessageReceived(t *testing.T) {
log := logrus.New()
hook := newTestHook()
log.Hooks.Add(hook)
log.Error(expectedMsg)
select {
case received := <-noticeChan:
receivedErr := received.Errors[0]
if receivedErr.Message != expectedMsg {
t.Errorf("Unexpected message received: %s", receivedErr.Message)
}
case <-time.After(time.Second):
t.Error("Timed out; no notice received by Airbrake API")
}
}
// TestLogEntryMessageReceived confirms that, when passing an error type using
// logrus.Fields, a HTTP server emulating an Airbrake endpoint receives the
// error message returned by the Error() method on the error interface
// rather than the logrus.Entry.Message string.
func TestLogEntryWithErrorReceived(t *testing.T) {
log := logrus.New()
hook := newTestHook()
log.Hooks.Add(hook)
log.WithFields(logrus.Fields{
"error": &customErr{expectedMsg},
}).Error(unintendedMsg)
select {
case received := <-noticeChan:
receivedErr := received.Errors[0]
if receivedErr.Message != expectedMsg {
t.Errorf("Unexpected message received: %s", receivedErr.Message)
}
if receivedErr.Type != expectedClass {
t.Errorf("Unexpected error class: %s", receivedErr.Type)
}
case <-time.After(time.Second):
t.Error("Timed out; no notice received by Airbrake API")
}
}
// TestLogEntryWithNonErrorTypeNotReceived confirms that, when passing a
// non-error type using logrus.Fields, a HTTP server emulating an Airbrake
// endpoint receives the logrus.Entry.Message string.
//
// Only error types are supported when setting the 'error' field using
// logrus.WithFields().
func TestLogEntryWithNonErrorTypeNotReceived(t *testing.T) {
log := logrus.New()
hook := newTestHook()
log.Hooks.Add(hook)
log.WithFields(logrus.Fields{
"error": expectedMsg,
}).Error(unintendedMsg)
select {
case received := <-noticeChan:
receivedErr := received.Errors[0]
if receivedErr.Message != unintendedMsg {
t.Errorf("Unexpected message received: %s", receivedErr.Message)
}
case <-time.After(time.Second):
t.Error("Timed out; no notice received by Airbrake API")
}
}
func TestLogEntryWithCustomFields(t *testing.T) {
log := logrus.New()
hook := newTestHook()
log.Hooks.Add(hook)
log.WithFields(logrus.Fields{
"user_id": "123",
}).Error(unintendedMsg)
select {
case received := <-noticeChan:
receivedErr := received.Errors[0]
if receivedErr.Message != unintendedMsg {
t.Errorf("Unexpected message received: %s", receivedErr.Message)
}
if received.Context["user_id"] != "123" {
t.Errorf("Expected message to contain Context[\"user_id\"] == \"123\" got %q", received.Context["user_id"])
}
case <-time.After(time.Second):
t.Error("Timed out; no notice received by Airbrake API")
}
}
func TestLogEntryWithHTTPRequestFields(t *testing.T) {
log := logrus.New()
hook := newTestHook()
log.Hooks.Add(hook)
req, err := http.NewRequest("GET", "http://example.com", nil)
if err != nil {
t.Fatal(err)
}
log.WithFields(logrus.Fields{
"user_id": "123",
"request": req,
}).Error(unintendedMsg)
select {
case received := <-noticeChan:
receivedErr := received.Errors[0]
if receivedErr.Message != unintendedMsg {
t.Errorf("Unexpected message received: %s", receivedErr.Message)
}
if received.Context["user_id"] != "123" {
t.Errorf("Expected message to contain Context[\"user_id\"] == \"123\" got %q", received.Context["user_id"])
}
if received.Context["url"] != "http://example.com" {
t.Errorf("Expected message to contain Context[\"url\"] == \"http://example.com\" got %q", received.Context["url"])
}
case <-time.After(time.Second):
t.Error("Timed out; no notice received by Airbrake API")
}
}
// Returns a new airbrakeHook with the test server proxied
func newTestHook() *airbrakeHook {
// Make a http.Client with the transport
httpClient := &http.Client{Transport: &FakeRoundTripper{}}
hook := NewHook(123, testAPIKey, "production")
hook.Airbrake.Client = httpClient
return hook
}
// gobrake API doesn't allow to override endpoint, we need a http.Roundtripper
type FakeRoundTripper struct {
}
func (rt *FakeRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
panic(err)
}
notice := &gobrake.Notice{}
err = json.Unmarshal(b, notice)
if err != nil {
panic(err)
}
noticeChan <- notice
jsonResponse := struct {
Id string `json:"id"`
}{"1"}
sendResponse, _ := json.Marshal(jsonResponse)
res := &http.Response{
StatusCode: http.StatusCreated,
Body: ioutil.NopCloser(bytes.NewReader(sendResponse)),
Header: make(http.Header),
}
return res, nil
}