forked from gotd/botapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler_test.go
More file actions
106 lines (82 loc) · 2.36 KB
/
Copy pathhandler_test.go
File metadata and controls
106 lines (82 loc) · 2.36 KB
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
package botapi
import (
"context"
"errors"
"testing"
)
func TestRouterFirstMatchWins(t *testing.T) {
b := newTestBot(t)
var calls []string
b.on(func(c *Context) error { calls = append(calls, "skipped"); return nil }, func(c *Context) bool { return false })
b.on(func(c *Context) error { calls = append(calls, "matched"); return nil })
b.on(func(c *Context) error { calls = append(calls, "second-match"); return nil })
b.route(context.Background(), &Update{UpdateID: 1})
if len(calls) != 1 || calls[0] != "matched" {
t.Fatalf("expected only the first matching handler, got %v", calls)
}
}
func TestMiddlewareOrder(t *testing.T) {
b := newTestBot(t)
var order []string
b.Use(func(next Handler) Handler {
return func(c *Context) error {
order = append(order, "outer-in")
defer func() { order = append(order, "outer-out") }()
return next(c)
}
})
b.Use(func(next Handler) Handler {
return func(c *Context) error {
order = append(order, "inner-in")
defer func() { order = append(order, "inner-out") }()
return next(c)
}
})
b.on(func(c *Context) error { order = append(order, "handler"); return nil })
b.route(context.Background(), &Update{})
want := []string{"outer-in", "inner-in", "handler", "inner-out", "outer-out"}
if len(order) != len(want) {
t.Fatalf("order = %v, want %v", order, want)
}
for i := range want {
if order[i] != want[i] {
t.Fatalf("order = %v, want %v", order, want)
}
}
}
func TestOuterMiddlewareOrder(t *testing.T) {
b := newTestBot(t)
var order []string
b.UseOuter(func(next Handler) Handler {
return func(c *Context) error {
order = append(order, "outer")
return next(c)
}
})
b.Use(func(next Handler) Handler {
return func(c *Context) error {
order = append(order, "global")
return next(c)
}
})
b.on(func(c *Context) error {
order = append(order, "handler")
return nil
})
b.route(context.Background(), &Update{})
want := []string{"outer", "global", "handler"}
if len(order) != len(want) {
t.Fatalf("order = %v, want %v", order, want)
}
for i := range want {
if order[i] != want[i] {
t.Fatalf("order = %v, want %v", order, want)
}
}
}
func TestRouterHandlerErrorIsContained(t *testing.T) {
b := newTestBot(t)
b.on(func(c *Context) error { return errors.New("boom") })
// Must not panic or propagate; the error is logged.
b.route(context.Background(), &Update{})
}