MCPcopy
hub / github.com/go-chi/chi / TestMuxNestedNotFound

Function TestMuxNestedNotFound

mux_test.go:316–392  ·  mux_test.go::TestMuxNestedNotFound
(t *testing.T)

Source from the content-addressed store, hash-verified

314}
315
316func TestMuxNestedNotFound(t *testing.T) {
317 r := NewRouter()
318
319 r.Use(func(next http.Handler) http.Handler {
320 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
321 r = r.WithContext(context.WithValue(r.Context(), ctxKey{"mw"}, "mw"))
322 next.ServeHTTP(w, r)
323 })
324 })
325
326 r.Get("/hi", func(w http.ResponseWriter, r *http.Request) {
327 w.Write([]byte("bye"))
328 })
329
330 r.With(func(next http.Handler) http.Handler {
331 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
332 r = r.WithContext(context.WithValue(r.Context(), ctxKey{"with"}, "with"))
333 next.ServeHTTP(w, r)
334 })
335 }).NotFound(func(w http.ResponseWriter, r *http.Request) {
336 chkMw := r.Context().Value(ctxKey{"mw"}).(string)
337 chkWith := r.Context().Value(ctxKey{"with"}).(string)
338 w.WriteHeader(404)
339 w.Write([]byte(fmt.Sprintf("root 404 %s %s", chkMw, chkWith)))
340 })
341
342 sr1 := NewRouter()
343
344 sr1.Get("/sub", func(w http.ResponseWriter, r *http.Request) {
345 w.Write([]byte("sub"))
346 })
347 sr1.Group(func(sr1 Router) {
348 sr1.Use(func(next http.Handler) http.Handler {
349 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
350 r = r.WithContext(context.WithValue(r.Context(), ctxKey{"mw2"}, "mw2"))
351 next.ServeHTTP(w, r)
352 })
353 })
354 sr1.NotFound(func(w http.ResponseWriter, r *http.Request) {
355 chkMw2 := r.Context().Value(ctxKey{"mw2"}).(string)
356 w.WriteHeader(404)
357 w.Write([]byte(fmt.Sprintf("sub 404 %s", chkMw2)))
358 })
359 })
360
361 sr2 := NewRouter()
362 sr2.Get("/sub", func(w http.ResponseWriter, r *http.Request) {
363 w.Write([]byte("sub2"))
364 })
365
366 r.Mount("/admin1", sr1)
367 r.Mount("/admin2", sr2)
368
369 ts := httptest.NewServer(r)
370 defer ts.Close()
371
372 if _, body := testRequest(t, ts, "GET", "/hi", nil); body != "bye" {
373 t.Fatal(body)

Callers

nothing calls this directly

Calls 14

UseMethod · 0.95
GetMethod · 0.95
NotFoundMethod · 0.95
WithMethod · 0.95
GroupMethod · 0.95
MountMethod · 0.95
NewRouterFunction · 0.85
HandlerFuncMethod · 0.80
ValueMethod · 0.80
CloseMethod · 0.80
testRequestFunction · 0.70
WriteMethod · 0.65

Tested by

no test coverage detected