forked from microsoftgraph/msgraph-sdk-javascript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTelemetryHandler.ts
More file actions
142 lines (133 loc) · 4.85 KB
/
Copy pathTelemetryHandler.ts
File metadata and controls
142 lines (133 loc) · 4.85 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { assert } from "chai";
import { GRAPH_BASE_URL } from "../../src/Constants";
import { Context } from "../../src/IContext";
import { MiddlewareControl } from "../../src/middleware/MiddlewareControl";
import { FeatureUsageFlag, TelemetryHandlerOptions } from "../../src/middleware/options/TelemetryHandlerOptions";
import { TelemetryHandler } from "../../src/middleware/TelemetryHandler";
import { PACKAGE_VERSION } from "../../src/Version";
import { DummyHTTPMessageHandler } from "../DummyHTTPMessageHandler";
describe("TelemetryHandler.ts", () => {
/* tslint:disable: no-string-literal */
describe("execute", function() {
this.timeout(20 * 1000);
const telemetryHandler = new TelemetryHandler();
const dummyHTTPHandler = new DummyHTTPMessageHandler();
const uuid = "dummy_uuid";
const sdkVersion = "dummy_version";
telemetryHandler.setNext(dummyHTTPHandler);
const okayResponse = new Response("", {
status: 200,
statusText: "OK",
});
it("Should not disturb client-request-id in the header", async () => {
const context: Context = {
request: GRAPH_BASE_URL,
options: {
headers: {
"client-request-id": uuid,
},
},
};
dummyHTTPHandler.setResponses([okayResponse]);
await telemetryHandler.execute(context);
assert.equal(context.options.headers["client-request-id"], uuid);
});
it("Should create client-request-id if one is not present in the request header", async () => {
const context: Context = {
request: "https://GRAPH.microsoft.com:443/",
options: {
headers: {
method: "GET",
},
},
};
dummyHTTPHandler.setResponses([okayResponse]);
await telemetryHandler.execute(context);
assert.isDefined(context.options.headers["client-request-id"]);
});
it("Should set sdk version header without feature flag usage if telemetry options is not present", async () => {
const context: Context = {
request: GRAPH_BASE_URL,
options: {
headers: {
method: "GET",
},
},
};
dummyHTTPHandler.setResponses([okayResponse]);
await telemetryHandler.execute(context);
assert.equal(context.options.headers["SdkVersion"], `graph-js/${PACKAGE_VERSION}`);
});
it("Should set sdk version header with feature flag", async () => {
const telemetryOptions = new TelemetryHandlerOptions();
telemetryOptions["setFeatureUsage"](FeatureUsageFlag.AUTHENTICATION_HANDLER_ENABLED);
const context: Context = {
request: GRAPH_BASE_URL,
options: {
headers: {
method: "GET",
},
},
middlewareControl: new MiddlewareControl([telemetryOptions]),
};
dummyHTTPHandler.setResponses([okayResponse]);
await telemetryHandler.execute(context);
assert.equal(context.options.headers["SdkVersion"], `graph-js/${PACKAGE_VERSION} (featureUsage=${FeatureUsageFlag.AUTHENTICATION_HANDLER_ENABLED.toString(16)})`);
});
it("Should not set telemetry for non-graph url", async () => {
const context: Context = {
request: "test url",
options: {
headers: {
method: "GET",
},
},
middlewareControl: new MiddlewareControl(),
};
dummyHTTPHandler.setResponses([okayResponse]);
await telemetryHandler.execute(context);
assert.equal(context.options.headers["client-request-id"], undefined);
assert.equal(context.options.headers["SdkVersion"], undefined);
assert.equal(context.options.headers["setFeatureUsage"], undefined);
});
it("Should not disturb client-request-id in the header when Request object is passed with Graph URL", async () => {
const request = new Request(GRAPH_BASE_URL);
const context: Context = {
request,
options: {
headers: {
"client-request-id": uuid,
SdkVersion: sdkVersion,
},
},
};
dummyHTTPHandler.setResponses([okayResponse]);
await telemetryHandler.execute(context);
assert.equal(context.options.headers["client-request-id"], uuid);
assert.equal(context.options.headers["SdkVersion"], sdkVersion);
});
it("Should delete Telemetry in the header when Request object is passed with non Graph URL", async () => {
const request = new Request("test_url");
const context: Context = {
request,
options: {
headers: {
"client-request-id": uuid,
SdkVersion: "test_version",
},
},
};
dummyHTTPHandler.setResponses([okayResponse]);
await telemetryHandler.execute(context);
assert.equal(context.options.headers["client-request-id"], undefined);
assert.equal(context.options.headers["SdkVersion"], undefined);
});
});
/* tslint:enable: no-string-literal */
});