|
| 1 | +import { assert } from "chai"; |
| 2 | + |
| 3 | +import { Context } from "../../src/IContext"; |
| 4 | +import { ChaosHandler } from "../../src/middleware/ChaosHandler"; |
| 5 | +import { MiddlewareControl } from "../../src/middleware/MiddlewareControl"; |
| 6 | +import { ChaosHandlerOptions } from "../../src/middleware/options/ChaosHandlerOptions"; |
| 7 | +import { ChaosStrategy } from "../../src/middleware/options/ChaosStrategy"; |
| 8 | +import { RequestMethod } from "../../src/RequestMethod"; |
| 9 | +import { DummyHTTPMessageHandler } from "../DummyHTTPMessageHandler"; |
| 10 | + |
| 11 | +const chaosHandlerOptions = new ChaosHandlerOptions(); |
| 12 | +const chaosHandler = new ChaosHandler(); |
| 13 | + |
| 14 | +describe("ChaosHandler.ts", () => { |
| 15 | + /* tslint:disable: no-string-literal */ |
| 16 | + describe("constructor", () => { |
| 17 | + it("Should create an instance with given options", () => { |
| 18 | + const handler = new ChaosHandler(chaosHandlerOptions); |
| 19 | + assert.isDefined(handler["options"]); |
| 20 | + }); |
| 21 | + |
| 22 | + it("Should create an instance with default set of options", () => { |
| 23 | + const handler = new ChaosHandler(); |
| 24 | + assert.isDefined(handler["options"]); |
| 25 | + }); |
| 26 | + }); |
| 27 | + |
| 28 | + describe("createResponseHeaders", () => { |
| 29 | + it("Should have request-id for every random statusCode", () => { |
| 30 | + const responseHeader = chaosHandler["createResponseHeaders"](204, "xxxxxxxxxxxxxxxx", new Date().toString()); |
| 31 | + assert.isDefined(responseHeader.get("request-id")); |
| 32 | + }); |
| 33 | + |
| 34 | + it("Should have retry-after for 429 case", () => { |
| 35 | + const responseHeader = chaosHandler["createResponseHeaders"](429, "xxxxxxxxxxxxxxxx", new Date().toString()); |
| 36 | + assert.isDefined(responseHeader.get("retry-after")); |
| 37 | + }); |
| 38 | + }); |
| 39 | + |
| 40 | + describe("createResponseBody", () => { |
| 41 | + it("Should return error in response body for error scenarios", () => { |
| 42 | + const responseBody = chaosHandler["createResponseBody"](404, "Not Found", "xxxxxxxxxxxxxx", new Date().toString()); |
| 43 | + assert.isDefined(responseBody["error"]); |
| 44 | + }); |
| 45 | + |
| 46 | + it("Should return empty response body for success scenarios", () => { |
| 47 | + const responseBody = chaosHandler["createResponseBody"](200, "Not Found", "xxxxxxxxxxxxxx", new Date().toString()); |
| 48 | + assert.equal(Object.keys(responseBody).length, 0); |
| 49 | + }); |
| 50 | + }); |
| 51 | + |
| 52 | + describe("createResponse", () => { |
| 53 | + const cxt: Context = { |
| 54 | + request: "https://graph.microsoft.com/v1.0/me", |
| 55 | + options: { |
| 56 | + method: "GET", |
| 57 | + }, |
| 58 | + }; |
| 59 | + |
| 60 | + it("Should return a valid response object for MANUAL case", () => { |
| 61 | + chaosHandler["createResponse"](new ChaosHandlerOptions(ChaosStrategy.MANUAL, 404), cxt); |
| 62 | + assert.isDefined(cxt.response); |
| 63 | + }); |
| 64 | + |
| 65 | + it("Should return a valid response object for RANDOM case", () => { |
| 66 | + chaosHandler["createResponse"](new ChaosHandlerOptions(ChaosStrategy.RANDOM), cxt); |
| 67 | + assert.isDefined(cxt.response); |
| 68 | + }); |
| 69 | + }); |
| 70 | + |
| 71 | + describe("sendRequest", async () => { |
| 72 | + const cxt: Context = { |
| 73 | + request: "https://graph.microsoft.com/v1.0/me", |
| 74 | + options: { |
| 75 | + method: "GET", |
| 76 | + }, |
| 77 | + }; |
| 78 | + |
| 79 | + const manualMap: Map<string, Map<string, number>> = new Map([["/me", new Map([["GET", 500]])]]); |
| 80 | + const tempManualOptions: ChaosHandlerOptions = new ChaosHandlerOptions(ChaosStrategy.MANUAL); |
| 81 | + const tempChaosHandler = new ChaosHandler(tempManualOptions, manualMap); |
| 82 | + |
| 83 | + const dummyHTTPHandler = new DummyHTTPMessageHandler(); |
| 84 | + const handler = new ChaosHandler(); |
| 85 | + handler.setNext(dummyHTTPHandler); |
| 86 | + |
| 87 | + it("Should return a response after creating it", async () => { |
| 88 | + tempChaosHandler["sendRequest"](tempManualOptions, cxt); |
| 89 | + assert.isDefined(cxt.response); |
| 90 | + }); |
| 91 | + |
| 92 | + it("Should send the request to the graph", async () => { |
| 93 | + handler["sendRequest"](new ChaosHandlerOptions(ChaosStrategy.RANDOM, undefined, "I generated the error", 100), cxt); |
| 94 | + assert.isDefined(cxt.response); |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("getRandomStatusCode", () => { |
| 99 | + it("Should return a status code for GET method", () => { |
| 100 | + assert.isDefined(chaosHandler["getRandomStatusCode"](RequestMethod.GET)); |
| 101 | + }); |
| 102 | + |
| 103 | + it("Should return a status code for POST method", () => { |
| 104 | + assert.isDefined(chaosHandler["getRandomStatusCode"](RequestMethod.POST)); |
| 105 | + }); |
| 106 | + |
| 107 | + it("Should return a status code for PUT method", () => { |
| 108 | + assert.isDefined(chaosHandler["getRandomStatusCode"](RequestMethod.PUT)); |
| 109 | + }); |
| 110 | + |
| 111 | + it("Should return a status code for PATCH method", () => { |
| 112 | + assert.isDefined(chaosHandler["getRandomStatusCode"](RequestMethod.PATCH)); |
| 113 | + }); |
| 114 | + |
| 115 | + it("Should return a status code for DELETE method", () => { |
| 116 | + assert.isDefined(chaosHandler["getRandomStatusCode"](RequestMethod.DELETE)); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + describe("getRelativeURL", () => { |
| 121 | + it("Should return a relative URL for the complete URL", () => { |
| 122 | + assert.equal(chaosHandler["getRelativeURL"]("https://graph.microsoft.com/v1.0/me"), "/me"); |
| 123 | + }); |
| 124 | + |
| 125 | + it("Should return a relative URL for the complete URL with filter", () => { |
| 126 | + assert.equal(chaosHandler["getRelativeURL"]("https://graph.microsoft.com/v1.0/me/messages?filter=emailAddress eq 'jon@contoso.com'"), "/me/messages"); |
| 127 | + }); |
| 128 | + |
| 129 | + it("Should return a relative URL for the complete URL with ids", () => { |
| 130 | + assert.equal(chaosHandler["getRelativeURL"]("https://graph.microsoft.com/v1.0/me/messages/q1abcxx-xxxxxx-xxxxabc"), "/me/messages/q1abcxx-xxxxxx-xxxxabc"); |
| 131 | + }); |
| 132 | + |
| 133 | + it("Should return a relative URL for the complete URL in case of beta", () => { |
| 134 | + assert.equal(chaosHandler["getRelativeURL"]("https://graph.microsoft.com/beta/me/messages"), "/me/messages"); |
| 135 | + }); |
| 136 | + }); |
| 137 | + |
| 138 | + describe("setStatusCode", () => { |
| 139 | + const manualMap: Map<string, Map<string, number>> = new Map([["/me/messages/.*", new Map([["GET", 500], ["PATCH", 201]])], ["/me", new Map([["GET", 500], ["PATCH", 201]])]]); |
| 140 | + const tempManualOptions: ChaosHandlerOptions = new ChaosHandlerOptions(ChaosStrategy.MANUAL); |
| 141 | + const tempManualOptionsRegex: ChaosHandlerOptions = new ChaosHandlerOptions(ChaosStrategy.MANUAL); |
| 142 | + const tempChaosHandlerManual = new ChaosHandler(tempManualOptions, manualMap); |
| 143 | + const tempChaosHandlerManualRegex = new ChaosHandler(tempManualOptionsRegex, manualMap); |
| 144 | + |
| 145 | + it("Should set a statusCode for MANUAL mode", () => { |
| 146 | + const tempOptions = new ChaosHandlerOptions(ChaosStrategy.MANUAL, 404); |
| 147 | + chaosHandler["setStatusCode"](tempOptions, "https://graph.microsoft.com/v1.0/me", RequestMethod.GET); |
| 148 | + assert.isDefined(tempOptions.statusCode); |
| 149 | + }); |
| 150 | + |
| 151 | + it("Should set a statusCode for RANDOM mode", () => { |
| 152 | + const tempOptions = new ChaosHandlerOptions(ChaosStrategy.RANDOM, undefined, "I generated the error", 100); |
| 153 | + chaosHandler["setStatusCode"](tempOptions, "https://graph.microsoft.com/v1.0/me", RequestMethod.POST); |
| 154 | + assert.isDefined(tempOptions.statusCode); |
| 155 | + }); |
| 156 | + |
| 157 | + it("Should set a statusCode for MANUAL mode with manualMap", () => { |
| 158 | + tempChaosHandlerManual["setStatusCode"](tempManualOptions, "https://graph.microsoft.com/v1.0/me", RequestMethod.PATCH); |
| 159 | + assert.equal(tempManualOptions.statusCode, 201); |
| 160 | + }); |
| 161 | + |
| 162 | + it("Should set a statusCode for MANUAL mode with manualMap matching regex", () => { |
| 163 | + tempChaosHandlerManualRegex["setStatusCode"](tempManualOptionsRegex, "https://graph.microsoft.com/v1.0/me/messages/abc123-xxxxx-xxxxx", RequestMethod.GET); |
| 164 | + assert.equal(tempManualOptionsRegex.statusCode, 500); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | + describe("getOptions", () => { |
| 169 | + it("Should return the options in the context object", () => { |
| 170 | + const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, 405); |
| 171 | + const cxt: Context = { |
| 172 | + request: "url", |
| 173 | + middlewareControl: new MiddlewareControl([options]), |
| 174 | + }; |
| 175 | + const o = chaosHandler["getOptions"](cxt); |
| 176 | + assert.equal(o.chaosStrategy, ChaosStrategy.MANUAL); |
| 177 | + assert.equal(o.statusCode, 405); |
| 178 | + }); |
| 179 | + |
| 180 | + it("Should return the default set of options with RANDOM in the middleware", () => { |
| 181 | + const cxt: Context = { |
| 182 | + request: "url", |
| 183 | + }; |
| 184 | + const o = chaosHandler["getOptions"](cxt); |
| 185 | + assert.equal(o.chaosStrategy, ChaosStrategy.RANDOM); |
| 186 | + assert.equal(o.statusCode, undefined); |
| 187 | + }); |
| 188 | + |
| 189 | + it("Should return the default set of options with DEFAULT in the middleware", () => { |
| 190 | + const tempChaosHandler = new ChaosHandler(new ChaosHandlerOptions(ChaosStrategy.MANUAL)); |
| 191 | + const cxt: Context = { |
| 192 | + request: "url", |
| 193 | + }; |
| 194 | + const o = tempChaosHandler["getOptions"](cxt); |
| 195 | + assert.equal(o.chaosStrategy, ChaosStrategy.MANUAL); |
| 196 | + assert.equal(o.statusCode, undefined); |
| 197 | + }); |
| 198 | + }); |
| 199 | + |
| 200 | + describe("execute", async () => { |
| 201 | + const manualMap: Map<string, Map<string, number>> = new Map([["/me", new Map([["GET", 500], ["PATCH", 201]])]]); |
| 202 | + const dummyHTTPHandler = new DummyHTTPMessageHandler(); |
| 203 | + const tempChaosHandlerDefault = new ChaosHandler(new ChaosHandlerOptions()); |
| 204 | + const tempChaosHandlerRandom = new ChaosHandler(new ChaosHandlerOptions(ChaosStrategy.RANDOM)); |
| 205 | + const tempChaosHandlerManual = new ChaosHandler(new ChaosHandlerOptions(ChaosStrategy.MANUAL), manualMap); |
| 206 | + tempChaosHandlerDefault.setNext(dummyHTTPHandler); |
| 207 | + tempChaosHandlerRandom.setNext(dummyHTTPHandler); |
| 208 | + tempChaosHandlerManual.setNext(dummyHTTPHandler); |
| 209 | + |
| 210 | + it("Should return response for Default Case", async () => { |
| 211 | + const options = new ChaosHandlerOptions(ChaosStrategy.RANDOM); |
| 212 | + const cxt: Context = { |
| 213 | + request: "https://graph.microsoft.com/v1.0/me", |
| 214 | + options: { |
| 215 | + method: "GET", |
| 216 | + }, |
| 217 | + middlewareControl: new MiddlewareControl([options]), |
| 218 | + }; |
| 219 | + assert.isDefined(tempChaosHandlerDefault["execute"](cxt)); |
| 220 | + }); |
| 221 | + |
| 222 | + it("Should return response for Random case", async () => { |
| 223 | + const cxt: Context = { |
| 224 | + request: "https://graph.microsoft.com/v1.0/me", |
| 225 | + options: { |
| 226 | + method: "GET", |
| 227 | + }, |
| 228 | + }; |
| 229 | + assert.isDefined(tempChaosHandlerRandom["execute"](cxt)); |
| 230 | + }); |
| 231 | + |
| 232 | + it("Should return response for Manual Global case", async () => { |
| 233 | + const cxt: Context = { |
| 234 | + request: "https://graph.microsoft.com/v1.0/me", |
| 235 | + options: { |
| 236 | + method: "GET", |
| 237 | + }, |
| 238 | + }; |
| 239 | + assert.isDefined(tempChaosHandlerManual["execute"](cxt)); |
| 240 | + }); |
| 241 | + |
| 242 | + it("Should return response for Manual Request Level case", async () => { |
| 243 | + const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, 200); |
| 244 | + const cxt: Context = { |
| 245 | + request: "https://graph.microsoft.com/v1.0/me", |
| 246 | + options: { |
| 247 | + method: "GET", |
| 248 | + }, |
| 249 | + middlewareControl: new MiddlewareControl([options]), |
| 250 | + }; |
| 251 | + assert.isDefined(tempChaosHandlerManual["execute"](cxt)); |
| 252 | + }); |
| 253 | + }); |
| 254 | +}); |
0 commit comments