-
Notifications
You must be signed in to change notification settings - Fork 3
/
middleware_test.go
65 lines (54 loc) · 1.66 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
package logger_test
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/gol4ng/logger"
)
func TestMiddlewareInterfaceDecorate(t *testing.T) {
myEntry := logger.Entry{}
fakeHandler := func(e logger.Entry) error {
assert.Equal(t, myEntry, e)
return errors.New("my fake handler error")
}
fakeMiddleware := func(h logger.HandlerInterface) logger.HandlerInterface {
return func(e logger.Entry) error {
assert.Equal(t, myEntry, e)
err := h(e)
assert.Error(t, err)
return errors.New("middleware prepend " + err.Error())
}
}
middlewares := logger.MiddlewareStack(fakeMiddleware)
handler := middlewares.Decorate(fakeHandler)
err := handler(myEntry)
assert.Error(t, err)
assert.Equal(t, "middleware prepend my fake handler error", err.Error())
}
func TestDecorateHandler(t *testing.T) {
myEntry := logger.Entry{}
fakeHandler := func(e logger.Entry) error {
assert.Equal(t, myEntry, e)
return errors.New("my fake handler error")
}
fakeMiddleware := func(h logger.HandlerInterface) logger.HandlerInterface {
return func(e logger.Entry) error {
assert.Equal(t, myEntry, e)
err := h(e)
assert.Error(t, err)
return errors.New("middleware prepend " + err.Error())
}
}
handler := logger.DecorateHandler(fakeHandler, fakeMiddleware)
err := handler(myEntry)
assert.Error(t, err)
assert.Equal(t, "middleware prepend my fake handler error", err.Error())
}
func TestMiddlewareStack(t *testing.T) {
fakeMiddleware := func(logger.HandlerInterface) logger.HandlerInterface {
return func(entry logger.Entry) error {
return nil
}
}
assert.IsType(t, logger.Middlewares{}, logger.MiddlewareStack(fakeMiddleware))
}