Skip to content

Commit

Permalink
feat(cli): support global delay range
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip committed Nov 28, 2024
1 parent 28a4360 commit be5db0d
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 4 deletions.
25 changes: 25 additions & 0 deletions packages/cli/src/util/__tests__/config.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ describe('assertValidConfig', () => {
ignoreExamples: true,
dynamic: false,
jsonSchemaFakerFillProperties: true,
delay: [100, 200],
chaos: {
enabled: true,
rate: 50,
Expand All @@ -16,6 +17,14 @@ describe('assertValidConfig', () => {
expect(assertValidConfig.bind(null, validConfig)).not.toThrow();
});

it('should accept numeric delay', () => {
const validConfig: Config = {
delay: 100,
};

expect(assertValidConfig.bind(null, validConfig)).not.toThrow();
});

it('should throw an error for an invalid config', () => {
const invalidConfig = {
ignoreExamples: 'true', // invalid type
Expand Down Expand Up @@ -75,4 +84,20 @@ describe('assertValidConfig', () => {
})
).toThrow();
});

it('should require upper latency bound to be higher than lower latency bound', () => {
expect(
assertValidConfig.bind(null, {
delay: [100, 50],
})
).toThrow();
});

it('should throw an error for a config with negative delay', () => {
expect(
assertValidConfig.bind(null, {
delay: -100,
})
).toThrow();
});
});
35 changes: 31 additions & 4 deletions packages/cli/src/util/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export type Config = {
ignoreExamples?: boolean;
dynamic?: boolean;
jsonSchemaFakerFillProperties?: boolean;
delay?: number | [lowerBound: number, upperBound: number];
chaos?: {
enabled?: boolean;
rate?: number;
Expand All @@ -26,6 +27,7 @@ export type Config = {
const ajv = new Ajv.Ajv2020({
strict: true,
allErrors: true,
$data: true,
});

const validate = ajv.compile({
Expand All @@ -34,6 +36,34 @@ const validate = ajv.compile({
ignoreExamples: { type: 'boolean' },
dynamic: { type: 'boolean' },
jsonSchemaFakerFillProperties: { type: 'boolean' },
delay: {
oneOf: [
{
type: 'integer',
minimum: 0,
maximum: 5000,
},
{
type: 'array',
prefixItems: [
{
type: 'integer',
minimum: 0,
exclusiveMaximum: 5000,
},
{
type: 'integer',
minimum: {
$data: '1/0',
},
maximum: 5000,
},
],
minItems: 2,
items: false,
},
],
},
chaos: {
type: 'object',
unevaluatedProperties: false,
Expand Down Expand Up @@ -102,10 +132,7 @@ export async function safeApplyConfig(

try {
const input = JSON.parse(content);
const valid = validate(input) as unknown as (input: unknown) => input is CreateMockServerOptions;
if (!valid) {
throw ajv.errorsText(validate.errors);
}
assertValidConfig(input);

const merged = { ...defaultConfig, ...input };
for (const key of Object.keys(merged)) {
Expand Down
4 changes: 4 additions & 0 deletions packages/cli/src/util/createServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,9 @@ async function createPrismServerWithLogger(options: Observable<CreateBaseServerO
)
);
},
get delay() {
return options.delay;
},
get dynamic() {
return options.dynamic;
},
Expand Down Expand Up @@ -185,6 +188,7 @@ type CreateBaseServerOptions = {
config?: string;
dynamic: boolean;
cors: boolean;
delay?: number | [number, number];
chaos?: {
enabled?: boolean;
rate?: number;
Expand Down
33 changes: 33 additions & 0 deletions test-harness/specs/config/delay_constant.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
====test====
Given I mock and specify a constant delay
When I send a request to an operation
Then the response should be delayed by that amount
====spec====
openapi: "3.1.0"
info:
version: "0.0"
title: Config Test
paths:
/pets/{petId}:
get:
description: Get a pet by ID
responses:
"200":
description: A pet
content:
application/json:
schema:
type: integer
default: 0
====config====
{
"delay": 500
}
====server====
mock -p 4010 --config ${config} ${document}
====command====
curl -i -s -w "%{time_total}" "http://127.0.0.1:4010/pets/2" | awk '{gsub(/0([0-9]+\.[0-9]+)/, $1 * 1000 > 500 ? "Value is higher than 500" : "Value is not higher than 500"); print}'
====expect-loose====
HTTP/1.1 200 OK

Value is higher than 500
33 changes: 33 additions & 0 deletions test-harness/specs/config/delay_range.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
====test====
Given I mock and specify a constant delay
When I send a request to an operation
Then the response should be delayed by that amount
====spec====
openapi: "3.1.0"
info:
version: "0.0"
title: Config Test
paths:
/pets/{petId}:
get:
description: Get a pet by ID
responses:
"200":
description: A pet
content:
application/json:
schema:
type: integer
default: 0
====config====
{
"delay": [300, 500]
}
====server====
mock -p 4010 --config ${config} ${document}
====command====
curl -i -s -w "%{time_total}" "http://127.0.0.1:4010/pets/2" | awk '{gsub(/0([0-9]+\.[0-9]+)/, $1 * 1000 > 300 ? "Value is higher than 300" : "Value is not higher than 300"); print}'
====expect-loose====
HTTP/1.1 200 OK

Value is higher than 300
35 changes: 35 additions & 0 deletions test-harness/specs/delay/delay_config.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
====test====
When I send a request to an operation
And I have a config with a delay of 2500ms
And in the headers I specify `Prefer: delay=100`
Then I get a response with a delay of 100ms
====spec====
openapi: "3.1.1"
info:
version: "0"
title: Delays test
description: Delays test
paths:
/delay:
get:
description: widget details
responses:
"200":
description: delay response
content:
application/json:
schema:
type: integer
default: 0
====config====
{
"delay": 2500
}
====server====
mock -p 4010 ${document}
====command====
curl -i -s -w "%{time_total}" -H "Prefer: delay=100" "http://127.0.0.1:4010/delay" | awk '{gsub(/0([0-9]+\.[0-9]+)/, ($1 * 1000 < 2500 ? "Value is lower than 2500" : "Value is higher than 2500")); print}'
====expect-loose====
HTTP/1.1 200 OK

Value is lower than 2500

0 comments on commit be5db0d

Please sign in to comment.