-
Notifications
You must be signed in to change notification settings - Fork 9
/
graph_telemetry_handler_test.go
101 lines (93 loc) · 3.07 KB
/
graph_telemetry_handler_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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package msgraphgocore
import (
nethttp "net/http"
httptest "net/http/httptest"
testing "testing"
assert "github.com/stretchr/testify/assert"
)
type NoopPipeline struct {
client *nethttp.Client
}
func (pipeline *NoopPipeline) Next(req *nethttp.Request, middlewareIndex int) (*nethttp.Response, error) {
return pipeline.client.Do(req)
}
func newNoopPipeline() *NoopPipeline {
return &NoopPipeline{
client: nethttp.DefaultClient,
}
}
func TestItCreatesANewHandler(t *testing.T) {
handler := NewGraphTelemetryHandler(&GraphClientOptions{})
if handler == nil {
t.Error("handler is nil")
}
}
func TestItAddsHeaders(t *testing.T) {
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
res.WriteHeader(200)
res.Write([]byte("body"))
}))
defer func() { testServer.Close() }()
handler := NewGraphTelemetryHandler(&GraphClientOptions{})
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
resp, err := handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
sdkVersionHeaderValue := req.Header[nethttp.CanonicalHeaderKey("SdkVersion")]
assert.NotEmpty(t, sdkVersionHeaderValue)
assert.Contains(t, sdkVersionHeaderValue[0], "graph-go-core")
assert.Contains(t, sdkVersionHeaderValue[0], "hostOS")
assert.Contains(t, sdkVersionHeaderValue[0], "hostArch")
assert.Contains(t, sdkVersionHeaderValue[0], "runtimeEnvironment")
assert.NotEmpty(t, req.Header[nethttp.CanonicalHeaderKey("client-request-id")])
}
func TestItAddsServiceLibInfo(t *testing.T) {
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
res.WriteHeader(200)
res.Write([]byte("body"))
}))
defer func() { testServer.Close() }()
handler := NewGraphTelemetryHandler(&GraphClientOptions{
GraphServiceLibraryVersion: "1.0.0",
})
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
resp, err := handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
sdkVersionHeaderValue := req.Header[nethttp.CanonicalHeaderKey("SdkVersion")]
assert.NotEmpty(t, sdkVersionHeaderValue)
assert.Contains(t, sdkVersionHeaderValue[0], "graph-go/")
}
func TestItAddsServiceInfo(t *testing.T) {
testServer := httptest.NewServer(nethttp.HandlerFunc(func(res nethttp.ResponseWriter, req *nethttp.Request) {
res.WriteHeader(200)
res.Write([]byte("body"))
}))
defer func() { testServer.Close() }()
handler := NewGraphTelemetryHandler(&GraphClientOptions{
GraphServiceLibraryVersion: "1.0.0",
GraphServiceVersion: "v1",
})
req, err := nethttp.NewRequest(nethttp.MethodGet, testServer.URL, nil)
if err != nil {
t.Error(err)
}
resp, err := handler.Intercept(newNoopPipeline(), 0, req)
if err != nil {
t.Error(err)
}
assert.NotNil(t, resp)
sdkVersionHeaderValue := req.Header[nethttp.CanonicalHeaderKey("SdkVersion")]
assert.NotEmpty(t, sdkVersionHeaderValue)
assert.Contains(t, sdkVersionHeaderValue[0], "graph-go-v1/")
}