allow fault injection callbacks to run async code
This commit is contained in:
parent
48fa2ccea1
commit
883637d3f2
@ -28,17 +28,10 @@ import { URL } from "url";
|
|||||||
import {
|
import {
|
||||||
GlobalTestState,
|
GlobalTestState,
|
||||||
ExchangeService,
|
ExchangeService,
|
||||||
BankService,
|
|
||||||
ExchangeServiceInterface,
|
ExchangeServiceInterface,
|
||||||
MerchantServiceInterface,
|
MerchantServiceInterface,
|
||||||
MerchantService,
|
MerchantService,
|
||||||
PrivateOrderStatusQuery,
|
|
||||||
} from "./harness";
|
} from "./harness";
|
||||||
import {
|
|
||||||
PostOrderRequest,
|
|
||||||
PostOrderResponse,
|
|
||||||
MerchantOrderPrivateStatusResponse,
|
|
||||||
} from "./merchantApiTypes";
|
|
||||||
|
|
||||||
export interface FaultProxyConfig {
|
export interface FaultProxyConfig {
|
||||||
inboundPort: number;
|
inboundPort: number;
|
||||||
@ -65,8 +58,8 @@ export interface FaultInjectionResponseContext {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export interface FaultSpec {
|
export interface FaultSpec {
|
||||||
modifyRequest?: (ctx: FaultInjectionRequestContext) => void;
|
modifyRequest?: (ctx: FaultInjectionRequestContext) => Promise<void>;
|
||||||
modifyResponse?: (ctx: FaultInjectionResponseContext) => void;
|
modifyResponse?: (ctx: FaultInjectionResponseContext) => Promise<void>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export class FaultProxy {
|
export class FaultProxy {
|
||||||
@ -85,7 +78,7 @@ export class FaultProxy {
|
|||||||
req.on("data", (chunk) => {
|
req.on("data", (chunk) => {
|
||||||
requestChunks.push(chunk);
|
requestChunks.push(chunk);
|
||||||
});
|
});
|
||||||
req.on("end", () => {
|
req.on("end", async () => {
|
||||||
console.log("end of data");
|
console.log("end of data");
|
||||||
let requestBuffer: Buffer | undefined;
|
let requestBuffer: Buffer | undefined;
|
||||||
if (requestChunks.length > 0) {
|
if (requestChunks.length > 0) {
|
||||||
@ -103,7 +96,7 @@ export class FaultProxy {
|
|||||||
|
|
||||||
for (const faultSpec of this.currentFaultSpecs) {
|
for (const faultSpec of this.currentFaultSpecs) {
|
||||||
if (faultSpec.modifyRequest) {
|
if (faultSpec.modifyRequest) {
|
||||||
faultSpec.modifyRequest(faultReqContext);
|
await faultSpec.modifyRequest(faultReqContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -138,7 +131,7 @@ export class FaultProxy {
|
|||||||
proxyResp.on("data", (proxyRespData) => {
|
proxyResp.on("data", (proxyRespData) => {
|
||||||
respChunks.push(proxyRespData);
|
respChunks.push(proxyRespData);
|
||||||
});
|
});
|
||||||
proxyResp.on("end", () => {
|
proxyResp.on("end", async () => {
|
||||||
console.log("end of target response");
|
console.log("end of target response");
|
||||||
let responseBuffer: Buffer | undefined;
|
let responseBuffer: Buffer | undefined;
|
||||||
if (respChunks.length > 0) {
|
if (respChunks.length > 0) {
|
||||||
@ -154,7 +147,7 @@ export class FaultProxy {
|
|||||||
for (const faultSpec of this.currentFaultSpecs) {
|
for (const faultSpec of this.currentFaultSpecs) {
|
||||||
const modResponse = faultSpec.modifyResponse;
|
const modResponse = faultSpec.modifyResponse;
|
||||||
if (modResponse) {
|
if (modResponse) {
|
||||||
modResponse(faultRespContext);
|
await modResponse(faultRespContext);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (faultRespContext.dropResponse) {
|
if (faultRespContext.dropResponse) {
|
||||||
|
@ -155,7 +155,7 @@ export async function runExchangeManagementTest(t: GlobalTestState) {
|
|||||||
t.assertTrue(exchangesList.exchanges.length === 0);
|
t.assertTrue(exchangesList.exchanges.length === 0);
|
||||||
|
|
||||||
faultyExchange.faultProxy.addFault({
|
faultyExchange.faultProxy.addFault({
|
||||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||||
const url = new URL(ctx.request.requestUrl);
|
const url = new URL(ctx.request.requestUrl);
|
||||||
if (url.pathname === "/keys") {
|
if (url.pathname === "/keys") {
|
||||||
const body = {
|
const body = {
|
||||||
@ -193,7 +193,7 @@ export async function runExchangeManagementTest(t: GlobalTestState) {
|
|||||||
faultyExchange.faultProxy.clearAllFaults();
|
faultyExchange.faultProxy.clearAllFaults();
|
||||||
|
|
||||||
faultyExchange.faultProxy.addFault({
|
faultyExchange.faultProxy.addFault({
|
||||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||||
const url = new URL(ctx.request.requestUrl);
|
const url = new URL(ctx.request.requestUrl);
|
||||||
if (url.pathname === "/keys") {
|
if (url.pathname === "/keys") {
|
||||||
const keys = ctx.responseBody?.toString("utf-8");
|
const keys = ctx.responseBody?.toString("utf-8");
|
||||||
|
@ -91,7 +91,7 @@ export async function runPayAbortTest(t: GlobalTestState) {
|
|||||||
let firstDepositUrl: string | undefined;
|
let firstDepositUrl: string | undefined;
|
||||||
|
|
||||||
faultyExchange.faultProxy.addFault({
|
faultyExchange.faultProxy.addFault({
|
||||||
modifyRequest(ctx: FaultInjectionRequestContext) {
|
async modifyRequest(ctx: FaultInjectionRequestContext) {
|
||||||
const url = new URL(ctx.requestUrl);
|
const url = new URL(ctx.requestUrl);
|
||||||
if (url.pathname.endsWith("/deposit")) {
|
if (url.pathname.endsWith("/deposit")) {
|
||||||
if (!firstDepositUrl) {
|
if (!firstDepositUrl) {
|
||||||
@ -104,7 +104,7 @@ export async function runPayAbortTest(t: GlobalTestState) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||||
const url = new URL(ctx.request.requestUrl);
|
const url = new URL(ctx.request.requestUrl);
|
||||||
if (url.pathname.endsWith("/deposit") && url.href != firstDepositUrl) {
|
if (url.pathname.endsWith("/deposit") && url.href != firstDepositUrl) {
|
||||||
ctx.responseBody = Buffer.from("{}");
|
ctx.responseBody = Buffer.from("{}");
|
||||||
@ -114,7 +114,7 @@ export async function runPayAbortTest(t: GlobalTestState) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
faultyMerchant.faultProxy.addFault({
|
faultyMerchant.faultProxy.addFault({
|
||||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||||
const url = new URL(ctx.request.requestUrl);
|
const url = new URL(ctx.request.requestUrl);
|
||||||
if (url.pathname.endsWith("/pay") && url.href != firstDepositUrl) {
|
if (url.pathname.endsWith("/pay") && url.href != firstDepositUrl) {
|
||||||
ctx.responseBody = Buffer.from("{}");
|
ctx.responseBody = Buffer.from("{}");
|
||||||
|
@ -167,7 +167,7 @@ export async function runPayPaidTest(t: GlobalTestState) {
|
|||||||
let numPaidRequested = 0;
|
let numPaidRequested = 0;
|
||||||
|
|
||||||
faultyMerchant.faultProxy.addFault({
|
faultyMerchant.faultProxy.addFault({
|
||||||
modifyRequest(ctx: FaultInjectionRequestContext) {
|
async modifyRequest(ctx: FaultInjectionRequestContext) {
|
||||||
const url = new URL(ctx.requestUrl);
|
const url = new URL(ctx.requestUrl);
|
||||||
if (url.pathname.endsWith("/pay")) {
|
if (url.pathname.endsWith("/pay")) {
|
||||||
numPayRequested++;
|
numPayRequested++;
|
||||||
|
@ -83,10 +83,10 @@ export async function runPaymentFaultTest(t: GlobalTestState) {
|
|||||||
|
|
||||||
// Print all requests to the exchange
|
// Print all requests to the exchange
|
||||||
faultyExchange.faultProxy.addFault({
|
faultyExchange.faultProxy.addFault({
|
||||||
modifyRequest(ctx: FaultInjectionRequestContext) {
|
async modifyRequest(ctx: FaultInjectionRequestContext) {
|
||||||
console.log("got request", ctx);
|
console.log("got request", ctx);
|
||||||
},
|
},
|
||||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||||
console.log("got response", ctx);
|
console.log("got response", ctx);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -180,7 +180,7 @@ export async function runPaymentFaultTest(t: GlobalTestState) {
|
|||||||
// Drop 3 responses from the exchange.
|
// Drop 3 responses from the exchange.
|
||||||
let faultCount = 0;
|
let faultCount = 0;
|
||||||
faultyExchange.faultProxy.addFault({
|
faultyExchange.faultProxy.addFault({
|
||||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||||
if (!ctx.request.requestUrl.endsWith("/deposit")) {
|
if (!ctx.request.requestUrl.endsWith("/deposit")) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -116,7 +116,7 @@ export async function runPaymentTransientTest(t: GlobalTestState) {
|
|||||||
let faultInjected = false;
|
let faultInjected = false;
|
||||||
|
|
||||||
faultyMerchant.faultProxy.addFault({
|
faultyMerchant.faultProxy.addFault({
|
||||||
modifyResponse(ctx: FaultInjectionResponseContext) {
|
async modifyResponse(ctx: FaultInjectionResponseContext) {
|
||||||
console.log("in modifyResponse");
|
console.log("in modifyResponse");
|
||||||
const url = new URL(ctx.request.requestUrl);
|
const url = new URL(ctx.request.requestUrl);
|
||||||
console.log("pathname is", url.pathname);
|
console.log("pathname is", url.pathname);
|
||||||
|
Loading…
Reference in New Issue
Block a user