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' ;
1018import { BaseGraph } from '../BaseGraph' ;
1119import { MgtBaseComponent } from '../components/baseComponent' ;
1220import { 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
2240export 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