Uses Custom Middleware Chain, it's not included in default middleware chain
- 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 middlewareChain
require("isomorphic-fetch");
const MicrosoftGraph = require("../../lib/src/index.js");
const secrets = require("./secrets");
const fs = require("fs");
// Initialising the client
const client = MicrosoftGraph.Client.init({
defaultVersion: "v1.0",
debugLogging: true,
authProvider: (done) => {
done(null, secrets.accessToken);
},
});
/*
Create a custom MiddlewareChain passing information in this way
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);
*/
// This request would use the Map (Manual mode)
const mail = {
subject: "Chaos Handler Samples",
toRecipients: [
{
emailAddress: {
address: "[email protected]",
},
},
],
body: {
content: "<h1>Testing Handler Samples Sample</h1><br>https://github.com/microsoftgraph/msgraph-sdk-javascript",
contentType: "html",
},
};
client
.api("/users/me/sendMail")
.post({
message: mail,
})
.then((res) => {
console.log(res, "This is for sendMail");
})
.catch((err) => {
console.log(err, "This is for sendMail in error case");
});
// OverRiding to Random mode, providing the chaos percentage as 60(percentage times the error would be generated from handler)
client
.api("/me")
.middlewareOptions([new MicrosoftGraph.ChaosHandlerOptions(MicrosoftGraph.ChaosStrategy.RANDOM, "I generated the error", undefined, 60)])
.get()
.then((res) => {
console.log(res);
})
.catch((err) => {
console.log(err);
});
// This request is passed to the graph and gets a response from the graph, as no entry for /me GET request in the Map
client
.api("/me")
.get()
.then((res) => {
console.log("Found", res, "users");
})
.catch((err) => {
console.log(err, "!!!!!!!!!");
});
// Using Manual Map with regex matching
client
.api("/me/messages/hjdlfslod-fdssdkjfs-6zdkmghs-sadhsu2")
.header("content-type", "application/json")
.update({
birthday: "1908-12-22T00:00:00Z",
})
.then((res) => {
console.log("This is regex matching... Updated Bday");
})
.catch((err) => {
console.log(err, "matched");
});