-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler_mux_test.go
163 lines (143 loc) · 4.49 KB
/
handler_mux_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
package cone_test
import (
"testing"
"github.com/zapling/cone"
"github.com/zapling/cone/conetest"
)
func TestHandle(t *testing.T) {
t.Run("Empty subject should panic", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("Expected panic, empty subject is not allowed")
}
}()
c := cone.NewHandlerMux()
c.Handle("", nil)
})
t.Run("Nil handler is ok", func(t *testing.T) {
c := cone.NewHandlerMux()
c.Handle("event.subject", nil)
})
t.Run("Same subject should override previous handler", func(t *testing.T) {
c := cone.NewHandlerMux()
var handlerCalled string
var firstHandler cone.HandlerFunc = func(_ cone.Response, _ *cone.Event) { handlerCalled = "first" }
var secondHandler cone.HandlerFunc = func(_ cone.Response, _ *cone.Event) { handlerCalled = "second" }
c.Handle("event.subject", firstHandler)
c.Handle("event.subject", secondHandler)
r := conetest.NewRecorder()
c.Serve(r, conetest.NewEvent("event.subject", nil))
if handlerCalled != "second" {
t.Errorf("Second handler should have been called, but '%s' was called", handlerCalled)
}
})
}
func TestHandleFunc(t *testing.T) {
t.Run("Empty subject should panic", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("Expected panic, empty subject is not allowed")
}
}()
c := cone.NewHandlerMux()
c.HandleFunc("", nil)
})
t.Run("Nil handler is ok", func(t *testing.T) {
c := cone.NewHandlerMux()
c.HandleFunc("event.subject", nil)
})
t.Run("Same subject should override previous handler", func(t *testing.T) {
c := cone.NewHandlerMux()
var handlerCalled string
var firstHandler cone.HandlerFunc = func(_ cone.Response, _ *cone.Event) { handlerCalled = "first" }
var secondHandler cone.HandlerFunc = func(_ cone.Response, _ *cone.Event) { handlerCalled = "second" }
c.HandleFunc("event.subject", firstHandler)
c.HandleFunc("event.subject", secondHandler)
r := conetest.NewRecorder()
c.Serve(r, conetest.NewEvent("event.subject", nil))
if handlerCalled != "second" {
t.Errorf("Second handler should have been called, but '%s' was called", handlerCalled)
}
})
}
func TestHandlerMiddleware(t *testing.T) {
middleware := func(next cone.Handler) cone.HandlerFunc {
return func(r cone.Response, e *cone.Event) {
if e.Subject != "valid" {
return
}
next.Serve(r, e)
}
}
var handler cone.HandlerFunc = func(r cone.Response, e *cone.Event) {
_ = r.Ack()
}
t.Run("Should not pass middleware", func(t *testing.T) {
r := conetest.NewRecorder()
e := conetest.NewEvent("event.subject", nil)
middleware(handler).Serve(r, e)
if r.Result() != "" {
t.Fatalf("Expected no result but got: %s", r.Result())
}
})
t.Run("Should pass middleware", func(t *testing.T) {
r := conetest.NewRecorder()
e := conetest.NewEvent("valid", nil)
middleware(handler).Serve(r, e)
if r.Result() != conetest.Ack {
t.Fatalf("Expected ack but got: %s", r.Result())
}
})
}
// func TestMiddlewareAroundConsumer(t *testing.T) {
// s := conetest.NewSource()
// h := cone.NewHandlerMux()
// h.HandleFunc("event.subject", func(r cone.Response, e *cone.Event) {
// })
//
// middleware := func(next cone.Handler) cone.HandlerFunc {
// return func(r cone.Response, e *cone.Event) {
// next.Serve(r, e)
// }
// }
//
// c := cone.New(s, middleware(h))
// c.Serve()
// }
func TestServe(t *testing.T) {
t.Run("Nil Response should panic", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("Expected panic, empty Response is not allowed")
}
}()
c := cone.NewHandlerMux()
c.Serve(nil, conetest.NewEvent("event.subject", nil))
})
t.Run("Nil Event should panic", func(t *testing.T) {
defer func() {
if err := recover(); err == nil {
t.Fatalf("Expected panic but got none, empty Event is not allowed")
}
}()
c := cone.NewHandlerMux()
c.Serve(conetest.NewRecorder(), nil)
})
t.Run("Unregistred event should nak", func(t *testing.T) {
c := cone.NewHandlerMux()
r := conetest.NewRecorder()
c.Serve(r, conetest.NewEvent("not.wanted", nil))
if r.Result() != conetest.Nak {
t.Errorf("Expected %s but got: %s", conetest.Nak, r.Result())
}
})
t.Run("Registred event should ack", func(t *testing.T) {
c := cone.NewHandlerMux()
c.HandleFunc("is.wanted", func(_ cone.Response, _ *cone.Event) {})
r := conetest.NewRecorder()
c.Serve(r, conetest.NewEvent("is.wanted", nil))
if r.Result() != conetest.Ack {
t.Errorf("Expected %s but got: %s", conetest.Nak, r.Result())
}
})
}