MCPcopy
hub / github.com/gofiber/fiber / Test_RouteChain_WithHTTPHandlers

Function Test_RouteChain_WithHTTPHandlers

router_test.go:179–222  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

177}
178
179func Test_RouteChain_WithHTTPHandlers(t *testing.T) {
180 t.Parallel()
181
182 app := New()
183
184 chain := app.RouteChain("/combo")
185 chain.Get(func(c Ctx) error {
186 c.Set("X-Chain", "fiber")
187 return c.Next()
188 }, http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
189 _, err := w.Write([]byte("combo"))
190 assert.NoError(t, err)
191 }))
192
193 chain.RouteChain("/nested").Get(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
194 w.Header().Set("X-Nested", "true")
195 _, err := w.Write([]byte("nested"))
196 assert.NoError(t, err)
197 }))
198
199 resp, err := app.Test(httptest.NewRequest(MethodGet, "/combo", http.NoBody))
200 require.NoError(t, err)
201 require.Equal(t, 200, resp.StatusCode)
202 require.Equal(t, "fiber", resp.Header.Get("X-Chain"))
203 t.Cleanup(func() {
204 require.NoError(t, resp.Body.Close())
205 })
206
207 body, err := io.ReadAll(resp.Body)
208 require.NoError(t, err)
209 require.Equal(t, "combo", string(body))
210
211 nestedResp, err := app.Test(httptest.NewRequest(MethodGet, "/combo/nested", http.NoBody))
212 require.NoError(t, err)
213 require.Equal(t, 200, nestedResp.StatusCode)
214 require.Equal(t, "true", nestedResp.Header.Get("X-Nested"))
215 t.Cleanup(func() {
216 require.NoError(t, nestedResp.Body.Close())
217 })
218
219 nestedBody, err := io.ReadAll(nestedResp.Body)
220 require.NoError(t, err)
221 require.Equal(t, "nested", string(nestedBody))
222}
223
224func Test_Route_Match_SameLength(t *testing.T) {
225 t.Parallel()

Callers

nothing calls this directly

Calls 9

TestMethod · 0.80
NewFunction · 0.70
RouteChainMethod · 0.65
GetMethod · 0.65
SetMethod · 0.65
NextMethod · 0.65
WriteMethod · 0.65
CloseMethod · 0.65
HeaderMethod · 0.45

Tested by

no test coverage detected