Skip to content

Commit c1b957f

Browse files
nikithaucbaywet
andauthored
Checking if the request object url is a graph url (#357)
* Checking if the request object url is a graph url * removing ununsed const * Update src/middleware/TelemetryHandler.ts Co-authored-by: Vincent Biret <[email protected]> Co-authored-by: Vincent Biret <[email protected]>
1 parent 3372ee0 commit c1b957f

File tree

2 files changed

+58
-24
lines changed

2 files changed

+58
-24
lines changed

spec/middleware/TelemetryHandler.ts

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@ describe("TelemetryHandler.ts", () => {
2121
this.timeout(20 * 1000);
2222
const telemetryHandler = new TelemetryHandler();
2323
const dummyHTTPHandler = new DummyHTTPMessageHandler();
24+
const uuid = "dummy_uuid";
25+
const sdkVersion = "dummy_version";
2426
telemetryHandler.setNext(dummyHTTPHandler);
2527
const okayResponse = new Response("", {
2628
status: 200,
2729
statusText: "OK",
2830
});
2931
it("Should not disturb client-request-id in the header", async () => {
30-
const uuid = "dummy_uuid";
3132
const context: Context = {
3233
request: GRAPH_BASE_URL,
3334
options: {
@@ -102,6 +103,40 @@ describe("TelemetryHandler.ts", () => {
102103
assert.equal(context.options.headers["SdkVersion"], undefined);
103104
assert.equal(context.options.headers["setFeatureUsage"], undefined);
104105
});
106+
107+
it("Should not disturb client-request-id in the header when Request object is passed with Graph URL", async () => {
108+
const request = new Request(GRAPH_BASE_URL);
109+
const context: Context = {
110+
request,
111+
options: {
112+
headers: {
113+
"client-request-id": uuid,
114+
SdkVersion: sdkVersion,
115+
},
116+
},
117+
};
118+
dummyHTTPHandler.setResponses([okayResponse]);
119+
await telemetryHandler.execute(context);
120+
assert.equal(context.options.headers["client-request-id"], uuid);
121+
assert.equal(context.options.headers["SdkVersion"], sdkVersion);
122+
});
123+
124+
it("Should delete Telemetry in the header when Request object is passed with non Graph URL", async () => {
125+
const request = new Request("test_url");
126+
const context: Context = {
127+
request,
128+
options: {
129+
headers: {
130+
"client-request-id": uuid,
131+
SdkVersion: "test_version",
132+
},
133+
},
134+
};
135+
dummyHTTPHandler.setResponses([okayResponse]);
136+
await telemetryHandler.execute(context);
137+
assert.equal(context.options.headers["client-request-id"], undefined);
138+
assert.equal(context.options.headers["SdkVersion"], undefined);
139+
});
105140
});
106141
/* tslint:enable: no-string-literal */
107142
});

src/middleware/TelemetryHandler.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,29 @@ export class TelemetryHandler implements Middleware {
6666
*/
6767
public async execute(context: Context): Promise<void> {
6868
try {
69-
if (typeof context.request === "string") {
70-
if (isGraphURL(context.request)) {
71-
// Add telemetry only if the request url is a Graph URL.
72-
// Errors are reported as in issue #265 if headers are present when redirecting to a non Graph URL
73-
let clientRequestId: string = getRequestHeader(context.request, context.options, TelemetryHandler.CLIENT_REQUEST_ID_HEADER);
74-
if (clientRequestId === null) {
75-
clientRequestId = generateUUID();
76-
setRequestHeader(context.request, context.options, TelemetryHandler.CLIENT_REQUEST_ID_HEADER, clientRequestId);
77-
}
78-
let sdkVersionValue: string = `${TelemetryHandler.PRODUCT_NAME}/${PACKAGE_VERSION}`;
79-
let options: TelemetryHandlerOptions;
80-
if (context.middlewareControl instanceof MiddlewareControl) {
81-
options = context.middlewareControl.getMiddlewareOptions(TelemetryHandlerOptions) as TelemetryHandlerOptions;
82-
}
83-
if (options) {
84-
const featureUsage: string = options.getFeatureUsage();
85-
sdkVersionValue += ` (${TelemetryHandler.FEATURE_USAGE_STRING}=${featureUsage})`;
86-
}
87-
appendRequestHeader(context.request, context.options, TelemetryHandler.SDK_VERSION_HEADER, sdkVersionValue);
88-
} else {
89-
// Remove telemetry headers if present during redirection.
90-
delete context.options.headers[TelemetryHandler.CLIENT_REQUEST_ID_HEADER];
91-
delete context.options.headers[TelemetryHandler.SDK_VERSION_HEADER];
69+
const url = typeof context.request === "string" ? context.request : context.request.url;
70+
if (isGraphURL(url)) {
71+
// Add telemetry only if the request url is a Graph URL.
72+
// Errors are reported as in issue #265 if headers are present when redirecting to a non Graph URL
73+
let clientRequestId: string = getRequestHeader(context.request, context.options, TelemetryHandler.CLIENT_REQUEST_ID_HEADER);
74+
if (!clientRequestId) {
75+
clientRequestId = generateUUID();
76+
setRequestHeader(context.request, context.options, TelemetryHandler.CLIENT_REQUEST_ID_HEADER, clientRequestId);
9277
}
78+
let sdkVersionValue: string = `${TelemetryHandler.PRODUCT_NAME}/${PACKAGE_VERSION}`;
79+
let options: TelemetryHandlerOptions;
80+
if (context.middlewareControl instanceof MiddlewareControl) {
81+
options = context.middlewareControl.getMiddlewareOptions(TelemetryHandlerOptions) as TelemetryHandlerOptions;
82+
}
83+
if (options) {
84+
const featureUsage: string = options.getFeatureUsage();
85+
sdkVersionValue += ` (${TelemetryHandler.FEATURE_USAGE_STRING}=${featureUsage})`;
86+
}
87+
appendRequestHeader(context.request, context.options, TelemetryHandler.SDK_VERSION_HEADER, sdkVersionValue);
88+
} else {
89+
// Remove telemetry headers if present during redirection.
90+
delete context.options.headers[TelemetryHandler.CLIENT_REQUEST_ID_HEADER];
91+
delete context.options.headers[TelemetryHandler.SDK_VERSION_HEADER];
9392
}
9493
return await this.nextMiddleware.execute(context);
9594
} catch (error) {

0 commit comments

Comments
 (0)