Skip to content

Commit 5512ca3

Browse files
committed
Add the first non-final step: waitFor(ms)
1 parent 61a4f62 commit 5512ca3

File tree

8 files changed

+220
-54
lines changed

8 files changed

+220
-54
lines changed

src/rules/requests/request-rule-builder.ts

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import {
1717
FileStepDefinition,
1818
JsonRpcResponseStepDefinition,
1919
ResetConnectionStepDefinition,
20-
CallbackResponseMessageResult
20+
CallbackResponseMessageResult,
21+
RequestStepDefinition,
22+
DelayStepDefinition
2123
} from "./request-step-definitions";
2224
import { byteLength } from "../../util/util";
2325
import { BaseRuleBuilder } from "../base-rule-builder";
@@ -86,6 +88,16 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
8688
}
8789
}
8890

91+
private steps: Array<RequestStepDefinition> = [];
92+
93+
/**
94+
* Add a delay (in milliseconds) before the next step in the rule
95+
*/
96+
waitFor(ms: number): this {
97+
this.steps.push(new DelayStepDefinition(ms));
98+
return this;
99+
}
100+
89101
/**
90102
* Reply to matched requests with a given status code and (optionally) status message,
91103
* body, headers & trailers.
@@ -139,15 +151,17 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
139151
trailers = headersOrTrailers as Trailers | undefined;
140152
}
141153

154+
this.steps.push(new SimpleStepDefinition(
155+
status,
156+
statusMessage,
157+
data,
158+
headers,
159+
trailers
160+
));
161+
142162
const rule: RequestRuleData = {
143163
...this.buildBaseRuleData(),
144-
steps: [new SimpleStepDefinition(
145-
status,
146-
statusMessage,
147-
data,
148-
headers,
149-
trailers
150-
)]
164+
steps: this.steps
151165
};
152166

153167
return this.addRule(rule);
@@ -182,9 +196,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
182196
// connection after the response is sent, which can confuse clients.
183197
}, headers);
184198

199+
this.steps.push(new SimpleStepDefinition(status, undefined, jsonData, headers));
200+
185201
const rule: RequestRuleData = {
186202
...this.buildBaseRuleData(),
187-
steps: [new SimpleStepDefinition(status, undefined, jsonData, headers)]
203+
steps: this.steps
188204
};
189205

190206
return this.addRule(rule);
@@ -216,9 +232,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
216232
thenCallback(callback:
217233
(request: CompletedRequest) => MaybePromise<CallbackResponseResult>
218234
): Promise<MockedEndpoint> {
235+
this.steps.push(new CallbackStepDefinition(callback));
236+
219237
const rule: RequestRuleData = {
220238
...this.buildBaseRuleData(),
221-
steps: [new CallbackStepDefinition(callback)]
239+
steps: this.steps
222240
}
223241

224242
return this.addRule(rule);
@@ -245,9 +263,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
245263
* @category Responses
246264
*/
247265
thenStream(status: number, stream: Readable, headers?: Headers): Promise<MockedEndpoint> {
266+
this.steps.push(new StreamStepDefinition(status, stream, headers));
267+
248268
const rule: RequestRuleData = {
249269
...this.buildBaseRuleData(),
250-
steps: [new StreamStepDefinition(status, stream, headers)]
270+
steps: this.steps
251271
}
252272

253273
return this.addRule(rule);
@@ -294,9 +314,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
294314
headers = pathOrHeaders as Headers | undefined;
295315
}
296316

317+
this.steps.push(new FileStepDefinition(status, statusMessage, path, headers));
318+
297319
const rule: RequestRuleData = {
298320
...this.buildBaseRuleData(),
299-
steps: [new FileStepDefinition(status, statusMessage, path, headers)]
321+
steps: this.steps
300322
};
301323

302324
return this.addRule(rule);
@@ -322,9 +344,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
322344
* @category Responses
323345
*/
324346
thenPassThrough(options?: PassThroughStepOptions): Promise<MockedEndpoint> {
347+
this.steps.push(new PassThroughStepDefinition(options));
348+
325349
const rule: RequestRuleData = {
326350
...this.buildBaseRuleData(),
327-
steps: [new PassThroughStepDefinition(options)]
351+
steps: this.steps
328352
};
329353

330354
return this.addRule(rule);
@@ -360,15 +384,17 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
360384
forwarding?: Omit<PassThroughStepOptions['forwarding'], 'targetHost'>
361385
} = {}
362386
): Promise<MockedEndpoint> {
387+
this.steps.push(new PassThroughStepDefinition({
388+
...options,
389+
forwarding: {
390+
...options.forwarding,
391+
targetHost: forwardToLocation
392+
}
393+
}));
394+
363395
const rule: RequestRuleData = {
364396
...this.buildBaseRuleData(),
365-
steps: [new PassThroughStepDefinition({
366-
...options,
367-
forwarding: {
368-
...options.forwarding,
369-
targetHost: forwardToLocation
370-
}
371-
})]
397+
steps: this.steps
372398
};
373399

374400
return this.addRule(rule);
@@ -389,9 +415,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
389415
* @category Responses
390416
*/
391417
thenCloseConnection(): Promise<MockedEndpoint> {
418+
this.steps.push(new CloseConnectionStepDefinition());
419+
392420
const rule: RequestRuleData = {
393421
...this.buildBaseRuleData(),
394-
steps: [new CloseConnectionStepDefinition()]
422+
steps: this.steps
395423
};
396424

397425
return this.addRule(rule);
@@ -416,9 +444,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
416444
* @category Responses
417445
*/
418446
thenResetConnection(): Promise<MockedEndpoint> {
447+
this.steps.push(new ResetConnectionStepDefinition());
448+
419449
const rule: RequestRuleData = {
420450
...this.buildBaseRuleData(),
421-
steps: [new ResetConnectionStepDefinition()]
451+
steps: this.steps
422452
};
423453

424454
return this.addRule(rule);
@@ -439,9 +469,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
439469
* @category Responses
440470
*/
441471
thenTimeout(): Promise<MockedEndpoint> {
472+
this.steps.push(new TimeoutStepDefinition());
473+
442474
const rule: RequestRuleData = {
443475
...this.buildBaseRuleData(),
444-
steps: [new TimeoutStepDefinition()]
476+
steps: this.steps
445477
};
446478

447479
return this.addRule(rule);
@@ -455,9 +487,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
455487
* @category Responses
456488
*/
457489
thenSendJsonRpcResult(result: any) {
490+
this.steps.push(new JsonRpcResponseStepDefinition({ result }));
491+
458492
const rule = {
459493
...this.buildBaseRuleData(),
460-
steps: [new JsonRpcResponseStepDefinition({ result })]
494+
steps: this.steps
461495
};
462496

463497
return this.addRule(rule);
@@ -471,9 +505,11 @@ export class RequestRuleBuilder extends BaseRuleBuilder {
471505
* @category Responses
472506
*/
473507
thenSendJsonRpcError(error: any) {
508+
this.steps.push(new JsonRpcResponseStepDefinition({ error }));
509+
474510
const rule = {
475511
...this.buildBaseRuleData(),
476-
steps: [new JsonRpcResponseStepDefinition({ error })]
512+
steps: this.steps
477513
};
478514

479515
return this.addRule(rule);

src/rules/requests/request-step-definitions.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,23 @@ export class JsonRpcResponseStepDefinition extends Serializable implements Reque
10401040
}
10411041
}
10421042

1043+
export class DelayStepDefinition extends Serializable implements RequestStepDefinition {
1044+
1045+
readonly type = 'delay';
1046+
static readonly isFinal = false;
1047+
1048+
constructor(
1049+
public readonly delayMs: number
1050+
) {
1051+
super()
1052+
}
1053+
1054+
explain(): string {
1055+
return `wait ${this.delayMs}ms`;
1056+
}
1057+
1058+
}
1059+
10431060
export const StepDefinitionLookup = {
10441061
'simple': SimpleStepDefinition,
10451062
'callback': CallbackStepDefinition,
@@ -1049,5 +1066,6 @@ export const StepDefinitionLookup = {
10491066
'close-connection': CloseConnectionStepDefinition,
10501067
'reset-connection': ResetConnectionStepDefinition,
10511068
'timeout': TimeoutStepDefinition,
1052-
'json-rpc-response': JsonRpcResponseStepDefinition
1069+
'json-rpc-response': JsonRpcResponseStepDefinition,
1070+
'delay': DelayStepDefinition
10531071
}

src/rules/requests/request-steps.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import {
2020
OngoingResponse
2121
} from "../../types";
2222

23-
import { MaybePromise, ErrorLike, isErrorLike } from '@httptoolkit/util';
23+
import { MaybePromise, ErrorLike, isErrorLike, delay } from '@httptoolkit/util';
2424
import { isAbsoluteUrl, getEffectivePort } from '../../util/url';
2525
import {
2626
waitForCompletedRequest,
@@ -115,7 +115,8 @@ import {
115115
SERIALIZED_OMIT,
116116
SimpleStepDefinition,
117117
StreamStepDefinition,
118-
TimeoutStepDefinition
118+
TimeoutStepDefinition,
119+
DelayStepDefinition
119120
} from './request-step-definitions';
120121

121122
// Re-export various type definitions. This is mostly for compatibility with external
@@ -1396,6 +1397,13 @@ export class JsonRpcResponseStep extends JsonRpcResponseStepDefinition {
13961397
}
13971398
}
13981399

1400+
export class DelayStep extends DelayStepDefinition {
1401+
async handle(): Promise<{ continue: true }> {
1402+
await delay(this.delayMs);
1403+
return { continue: true };
1404+
}
1405+
}
1406+
13991407
export const StepLookup: typeof StepDefinitionLookup = {
14001408
'simple': SimpleStep,
14011409
'callback': CallbackStep,
@@ -1405,5 +1413,6 @@ export const StepLookup: typeof StepDefinitionLookup = {
14051413
'close-connection': CloseConnectionStep,
14061414
'reset-connection': ResetConnectionStep,
14071415
'timeout': TimeoutStep,
1408-
'json-rpc-response': JsonRpcResponseStep
1416+
'json-rpc-response': JsonRpcResponseStep,
1417+
'delay': DelayStep
14091418
}

0 commit comments

Comments
 (0)