MCPcopy
hub / github.com/grafana/dskit / TestRouteInjector

Function TestRouteInjector

middleware/route_injector_test.go:17–48  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

15)
16
17func 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
50func TestMakeLabelValue(t *testing.T) {
51 for input, want := range map[string]string{

Callers

nothing calls this directly

Calls 7

ExtractRouteNameFunction · 0.85
RunMethod · 0.80
NameMethod · 0.65
WrapMethod · 0.65
ContextMethod · 0.45
ServeHTTPMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected