|
| 1 | +# Testing Handler |
| 2 | + |
| 3 | +### How to include |
| 4 | + |
| 5 | +> Uses [Custom Middleware Chain](https://github.com/microsoftgraph/msgraph-sdk-javascript/blob/dev/docs/CustomMiddlewareChain.md), it's not included in default middleware chain |
| 6 | +
|
| 7 | +### Modes in Chaos Handler |
| 8 | + |
| 9 | +- 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). |
| 10 | +- Random mode - We get a random Response code from a set of response code defined for each method. |
| 11 | + |
| 12 | +**A request Passes through to the Graph, if there is no entry for the request** |
| 13 | + |
| 14 | +**Note - Always add ChaosHandler before HttpMessageHandler in the midllewareChain** |
| 15 | + |
| 16 | +### Samples |
| 17 | + |
| 18 | +```js |
| 19 | +require("isomorphic-fetch"); |
| 20 | +const MicrosoftGraph = require("../../lib/src/index.js"); |
| 21 | +const secrets = require("./secrets"); |
| 22 | +const fs = require("fs"); |
| 23 | +// Initialising the client |
| 24 | +const client = MicrosoftGraph.Client.init({ |
| 25 | + defaultVersion: "v1.0", |
| 26 | + debugLogging: true, |
| 27 | + authProvider: (done) => { |
| 28 | + done(null, secrets.accessToken); |
| 29 | + }, |
| 30 | +}); |
| 31 | + |
| 32 | +/* |
| 33 | +Create a custom MiddlewareChain passing information in this way |
| 34 | +
|
| 35 | +const manualMap = new Map([["/me/messages/.*", new Map([["GET", 429], ["PATCH", 429]])], ["/me", new Map([["POST", 502]])]]); |
| 36 | +const chaosHandler = new MicrosoftGraph.ChaosHandler(new MicrosoftGraph.ChaosHandlerOptions(MicrosoftGraph.ChaosStrategy.MANUAL), manualMap); |
| 37 | +*/ |
| 38 | + |
| 39 | +// This request would use the Map (Manual mode) |
| 40 | +const mail = { |
| 41 | + subject: "Chaos Handler Samples", |
| 42 | + toRecipients: [ |
| 43 | + { |
| 44 | + emailAddress: { |
| 45 | + address: "admin@M365x003297.OnMicrosoft.com", |
| 46 | + }, |
| 47 | + }, |
| 48 | + ], |
| 49 | + body: { |
| 50 | + content: "<h1>Testing Handler Samples Sample</h1><br>https://github.com/microsoftgraph/msgraph-sdk-javascript", |
| 51 | + contentType: "html", |
| 52 | + }, |
| 53 | +}; |
| 54 | +client |
| 55 | + .api("/users/me/sendMail") |
| 56 | + .post({ |
| 57 | + message: mail, |
| 58 | + }) |
| 59 | + .then((res) => { |
| 60 | + console.log(res, "This is for sendMail"); |
| 61 | + }) |
| 62 | + .catch((err) => { |
| 63 | + console.log(err, "This is for sendMail in error case"); |
| 64 | + }); |
| 65 | + |
| 66 | +// OverRiding to Random mode, providing the chaos percentage as 60(percentage times the error would be generated from handler) |
| 67 | +client |
| 68 | + .api("/me") |
| 69 | + .middlewareOptions([new MicrosoftGraph.ChaosHandlerOptions(MicrosoftGraph.ChaosStrategy.RANDOM, "I generated the error", undefined, 60)]) |
| 70 | + .get() |
| 71 | + .then((res) => { |
| 72 | + console.log(res); |
| 73 | + }) |
| 74 | + .catch((err) => { |
| 75 | + console.log(err); |
| 76 | + }); |
| 77 | + |
| 78 | +// This request is passed to the graph and gets a response from the graph, as no entry for /me GET request in the Map |
| 79 | +client |
| 80 | + .api("/me") |
| 81 | + .get() |
| 82 | + .then((res) => { |
| 83 | + console.log("Found", res, "users"); |
| 84 | + }) |
| 85 | + .catch((err) => { |
| 86 | + console.log(err, "!!!!!!!!!"); |
| 87 | + }); |
| 88 | + |
| 89 | +// Using Manual Map with regex matching |
| 90 | +client |
| 91 | + .api("/me/messages/hjdlfslod-fdssdkjfs-6zdkmghs-sadhsu2") |
| 92 | + .header("content-type", "application/json") |
| 93 | + .update({ |
| 94 | + birthday: "1908-12-22T00:00:00Z", |
| 95 | + }) |
| 96 | + .then((res) => { |
| 97 | + console.log("This is regex matching... Updated Bday"); |
| 98 | + }) |
| 99 | + .catch((err) => { |
| 100 | + console.log(err, "matched"); |
| 101 | + }); |
| 102 | +``` |
0 commit comments