forked from gofiber/fiber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hooks_test.go
202 lines (144 loc) · 3.66 KB
/
hooks_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
package fiber
import (
"errors"
"fmt"
"testing"
"time"
"github.com/gofiber/fiber/v2/internal/bytebufferpool"
"github.com/gofiber/fiber/v2/utils"
)
var testSimpleHandler = func(c *Ctx) error {
return c.SendString("simple")
}
func Test_Hook_OnRoute(t *testing.T) {
t.Parallel()
app := New()
app.Hooks().OnRoute(func(r Route) error {
utils.AssertEqual(t, "", r.Name)
return nil
})
app.Get("/", testSimpleHandler).Name("x")
subApp := New()
subApp.Get("/test", testSimpleHandler)
app.Mount("/sub", subApp)
}
func Test_Hook_OnName(t *testing.T) {
t.Parallel()
app := New()
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
app.Hooks().OnName(func(r Route) error {
_, err := buf.WriteString(r.Name)
utils.AssertEqual(t, nil, err)
return nil
})
app.Get("/", testSimpleHandler).Name("index")
subApp := New()
subApp.Get("/test", testSimpleHandler)
subApp.Get("/test2", testSimpleHandler)
app.Mount("/sub", subApp)
utils.AssertEqual(t, "index", buf.String())
}
func Test_Hook_OnName_Error(t *testing.T) {
t.Parallel()
app := New()
defer func() {
if err := recover(); err != nil {
utils.AssertEqual(t, "unknown error", fmt.Sprintf("%v", err))
}
}()
app.Hooks().OnName(func(r Route) error {
return errors.New("unknown error")
})
app.Get("/", testSimpleHandler).Name("index")
}
func Test_Hook_OnGroup(t *testing.T) {
t.Parallel()
app := New()
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
app.Hooks().OnGroup(func(g Group) error {
_, err := buf.WriteString(g.Prefix)
utils.AssertEqual(t, nil, err)
return nil
})
grp := app.Group("/x").Name("x.")
grp.Group("/a")
utils.AssertEqual(t, "/x/x/a", buf.String())
}
func Test_Hook_OnGroupName(t *testing.T) {
t.Parallel()
app := New()
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
app.Hooks().OnGroupName(func(g Group) error {
_, err := buf.WriteString(g.name)
utils.AssertEqual(t, nil, err)
return nil
})
grp := app.Group("/x").Name("x.")
grp.Get("/test", testSimpleHandler)
grp.Get("/test2", testSimpleHandler)
utils.AssertEqual(t, "x.", buf.String())
}
func Test_Hook_OnGroupName_Error(t *testing.T) {
t.Parallel()
app := New()
defer func() {
if err := recover(); err != nil {
utils.AssertEqual(t, "unknown error", fmt.Sprintf("%v", err))
}
}()
app.Hooks().OnGroupName(func(g Group) error {
return errors.New("unknown error")
})
grp := app.Group("/x").Name("x.")
grp.Get("/test", testSimpleHandler)
}
func Test_Hook_OnShutdown(t *testing.T) {
t.Parallel()
app := New()
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
app.Hooks().OnShutdown(func() error {
_, err := buf.WriteString("shutdowning")
utils.AssertEqual(t, nil, err)
return nil
})
utils.AssertEqual(t, nil, app.Shutdown())
utils.AssertEqual(t, "shutdowning", buf.String())
}
func Test_Hook_OnListen(t *testing.T) {
t.Parallel()
app := New(Config{
DisableStartupMessage: true,
})
buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)
app.Hooks().OnListen(func() error {
_, err := buf.WriteString("ready")
utils.AssertEqual(t, nil, err)
return nil
})
go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()
utils.AssertEqual(t, nil, app.Listen(":9000"))
utils.AssertEqual(t, "ready", buf.String())
}
func Test_Hook_OnHook(t *testing.T) {
// Reset test var
testPreforkMaster = true
testOnPrefork = true
app := New()
go func() {
time.Sleep(1000 * time.Millisecond)
utils.AssertEqual(t, nil, app.Shutdown())
}()
app.Hooks().OnFork(func(pid int) error {
utils.AssertEqual(t, 1, pid)
return nil
})
utils.AssertEqual(t, nil, app.prefork(NetworkTCP4, ":3000", nil))
}