Skip to content

Commit e238b45

Browse files
Sebastienlevert/fix chaos handler random (#429)
* Fixing ChaosHandler with interactive scenarios * Fixing manual strategy and collapse ifs * Removing unnecessary async in ChaodHandler tests * Update src/middleware/ChaosHandler.ts Co-authored-by: Nikitha Chettiar <[email protected]> Co-authored-by: Nikitha Chettiar <[email protected]>
1 parent 597f716 commit e238b45

File tree

3 files changed

+29
-19
lines changed

3 files changed

+29
-19
lines changed

src/middleware/ChaosHandler.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export class ChaosHandler implements Middleware {
7777

7878
if (chaosHandlerOptions.statusCode === 429) {
7979
// throttling case has to have a timeout scenario
80-
responseHeader.append("retry-after", "300");
80+
responseHeader.append("retry-after", "3");
8181
}
8282

8383
return responseHeader;
@@ -129,7 +129,7 @@ export class ChaosHandler implements Middleware {
129129
const responseHeader = this.createResponseHeaders(chaosHandlerOptions, requestID, requestDate.toString());
130130
const responseBody = this.createResponseBody(chaosHandlerOptions, requestID, requestDate.toString());
131131
const init: any = { url: requestURL, status: chaosHandlerOptions.statusCode, statusText: chaosHandlerOptions.statusMessage, headers: responseHeader };
132-
context.response = new Response(responseBody, init);
132+
context.response = new Response(typeof responseBody === "string" ? responseBody : JSON.stringify(responseBody), init);
133133
}
134134

135135
/**
@@ -141,10 +141,10 @@ export class ChaosHandler implements Middleware {
141141
*/
142142
private async sendRequest(chaosHandlerOptions: ChaosHandlerOptions, context: Context): Promise<void> {
143143
this.setStatusCode(chaosHandlerOptions, context.request as string, context.options.method as RequestMethod);
144-
if (!chaosHandlerOptions.statusCode) {
145-
await this.nextMiddleware.execute(context);
146-
} else {
144+
if ((chaosHandlerOptions.chaosStrategy === ChaosStrategy.MANUAL && !this.nextMiddleware) || Math.floor(Math.random() * 100) < chaosHandlerOptions.chaosPercentage) {
147145
this.createResponse(chaosHandlerOptions, context);
146+
} else if (this.nextMiddleware) {
147+
await this.nextMiddleware.execute(context);
148148
}
149149
}
150150

@@ -209,9 +209,7 @@ export class ChaosHandler implements Middleware {
209209
}
210210
} else {
211211
// Handling the case of Random here
212-
if (Math.floor(Math.random() * 100) < chaosHandlerOptions.chaosPercentage) {
213-
chaosHandlerOptions.statusCode = this.getRandomStatusCode(requestMethod);
214-
}
212+
chaosHandlerOptions.statusCode = this.getRandomStatusCode(requestMethod);
215213
// else statusCode would be undefined
216214
}
217215
}

test/common/core/Client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ describe("Client.ts", () => {
7575
};
7676
const authHandler = new AuthenticationHandler(new CustomAuthenticationProvider(provider));
7777
const responseBody = "Test response body";
78-
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing middleware array", 200, 0, responseBody);
78+
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing middleware array", 200, 100, responseBody);
7979
const middlewareArray = [authHandler, new ChaosHandler(options)];
8080
const client = Client.initWithMiddleware({ middleware: middlewareArray });
8181

@@ -90,7 +90,7 @@ describe("Client.ts", () => {
9090
const authHandler = new AuthenticationHandler(new CustomAuthenticationProvider(provider));
9191

9292
const responseBody = "Test response body";
93-
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing chained middleware array", 200, 0, responseBody);
93+
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing chained middleware array", 200, 100, responseBody);
9494
const chaosHandler = new ChaosHandler(options);
9595
const telemetryHandler = new TelemetryHandler();
9696

test/common/middleware/ChaosHandler.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ describe("ChaosHandler.ts", () => {
7171
});
7272
});
7373

74-
describe("sendRequest", async () => {
74+
describe("sendRequest", () => {
7575
const cxt: Context = {
7676
request: "https://graph.microsoft.com/v1.0/me",
7777
options: {
@@ -80,19 +80,19 @@ describe("ChaosHandler.ts", () => {
8080
};
8181

8282
const manualMap: Map<string, Map<string, number>> = new Map([["/me", new Map([["GET", 500]])]]);
83-
const tempManualOptions: ChaosHandlerOptions = new ChaosHandlerOptions(ChaosStrategy.MANUAL);
83+
const tempManualOptions: ChaosHandlerOptions = new ChaosHandlerOptions(ChaosStrategy.MANUAL, undefined, undefined, 100);
8484
const tempChaosHandler = new ChaosHandler(tempManualOptions, manualMap);
8585

8686
const dummyHTTPHandler = new DummyHTTPMessageHandler();
8787
const handler = new ChaosHandler();
8888
handler.setNext(dummyHTTPHandler);
8989

90-
it("Should return a response after creating it", async () => {
90+
it("Should return a response after creating it", () => {
9191
tempChaosHandler["sendRequest"](tempManualOptions, cxt);
9292
assert.isDefined(cxt.response);
9393
});
9494

95-
it("Should send the request to the graph", async () => {
95+
it("Should send the request to the graph", () => {
9696
handler["sendRequest"](new ChaosHandlerOptions(ChaosStrategy.RANDOM, "I generated the error", undefined, 100), cxt);
9797
assert.isDefined(cxt.response);
9898
});
@@ -215,7 +215,7 @@ describe("ChaosHandler.ts", () => {
215215
});
216216
});
217217

218-
describe("execute", async () => {
218+
describe("execute", () => {
219219
const manualMap: Map<string, Map<string, number>> = new Map([
220220
[
221221
"/me",
@@ -233,7 +233,7 @@ describe("ChaosHandler.ts", () => {
233233
tempChaosHandlerRandom.setNext(dummyHTTPHandler);
234234
tempChaosHandlerManual.setNext(dummyHTTPHandler);
235235

236-
it("Should return response for Default Case", async () => {
236+
it("Should return response for Default Case", () => {
237237
const options = new ChaosHandlerOptions(ChaosStrategy.RANDOM);
238238
const cxt: Context = {
239239
request: "https://graph.microsoft.com/v1.0/me",
@@ -245,7 +245,7 @@ describe("ChaosHandler.ts", () => {
245245
assert.isDefined(tempChaosHandlerDefault["execute"](cxt));
246246
});
247247

248-
it("Should return response for Random case", async () => {
248+
it("Should return response for Random case", () => {
249249
const cxt: Context = {
250250
request: "https://graph.microsoft.com/v1.0/me",
251251
options: {
@@ -255,7 +255,7 @@ describe("ChaosHandler.ts", () => {
255255
assert.isDefined(tempChaosHandlerRandom["execute"](cxt));
256256
});
257257

258-
it("Should return response for Manual Global case", async () => {
258+
it("Should return response for Manual Global case", () => {
259259
const cxt: Context = {
260260
request: "https://graph.microsoft.com/v1.0/me",
261261
options: {
@@ -265,7 +265,7 @@ describe("ChaosHandler.ts", () => {
265265
assert.isDefined(tempChaosHandlerManual["execute"](cxt));
266266
});
267267

268-
it("Should return response for Manual Request Level case", async () => {
268+
it("Should return response for Manual Request Level case", () => {
269269
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Manual Request level case", 200);
270270
const cxt: Context = {
271271
request: "https://graph.microsoft.com/v1.0/me",
@@ -276,5 +276,17 @@ describe("ChaosHandler.ts", () => {
276276
};
277277
assert.isDefined(tempChaosHandlerManual["execute"](cxt));
278278
});
279+
280+
it("Should return response for Manual Request Level case 100%", () => {
281+
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Manual Request level case", 429, 100);
282+
const cxt: Context = {
283+
request: "https://graph.microsoft.com/v1.0/me",
284+
options: {
285+
method: "GET",
286+
},
287+
middlewareControl: new MiddlewareControl([options]),
288+
};
289+
assert.isDefined(tempChaosHandlerManual["execute"](cxt));
290+
});
279291
});
280292
});

0 commit comments

Comments
 (0)