forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrequest_test.go
58 lines (46 loc) · 1.86 KB
/
request_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package middleware
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestCanGetRouteNameFromContext(t *testing.T) {
tcs := []struct {
namedHandler string
path string
expected string
}{
{namedHandler: "", path: "/public/img/apple-touch-icon.png", expected: "public-assets"},
{namedHandler: "", path: "/favicon.ico", expected: "public-assets"},
{namedHandler: "", path: "/robots.txt", expected: "/robots.txt"},
{namedHandler: "", path: "/debug/pprof/heap", expected: "/debug/pprof-handlers"},
{namedHandler: "", path: "/debug/pprof/allocs", expected: "/debug/pprof-handlers"},
{namedHandler: "", path: "/debug/pprof/threadcreate", expected: "/debug/pprof-handlers"},
{namedHandler: "/api/dashboard/:uid", path: "/api/dashboard/ddfgeasdfr", expected: "/api/dashboard/:uid"},
{namedHandler: "", path: "/metrics", expected: "/metrics"},
}
for _, tc := range tcs {
req, err := http.NewRequest(http.MethodPost, "https://ops.grafana.net"+tc.path, nil)
if tc.namedHandler != "" {
req = req.WithContext(context.WithValue(context.Background(), routeOperationNameKey, tc.namedHandler))
}
assert.NoError(t, err)
handler, _ := RouteOperationName(req)
assert.Equal(t, tc.expected, handler)
}
}
func TestOperationNameCanOnlyBeSetOnce(t *testing.T) {
req, _ := http.NewRequestWithContext(context.Background(), http.MethodPost, "https://grafana.com", nil)
// set the initial operation name
req = addRouteNameToContext(req, "first")
// check that the operation name is set correctly
value, exists := RouteOperationName(req)
assert.True(t, exists, "route name should exist")
assert.Equal(t, "first", value)
// check that it cannot be overwritten
req = addRouteNameToContext(req, "second")
value, exists = RouteOperationName(req)
assert.True(t, exists, "route name should exist")
assert.Equal(t, "first", value)
}