Skip to content

Commit 65de956

Browse files
committed
fixed escape issue in mock graph client
1 parent a433043 commit 65de956

File tree

3 files changed

+85
-33
lines changed

3 files changed

+85
-33
lines changed

src/BaseGraph.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { Client, GraphRequest, MiddlewareOptions, ResponseType } from '@microsoft/microsoft-graph-client';
8+
import { Client, GraphRequest, Middleware, MiddlewareOptions, ResponseType } from '@microsoft/microsoft-graph-client';
99
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
1010
import * as MicrosoftGraphBeta from '@microsoft/microsoft-graph-types-beta';
1111
import { MgtBaseComponent } from './components/baseComponent';
@@ -600,6 +600,27 @@ export abstract class BaseGraph {
600600
.delete();
601601
}
602602

603+
/**
604+
* Helper method to chain Middleware when instantiating new Client
605+
*
606+
* @protected
607+
* @param {...Middleware[]} middleware
608+
* @returns {Middleware}
609+
* @memberof BaseGraph
610+
*/
611+
protected chainMiddleware(...middleware: Middleware[]): Middleware {
612+
const rootMiddleware = middleware[0];
613+
let current = rootMiddleware;
614+
for (let i = 1; i < middleware.length; ++i) {
615+
const next = middleware[i];
616+
if (current.setNext) {
617+
current.setNext(next);
618+
}
619+
current = next;
620+
}
621+
return rootMiddleware;
622+
}
623+
603624
private blobToBase64(blob: Blob): Promise<string> {
604625
return new Promise((resolve, reject) => {
605626
const reader = new FileReader();

src/Graph.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -62,17 +62,4 @@ export class Graph extends BaseGraph {
6262
graph.componentName = component.tagName.toLowerCase();
6363
return graph;
6464
}
65-
66-
private chainMiddleware(...middleware: Middleware[]): Middleware {
67-
const rootMiddleware = middleware[0];
68-
let current = rootMiddleware;
69-
for (let i = 1; i < middleware.length; ++i) {
70-
const next = middleware[i];
71-
if (current.setNext) {
72-
current.setNext(next);
73-
}
74-
current = next;
75-
}
76-
return rootMiddleware;
77-
}
7865
}

src/mock/MockGraph.ts

Lines changed: 63 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,30 @@
55
* -------------------------------------------------------------------------------------------
66
*/
77

8-
import { Client } from '@microsoft/microsoft-graph-client';
9-
import * as MicrosoftGraph from '@microsoft/microsoft-graph-types';
8+
import {
9+
AuthenticationHandler,
10+
Client,
11+
Context,
12+
HTTPMessageHandler,
13+
Middleware,
14+
RetryHandler,
15+
RetryHandlerOptions,
16+
TelemetryHandler
17+
} from '@microsoft/microsoft-graph-client';
1018
import { BaseGraph } from '../BaseGraph';
1119
import { MgtBaseComponent } from '../components/baseComponent';
1220
import { MockProvider } from './MockProvider';
1321

22+
/**
23+
* The base URL for the mock endpoint
24+
*/
25+
const BASE_URL = 'https://proxy.apisandbox.msdn.microsoft.com/svc?url=';
26+
27+
/**
28+
* The base URL for the graph
29+
*/
30+
const ROOT_GRAPH_URL = 'https://graph.microsoft.com/';
31+
1432
/**
1533
* MockGraph Instance
1634
*
@@ -20,15 +38,20 @@ import { MockProvider } from './MockProvider';
2038
*/
2139
// tslint:disable-next-line: max-classes-per-file
2240
export class MockGraph extends BaseGraph {
23-
private static readonly BASE_URL = 'https://proxy.apisandbox.msdn.microsoft.com/svc?url=';
24-
private static readonly ROOT_GRAPH_URL: string = 'https://graph.microsoft.com/';
25-
2641
constructor(mockProvider: MockProvider) {
2742
super();
2843

44+
const middleware: Middleware[] = [
45+
new AuthenticationHandler(mockProvider),
46+
new RetryHandler(new RetryHandlerOptions()),
47+
new TelemetryHandler(),
48+
new MockMiddleware(),
49+
new HTTPMessageHandler()
50+
];
51+
2952
this.client = Client.initWithMiddleware({
30-
authProvider: mockProvider,
31-
baseUrl: MockGraph.BASE_URL + escape(MockGraph.ROOT_GRAPH_URL)
53+
baseUrl: BASE_URL + ROOT_GRAPH_URL,
54+
middleware: this.chainMiddleware(...middleware)
3255
});
3356
}
3457

@@ -44,21 +67,42 @@ export class MockGraph extends BaseGraph {
4467
// The MockGraph isn't making real Graph requests, so we can simply no-op and return the same instance.
4568
return this;
4669
}
70+
}
4771

72+
/**
73+
* Implements Middleware for the Mock Client to escape
74+
* the graph url from the request
75+
*
76+
* @class MockMiddleware
77+
* @implements {Middleware}
78+
*/
79+
// tslint:disable-next-line: max-classes-per-file
80+
class MockMiddleware implements Middleware {
4881
/**
49-
* get events for Calendar
50-
*
51-
* @param {Date} startDateTime
52-
* @param {Date} endDateTime
53-
* @returns {Promise<MicrosoftGraph.Event[]>}
54-
* @memberof MockGraph
82+
* @private
83+
* A member to hold next middleware in the middleware chain
5584
*/
56-
public async getEvents(startDateTime: Date, endDateTime: Date): Promise<MicrosoftGraph.Event[]> {
57-
const sdt = `startdatetime=${startDateTime.toISOString()}`;
58-
const edt = `enddatetime=${endDateTime.toISOString()}`;
59-
const uri = `/me/calendarview?${sdt}&${edt}`;
85+
private _nextMiddleware: Middleware;
86+
87+
// tslint:disable-next-line: completed-docs
88+
public async execute(context: Context): Promise<void> {
89+
try {
90+
const url = context.request as string;
91+
const baseLength = BASE_URL.length;
92+
context.request = url.substring(0, baseLength) + escape(url.substring(baseLength));
6093

61-
const calendarView = await this.client.api(escape(uri)).get();
62-
return calendarView ? calendarView.value : null;
94+
return await this._nextMiddleware.execute(context);
95+
} catch (error) {
96+
throw error;
97+
}
98+
}
99+
/**
100+
* Handles setting of next middleware
101+
*
102+
* @param {Middleware} next
103+
* @memberof SdkVersionMiddleware
104+
*/
105+
public setNext(next: Middleware): void {
106+
this._nextMiddleware = next;
63107
}
64108
}

0 commit comments

Comments
 (0)