MCPcopy
hub / github.com/gorilla/mux / TestMethodsSubrouterSubrouter

Function TestMethodsSubrouterSubrouter

mux_test.go:2352–2406  ·  view source on GitHub ↗

TestMethodsSubrouterSubrouter matches handlers on subrouters produced from method matchers registered on a root subrouter.

(t *testing.T)

Source from the content-addressed store, hash-verified

2350// TestMethodsSubrouterSubrouter matches handlers on subrouters produced
2351// from method matchers registered on a root subrouter.
2352func TestMethodsSubrouterSubrouter(t *testing.T) {
2353 t.Parallel()
2354
2355 router := NewRouter()
2356 sub := router.PathPrefix("/1").Subrouter()
2357 sub.Methods("POST").Subrouter().HandleFunc("/2", methodHandler("POST"))
2358 sub.Methods("GET").Subrouter().HandleFunc("/2", methodHandler("GET"))
2359 sub.Methods("PATCH").Subrouter().HandleFunc("/2", methodHandler("PATCH"))
2360 sub.HandleFunc("/2", methodHandler("PUT")).Subrouter().Methods("PUT")
2361 sub.HandleFunc("/2", methodHandler("POST2")).Subrouter().Methods("POST")
2362
2363 tests := []methodsSubrouterTest{
2364 {
2365 title: "match first POST handler",
2366 router: router,
2367 method: "POST",
2368 path: "http://localhost/1/2",
2369 wantCode: http.StatusOK,
2370 },
2371 {
2372 title: "match GET handler",
2373 router: router,
2374 method: "GET",
2375 path: "http://localhost/1/2",
2376 wantCode: http.StatusOK,
2377 },
2378 {
2379 title: "match PATCH handler",
2380 router: router,
2381 method: "PATCH",
2382 path: "http://localhost/1/2",
2383 wantCode: http.StatusOK,
2384 },
2385 {
2386 title: "match PUT handler",
2387 router: router,
2388 method: "PUT",
2389 path: "http://localhost/1/2",
2390 wantCode: http.StatusOK,
2391 },
2392 {
2393 title: "disallow DELETE method",
2394 router: router,
2395 method: "DELETE",
2396 path: "http://localhost/1/2",
2397 wantCode: http.StatusMethodNotAllowed,
2398 },
2399 }
2400
2401 for _, test := range tests {
2402 t.Run(test.title, func(t *testing.T) {
2403 testMethodsSubrouter(t, test)
2404 })
2405 }
2406}
2407
2408// TestMethodsSubrouterPathVariable matches handlers on matching paths
2409// with path variables in them.

Callers

nothing calls this directly

Calls 7

PathPrefixMethod · 0.95
NewRouterFunction · 0.85
methodHandlerFunction · 0.85
testMethodsSubrouterFunction · 0.85
SubrouterMethod · 0.80
HandleFuncMethod · 0.80
MethodsMethod · 0.45

Tested by

no test coverage detected