Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/middleware/ChaosHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export class ChaosHandler implements Middleware {

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

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

/**
Expand All @@ -141,10 +141,12 @@ export class ChaosHandler implements Middleware {
*/
private async sendRequest(chaosHandlerOptions: ChaosHandlerOptions, context: Context): Promise<void> {
this.setStatusCode(chaosHandlerOptions, context.request as string, context.options.method as RequestMethod);
if (!chaosHandlerOptions.statusCode) {
await this.nextMiddleware.execute(context);
} else {
if (Math.floor(Math.random() * 100) < chaosHandlerOptions.chaosPercentage) {
this.createResponse(chaosHandlerOptions, context);
} else {
if (this.nextMiddleware) {
await this.nextMiddleware.execute(context);
}
}
}

Expand Down Expand Up @@ -209,9 +211,7 @@ export class ChaosHandler implements Middleware {
}
} else {
// Handling the case of Random here
if (Math.floor(Math.random() * 100) < chaosHandlerOptions.chaosPercentage) {
chaosHandlerOptions.statusCode = this.getRandomStatusCode(requestMethod);
}
chaosHandlerOptions.statusCode = this.getRandomStatusCode(requestMethod);
// else statusCode would be undefined
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/common/core/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe("Client.ts", () => {
};
const authHandler = new AuthenticationHandler(new CustomAuthenticationProvider(provider));
const responseBody = "Test response body";
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing middleware array", 200, 0, responseBody);
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Testing middleware array", 200, 100, responseBody);
const middlewareArray = [authHandler, new ChaosHandler(options)];
const client = Client.initWithMiddleware({ middleware: middlewareArray });

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

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

Expand Down
14 changes: 13 additions & 1 deletion test/common/middleware/ChaosHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe("ChaosHandler.ts", () => {
};

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

const dummyHTTPHandler = new DummyHTTPMessageHandler();
Expand Down Expand Up @@ -276,5 +276,17 @@ describe("ChaosHandler.ts", () => {
};
assert.isDefined(tempChaosHandlerManual["execute"](cxt));
});

it("Should return response for Manual Request Level case 100%", async () => {
const options = new ChaosHandlerOptions(ChaosStrategy.MANUAL, "Manual Request level case", 429, 100);
const cxt: Context = {
request: "https://graph.microsoft.com/v1.0/me",
options: {
method: "GET",
},
middlewareControl: new MiddlewareControl([options]),
};
assert.isDefined(tempChaosHandlerManual["execute"](cxt));
});
});
});