-
Notifications
You must be signed in to change notification settings - Fork 69
/
middleware_test.go
97 lines (76 loc) · 1.98 KB
/
middleware_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
package bugsnag
import (
"bytes"
"fmt"
"github.com/bugsnag/bugsnag-go/errors"
"log"
"reflect"
"testing"
)
func TestMiddlewareOrder(t *testing.T) {
err := fmt.Errorf("test")
data := []interface{}{errors.New(err, 1)}
event, config := newEvent(data, &defaultNotifier)
result := make([]int, 0, 7)
stack := middlewareStack{}
stack.OnBeforeNotify(func(e *Event, c *Configuration) error {
result = append(result, 2)
return nil
})
stack.OnBeforeNotify(func(e *Event, c *Configuration) error {
result = append(result, 1)
return nil
})
stack.OnBeforeNotify(func(e *Event, c *Configuration) error {
result = append(result, 0)
return nil
})
stack.Run(event, config, func() error {
result = append(result, 3)
return nil
})
if !reflect.DeepEqual(result, []int{0, 1, 2, 3}) {
t.Errorf("unexpected middleware order %v", result)
}
}
func TestBeforeNotifyReturnErr(t *testing.T) {
stack := middlewareStack{}
err := fmt.Errorf("test")
data := []interface{}{errors.New(err, 1)}
event, config := newEvent(data, &defaultNotifier)
stack.OnBeforeNotify(func(e *Event, c *Configuration) error {
return err
})
called := false
e := stack.Run(event, config, func() error {
called = true
return nil
})
if e != err {
t.Errorf("Middleware didn't return the error")
}
if called == true {
t.Errorf("Notify was called when BeforeNotify returned False")
}
}
func TestBeforeNotifyPanic(t *testing.T) {
stack := middlewareStack{}
err := fmt.Errorf("test")
event, _ := newEvent([]interface{}{errors.New(err, 1)}, &defaultNotifier)
stack.OnBeforeNotify(func(e *Event, c *Configuration) error {
panic("oops")
})
called := false
b := &bytes.Buffer{}
stack.Run(event, &Configuration{Logger: log.New(b, log.Prefix(), 0)}, func() error {
called = true
return nil
})
logged := b.String()
if logged != "bugsnag/middleware: unexpected panic: oops\n" {
t.Errorf("Logged: %s", logged)
}
if called == false {
t.Errorf("Notify was not called when BeforeNotify panicked")
}
}