Repository navigation
Expand file tree
/
Copy pathrouter_test.go
More file actions
301 lines (259 loc) · 9.74 KB
/
Copy pathrouter_test.go
File metadata and controls
301 lines (259 loc) · 9.74 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package httpserver
import (
"bytes"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// TestRouterTestHelperSmoke proves the test helper reaches a handler registered
// with the real [Router.Handle] and that the configured response is written.
func TestRouterTestHelperSmoke(t *testing.T) {
router := newTestRouter()
router.Handle("GET /ping",
func(ctx *Context) {
ctx.NewResponse(http.StatusOK).StringBody("pong")
})
rec := doRouterRequest(router, http.MethodGet, "/ping")
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, "pong", rec.Body.String())
}
// TestRouter_Handle_WritesConfiguredStatusAndBody verifies that a handler that
// configures a response has that response written through [Router.Handle].
func TestRouter_Handle_WritesConfiguredStatusAndBody(t *testing.T) {
router := newTestRouter()
router.Handle("GET /items",
func(ctx *Context) {
ctx.NewResponse(http.StatusCreated).JsonBody(map[string]string{"id": "42"})
})
rec := doRouterRequest(router, http.MethodGet, "/items")
assert.Equal(t, http.StatusCreated, rec.Code)
assert.Equal(t, "application/json; charset=utf-8", rec.Header().Get("Content-Type"))
assert.JSONEq(t, `{"id":"42"}`, rec.Body.String())
}
// TestRouter_Handle_NoResponse_Produces500 verifies the fallback inside
// [Router.Handle]: a handler that never calls [Context.NewResponse] results in
// HTTP 500.
func TestRouter_Handle_NoResponse_Produces500(t *testing.T) {
router := newTestRouter()
router.Handle("GET /noop",
func(ctx *Context) {
// intentionally do not configure a response
})
rec := doRouterRequest(router, http.MethodGet, "/noop")
assert.Equal(t, http.StatusInternalServerError, rec.Code)
}
// recordingMiddleware appends label before calling next, then appends
// label+":after" once next returns.
func recordingMiddleware(trace *[]string, label string) Middleware {
return func(ctx *Context, next func()) {
*trace = append(*trace, label+":before")
next()
*trace = append(*trace, label+":after")
}
}
// TestRouter_Middleware_Order verifies middleware execution order is exactly:
// outer before -> inner before -> handler -> inner after -> outer after.
func TestRouter_Middleware_Order(t *testing.T) {
trace := make([]string, 0, 6)
router := newTestRouter()
router.middlewares = []Middleware{recordingMiddleware(&trace, "outer")}
router.Group(recordingMiddleware(&trace, "inner")).Handle("GET /x",
func(ctx *Context) {
trace = append(trace, "handler")
ctx.NewResponse(http.StatusOK)
})
rec := doRouterRequest(router, http.MethodGet, "/x")
require.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, []string{
"outer:before",
"inner:before",
"handler",
"inner:after",
"outer:after",
}, trace)
}
// TestRouter_Middleware_ShortCircuit verifies that middleware can short-circuit
// the chain by not calling next; downstream middleware/handler must not run.
func TestRouter_Middleware_ShortCircuit(t *testing.T) {
trace := make([]string, 0, 4)
router := newTestRouter()
router.middlewares = []Middleware{
func(ctx *Context, next func()) {
trace = append(trace, "outer:before")
next()
trace = append(trace, "outer:after")
},
}
router.Group(
func(ctx *Context, next func()) {
// do not call next; this short-circuits
trace = append(trace, "short:before")
ctx.NewResponse(http.StatusTeapot)
trace = append(trace, "short:after")
},
).Handle("GET /x",
func(ctx *Context) {
t.Errorf("handler should not run when middleware short-circuits")
})
rec := doRouterRequest(router, http.MethodGet, "/x")
assert.Equal(t, http.StatusTeapot, rec.Code)
assert.Equal(t, []string{
"outer:before",
"short:before",
"short:after",
"outer:after",
}, trace)
}
// TestRouter_Middleware_ReplaceResponse_AfterNext verifies that a middleware
// can inspect the downstream response after next and then modify/replace it.
func TestRouter_Middleware_ReplaceResponse_AfterNext(t *testing.T) {
router := newTestRouter()
router.middlewares = []Middleware{
func(ctx *Context, next func()) {
next()
// overwrite whatever downstream configured
ctx.NewResponse(http.StatusTeapot).StringBody("overwritten")
},
}
router.Handle("GET /x",
func(ctx *Context) {
ctx.NewResponse(http.StatusOK).StringBody("original")
})
rec := doRouterRequest(router, http.MethodGet, "/x")
assert.Equal(t, http.StatusTeapot, rec.Code)
assert.Equal(t, "overwritten", rec.Body.String())
}
// TestRouter_Group_InheritsParentMiddleware verifies that a group inherits its
// parent's middleware in parent-first order.
func TestRouter_Group_InheritsParentMiddleware(t *testing.T) {
trace := make([]string, 0, 2)
router := newTestRouter()
router.middlewares = []Middleware{recordingMiddleware(&trace, "parent")}
// call Group with no additional middleware; the group should still run parent
group := router.Group()
assert.Len(t, group.middlewares, 1)
group.Handle("GET /x",
func(ctx *Context) {
ctx.NewResponse(http.StatusOK)
})
rec := doRouterRequest(router, http.MethodGet, "/x")
require.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, []string{"parent:before", "parent:after"}, trace)
}
// TestRouter_NestedGroups_PreserveOrder verifies that nested groups preserve
// middleware order across all levels.
func TestRouter_NestedGroups_PreserveOrder(t *testing.T) {
trace := make([]string, 0, 8)
router := newTestRouter()
router.middlewares = []Middleware{recordingMiddleware(&trace, "L1")}
level2 := router.Group(recordingMiddleware(&trace, "L2"))
level3 := level2.Group(recordingMiddleware(&trace, "L3"))
assert.Len(t, level3.middlewares, 3)
level3.Handle("GET /x",
func(ctx *Context) {
trace = append(trace, "handler")
ctx.NewResponse(http.StatusOK)
})
rec := doRouterRequest(router, http.MethodGet, "/x")
require.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, []string{
"L1:before", "L2:before", "L3:before",
"handler",
"L3:after", "L2:after", "L1:after",
}, trace)
}
// TestRouter_Group_DoesNotMutateParentOrSibling verifies that creating a child
// group does not mutate the parent or a sibling group.
func TestRouter_Group_DoesNotMutateParentOrSibling(t *testing.T) {
parentMW := recordingMiddleware(nil, "parent")
router := newTestRouter()
router.middlewares = []Middleware{parentMW}
childA := router.Group(recordingMiddleware(nil, "A"))
childB := router.Group(recordingMiddleware(nil, "B"))
// parent has only the original middleware
assert.Len(t, router.middlewares, 1)
// childA and childB each carry one of their own, plus the parent
assert.Len(t, childA.middlewares, 2)
assert.Len(t, childB.middlewares, 2)
// adding to childA must not affect parent or childB
childA.Group(recordingMiddleware(nil, "A.child"))
assert.Len(t, router.middlewares, 1)
assert.Len(t, childB.middlewares, 2)
}
// TestRouter_Group_DoesNotAliasCallerSlice verifies that mutating the caller's
// input slice after calling Group does not affect the resulting group: Group
// does not retain the caller's middleware slice.
func TestRouter_Group_DoesNotAliasCallerSlice(t *testing.T) {
m1 := func(ctx *Context, next func()) {
next()
}
middlewares := []Middleware{m1}
group := newTestRouter().Group(middlewares...)
// caller now mutates the slice contents
middlewares[0] = func(ctx *Context, next func()) {
t.Errorf("mutated middleware should not be used")
next()
}
// the group should still use m1
require.Len(t, group.middlewares, 1)
// identity check: the captured middleware must be m1
ran := false
group.middlewares[0](nil, func() { ran = true })
assert.True(t, ran, "group middleware should still call next through m1")
}
// TestRouter_IndependentRoutes verifies that two routes registered from the
// same router remain independent.
func TestRouter_IndependentRoutes(t *testing.T) {
router := newTestRouter()
router.Handle("GET /a",
func(ctx *Context) {
ctx.NewResponse(http.StatusOK).StringBody("a")
})
router.Handle("GET /b",
func(ctx *Context) {
ctx.NewResponse(http.StatusOK).StringBody("b")
})
recA := doRouterRequest(router, http.MethodGet, "/a")
recB := doRouterRequest(router, http.MethodGet, "/b")
assert.Equal(t, http.StatusOK, recA.Code)
assert.Equal(t, "a", recA.Body.String())
assert.Equal(t, http.StatusOK, recB.Code)
assert.Equal(t, "b", recB.Body.String())
}
// TestRouter_Handle_WriteResponseError_LogsFailure exercises the
// `ctx.writeResponse()` error path in [Router.Handle]: a handler that returns a
// failing [Response.StreamBody] must produce a 200 (status already committed by
// StreamBody), must not panic, and the failure must be logged through zerolog.
func TestRouter_Handle_WriteResponseError_LogsFailure(t *testing.T) {
var logBuf bytes.Buffer
logger := zerolog.New(&logBuf)
router := Router{serveMux: http.NewServeMux(), logger: &logger}
streamErr := errors.New("stream failed")
router.Handle("GET /", func(ctx *Context) {
ctx.NewResponse(http.StatusOK).StreamBody(func(*StreamWriter) error {
return streamErr
})
})
req, err := http.NewRequest(http.MethodGet, "/", nil)
require.NoError(t, err)
req = req.WithContext(logger.WithContext(req.Context()))
rec := httptest.NewRecorder()
// The stream-body error occurs after WriteHeader: the committed status
// stays on the recorder, the error is logged, and writeResponse aborts the
// connection via http.ErrAbortHandler.
assert.PanicsWithValue(t, http.ErrAbortHandler, func() {
router.serveMux.ServeHTTP(rec, req)
})
assert.Equal(t, http.StatusOK, rec.Code)
assert.Contains(t, logBuf.String(), "Failed to write response")
assert.Contains(t, logBuf.String(), "stream failed")
}