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

Function TestMethodsSubrouterPathVariable

mux_test.go:2410–2462  ·  mux_test.go::TestMethodsSubrouterPathVariable

TestMethodsSubrouterPathVariable matches handlers on matching paths with path variables in them.

(t *testing.T)

Source from the content-addressed store, hash-verified

2408// TestMethodsSubrouterPathVariable matches handlers on matching paths
2409// with path variables in them.
2410func TestMethodsSubrouterPathVariable(t *testing.T) {
2411 t.Parallel()
2412
2413 router := NewRouter()
2414 router.Methods("GET").Subrouter().HandleFunc("/foo", methodHandler("GET"))
2415 router.Methods("POST").Subrouter().HandleFunc("/{any}", methodHandler("POST"))
2416 router.Methods("DELETE").Subrouter().HandleFunc("/1/{any}", methodHandler("DELETE"))
2417 router.Methods("PUT").Subrouter().HandleFunc("/1/{any}", methodHandler("PUT"))
2418
2419 tests := []methodsSubrouterTest{
2420 {
2421 title: "match GET handler",
2422 router: router,
2423 method: "GET",
2424 path: "http://localhost/foo",
2425 wantCode: http.StatusOK,
2426 },
2427 {
2428 title: "match POST handler",
2429 router: router,
2430 method: "POST",
2431 path: "http://localhost/foo",
2432 wantCode: http.StatusOK,
2433 },
2434 {
2435 title: "match DELETE handler",
2436 router: router,
2437 method: "DELETE",
2438 path: "http://localhost/1/foo",
2439 wantCode: http.StatusOK,
2440 },
2441 {
2442 title: "match PUT handler",
2443 router: router,
2444 method: "PUT",
2445 path: "http://localhost/1/foo",
2446 wantCode: http.StatusOK,
2447 },
2448 {
2449 title: "disallow PATCH method",
2450 router: router,
2451 method: "PATCH",
2452 path: "http://localhost/1/foo",
2453 wantCode: http.StatusMethodNotAllowed,
2454 },
2455 }
2456
2457 for _, test := range tests {
2458 t.Run(test.title, func(t *testing.T) {
2459 testMethodsSubrouter(t, test)
2460 })
2461 }
2462}
2463
2464func ExampleSetURLVars() {
2465 req, _ := http.NewRequest("GET", "/foo", nil)

Callers

nothing calls this directly

Calls 6

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

Tested by

no test coverage detected