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

Function Test_App_Mount_RoutePositions

mount_test.go:179–242  ·  view source on GitHub ↗

go test -run Test_App_Mount_RoutePositions

(t *testing.T)

Source from the content-addressed store, hash-verified

177
178// go test -run Test_App_Mount_RoutePositions
179func Test_App_Mount_RoutePositions(t *testing.T) {
180 t.Parallel()
181 testEndpoint := func(app *App, route, expectedBody string) {
182 resp, err := app.Test(httptest.NewRequest(MethodGet, route, http.NoBody))
183 require.NoError(t, err, "app.Test(req)")
184 body, err := io.ReadAll(resp.Body)
185 require.NoError(t, err)
186 require.Equal(t, StatusOK, resp.StatusCode, "Status code")
187 require.Equal(t, expectedBody, string(body), "Unexpected response body")
188 }
189
190 app := New()
191 subApp1 := New()
192 subApp2 := New()
193 // app setup
194 {
195 app.Use(func(c Ctx) error {
196 // set initial value
197 c.Locals("world", "world")
198 return c.Next()
199 })
200 app.Use("/subApp1", subApp1)
201 app.Use(func(c Ctx) error {
202 return c.Next()
203 })
204 app.Get("/bar", func(c Ctx) error {
205 return c.SendString("ok")
206 })
207 app.Use(func(c Ctx) error {
208 // is overwritten when the positioning is not correct
209 c.Locals("world", "hello")
210 return c.Next()
211 })
212 methods := subApp2.Group("/subApp2")
213 methods.Get("/world", func(c Ctx) error {
214 v, ok := c.Locals("world").(string)
215 if !ok {
216 panic("unexpected data type")
217 }
218 return c.SendString(v)
219 })
220 app.Use("", subApp2)
221 }
222
223 testEndpoint(app, "/subApp2/world", "hello")
224
225 routeStackGET := app.Stack()[0]
226 require.True(t, routeStackGET[0].use)
227 require.Equal(t, "/", routeStackGET[0].path)
228
229 require.True(t, routeStackGET[1].use)
230 require.Equal(t, "/", routeStackGET[1].path)
231
232 require.False(t, routeStackGET[2].use)
233 require.Equal(t, "/bar", routeStackGET[2].path)
234
235 require.True(t, routeStackGET[3].use)
236 require.Equal(t, "/", routeStackGET[3].path)

Callers

nothing calls this directly

Calls 10

TestMethod · 0.80
StackMethod · 0.80
NewFunction · 0.70
UseMethod · 0.65
LocalsMethod · 0.65
NextMethod · 0.65
GetMethod · 0.65
SendStringMethod · 0.65
GroupMethod · 0.65
LenMethod · 0.65

Tested by

no test coverage detected