| 15 | ) |
| 16 | |
| 17 | func TestRouteInjector(t *testing.T) { |
| 18 | testCases := map[string]string{ |
| 19 | "/": "root", |
| 20 | "/foo/bar/blah": "foo_bar_blah", |
| 21 | "/templated/name-1/thing": "templated_name_thing", |
| 22 | "/named": "my-named-route", |
| 23 | "/does-not-exist": "notfound", |
| 24 | } |
| 25 | |
| 26 | for url, expectedRouteName := range testCases { |
| 27 | t.Run(url, func(t *testing.T) { |
| 28 | actualRouteName := "" |
| 29 | |
| 30 | handler := func(_ http.ResponseWriter, r *http.Request) { |
| 31 | actualRouteName = ExtractRouteName(r.Context()) |
| 32 | } |
| 33 | |
| 34 | router := mux.NewRouter() |
| 35 | router.HandleFunc("/", handler) |
| 36 | router.HandleFunc("/foo/bar/blah", handler) |
| 37 | router.HandleFunc("/templated/{name}/thing", handler) |
| 38 | router.HandleFunc("/named", handler).Name("my-named-route") |
| 39 | router.NotFoundHandler = http.HandlerFunc(handler) |
| 40 | |
| 41 | endpoint := RouteInjector{router}.Wrap(router) |
| 42 | endpoint.ServeHTTP(httptest.NewRecorder(), httptest.NewRequest(http.MethodGet, url, nil)) |
| 43 | |
| 44 | require.Equal(t, expectedRouteName, actualRouteName) |
| 45 | }) |
| 46 | } |
| 47 | |
| 48 | } |
| 49 | |
| 50 | func TestMakeLabelValue(t *testing.T) { |
| 51 | for input, want := range map[string]string{ |