Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions docs/ChaosHandlerSamples.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@

> Uses [Custom Middleware Chain](https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CustomMiddlewareChain.md), it's not included in default middleware chain

### Modes in Testing Handler
### Modes in Chaos Handler

- Manual mode - Setting the Response code manually. - Global/Client Level - Provide a map declared manually containing response code for the requests. - Request Level - Providing response code per request. This would be overriding the Global level response code (if any).
- Random mode - We get a random Response code from a set of response code defined for each method.

**A request Passes through to the Graph, if there is no entry for the request**

**Note - Always add ChaosHandler before HttpMessageHandler in the midllewareChain**

### Samples

```js
Expand All @@ -25,16 +29,12 @@ const client = MicrosoftGraph.Client.init({
},
});

// Declaring the Map, containing response codes for the urls
const manualMap = new Map([["/me/messages/.*", new Map([["GET", 429], ["PATCH", 429]])], ["/me", new Map([["POST", 502]])]]);
/*
Create a custom MiddlewareChain passing information in this way

// Declaring the chaosHandler and passing the map (if using map, we have to put default strategy as MANUAL)
const manualMap = new Map([["/me/messages/.*", new Map([["GET", 429], ["PATCH", 429]])], ["/me", new Map([["POST", 502]])]]);
const chaosHandler = new MicrosoftGraph.ChaosHandler(new MicrosoftGraph.ChaosHandlerOptions(MicrosoftGraph.ChaosStrategy.MANUAL), manualMap);

// Modifying the default middleware chain to add chaos handler, just before httpMessageHandler, otherwise there can be a problem
let arr = client.getMiddlewareChain();
arr.splice(arr.length - 1, 0, chaosHandler);
client.setMiddlewareChain(arr);
*/

// This request would use the Map (Manual mode)
const mail = {
Expand Down
38 changes: 1 addition & 37 deletions docs/CustomMiddlewareChain.md
Original file line number Diff line number Diff line change
Expand Up @@ -143,41 +143,5 @@ export class MyLoggingHandler implements Middleware {
}
}
```
Refer [MiddlewareOptions](../src/middleware/options/IMiddlewareOptions.ts) interface to know its structure.

### Modifying the Current Middleware Chain

```js
// initialising client
const client = MicrosoftGraph.Client.init({
defaultVersion: "v1.0",
debugLogging: true,
authProvider: (done) => {
done(null, secrets.accessToken);
},
});

// getting the current middleware chain (in this case, it's the default one)
let arr = client.getMiddlewareChain();

// Initialising the Middleware chain that we created
const dummyRandomHandler = new dummyRandomHandler();

// adding the dummy handler in the array of middlewares at 3rd position
arr.splice(2, 0, dummyRandomHandler);

// setting the new middleware chain
client.setMiddlewareChain(arr);

// calling the api
client
.api("/me")
.select("displayName")
.get()
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
```
Refer [MiddlewareOptions](../src/middleware/options/IMiddlewareOptions.ts) interface to know its structure.
39 changes: 0 additions & 39 deletions spec/core/HTTPClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,9 @@

import { assert } from "chai";

import { Client } from "../../src/Client";
import { HTTPClient } from "../../src/HTTPClient";
import { Context } from "../../src/IContext";
import { FetchOptions } from "../../src/IFetchOptions";
import { RedirectHandlerOptions } from "../../src/middleware/options/RedirectHandlerOptions";
import { RedirectHandler } from "../../src/middleware/RedirectHandler";
import { TelemetryHandler } from "../../src/middleware/TelemetryHandler";
import { DummyHTTPMessageHandler } from "../DummyHTTPMessageHandler";

describe("HTTPClient.ts", () => {
Expand Down Expand Up @@ -72,39 +68,4 @@ describe("HTTPClient.ts", () => {
}
});
});

describe("getMiddlewareArray", () => {
it("Should work fine for a single middleware in the chain, which does have a getNext method", () => {
const telemetryHandler = new TelemetryHandler();
const tempHttpClient: HTTPClient = new HTTPClient(telemetryHandler);
assert.equal(tempHttpClient.getMiddlewareArray().length, 1);
});

it("Should work fine for a single middleware in the chain, which doesn't have a getNext method", () => {
const tempHttpClient: HTTPClient = new HTTPClient(httpMessageHandler);
assert.equal(tempHttpClient.getMiddlewareArray().length, 1);
});

it("Should work fine for a chain containing many middlewares", () => {
const telemetryHandler = new TelemetryHandler();
const redirectHandler = new RedirectHandler(new RedirectHandlerOptions());
redirectHandler.setNext(telemetryHandler);
telemetryHandler.setNext(httpMessageHandler);
const tempHttpClient: HTTPClient = new HTTPClient(redirectHandler);
assert.equal(tempHttpClient.getMiddlewareArray().length, 3);
});
});

describe("setMiddlewareArray", () => {
it("Should make a chain out of the provided array of middlewares", () => {
const telemetryHandler = new TelemetryHandler();
const redirectHandler = new RedirectHandler(new RedirectHandlerOptions());
redirectHandler.setNext(httpMessageHandler);
const tempHttpClient: HTTPClient = new HTTPClient(redirectHandler);
const middlewareArray = tempHttpClient.getMiddlewareArray();
middlewareArray.splice(1, 0, telemetryHandler);
tempHttpClient.setMiddlewareArray(middlewareArray);
assert.equal(tempHttpClient.getMiddlewareArray().length, 3);
});
});
});
19 changes: 0 additions & 19 deletions src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import { HTTPClient } from "./HTTPClient";
import { HTTPClientFactory } from "./HTTPClientFactory";
import { ClientOptions } from "./IClientOptions";
import { Options } from "./IOptions";
import { Middleware } from "./middleware/IMiddleware";
import { validatePolyFilling } from "./ValidatePolyFilling";

export class Client {
Expand Down Expand Up @@ -104,24 +103,6 @@ export class Client {
this.httpClient = httpClient;
}

/**
* @public
* function to get the array of middlewares in use right now
* @returns An array of middlewares
*/
public getMiddlewareChain() {
return this.httpClient.getMiddlewareArray();
}

/**
* @public
* function to set the middleware chain
* @param {Middleware[]} middlewareArray - An array of middlewares
*/
public setMiddlewareChain(middlewareArray: Middleware[]) {
return this.httpClient.setMiddlewareArray(middlewareArray);
}

/**
* @public
* Entry point to make requests
Expand Down
31 changes: 0 additions & 31 deletions src/HTTPClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,37 +33,6 @@ export class HTTPClient {
this.middleware = middleware;
}

/**
* @public
* To get an array of Middleware, used in middleware chain
* @returns An array of middlewares
*/
public getMiddlewareArray(): Middleware[] {
const middlewareArray: Middleware[] = [];
let currentMiddleware = this.middleware;
while (currentMiddleware) {
middlewareArray.push(currentMiddleware);
if (typeof currentMiddleware.getNext !== "undefined") {
currentMiddleware = currentMiddleware.getNext();
} else {
break;
}
}
return middlewareArray;
}

/**
* @public
* To set the middleware chain
* @param {Middleware[]} middlewareArray - The array containing the middlewares
*/
public setMiddlewareArray(middlewareArray: Middleware[]) {
for (let num = 0; num < middlewareArray.length - 1; num += 1) {
middlewareArray[num].setNext(middlewareArray[num + 1]);
}
this.middleware = middlewareArray[0];
}

/**
* @public
* @async
Expand Down
9 changes: 0 additions & 9 deletions src/middleware/AuthenticationHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,4 @@ export class AuthenticationHandler implements Middleware {
public setNext(next: Middleware): void {
this.nextMiddleware = next;
}

/**
* @public
* To get the next middleware in the chain
* @returns next Middleware instance
*/
public getNext(): Middleware {
return this.nextMiddleware;
}
}
9 changes: 0 additions & 9 deletions src/middleware/ChaosHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,4 @@ export class ChaosHandler implements Middleware {
public setNext(next: Middleware): void {
this.nextMiddleware = next;
}

/**
* @public
* To get the next middleware in the chain
* @returns next Middleware instance
*/
public getNext(): Middleware {
return this.nextMiddleware;
}
}
1 change: 0 additions & 1 deletion src/middleware/IMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,4 @@ import { Context } from "../IContext";
export interface Middleware {
execute: (context: Context) => Promise<void>;
setNext?: (middleware: Middleware) => void;
getNext?: () => Middleware;
}
9 changes: 0 additions & 9 deletions src/middleware/RedirectHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,4 @@ export class RedirectHandler implements Middleware {
public setNext(next: Middleware): void {
this.nextMiddleware = next;
}

/**
* @public
* To get the next middleware in the chain
* @returns next Middleware instance
*/
public getNext(): Middleware {
return this.nextMiddleware;
}
}
9 changes: 0 additions & 9 deletions src/middleware/RetryHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,13 +216,4 @@ export class RetryHandler implements Middleware {
public setNext(next: Middleware): void {
this.nextMiddleware = next;
}

/**
* @public
* To get the next middleware in the chain
* @returns next Middleware instance
*/
public getNext(): Middleware {
return this.nextMiddleware;
}
}
9 changes: 0 additions & 9 deletions src/middleware/TelemetryHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,4 @@ export class TelemetryHandler implements Middleware {
public setNext(next: Middleware): void {
this.nextMiddleware = next;
}

/**
* @public
* To get the next middleware in the chain
* @returns next Middleware instance
*/
public getNext(): Middleware {
return this.nextMiddleware;
}
}