forked from stoplightio/prism
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cli): include chaos option in Spectra config
- Loading branch information
Showing
5 changed files
with
272 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { assertValidConfig, type Config } from '../config'; | ||
|
||
describe('assertValidConfig', () => { | ||
it('should not throw an error for a valid config', () => { | ||
const validConfig: Config = { | ||
ignoreExamples: true, | ||
dynamic: false, | ||
jsonSchemaFakerFillProperties: true, | ||
chaos: { | ||
enabled: true, | ||
rate: 50, | ||
codes: [500, 502], | ||
}, | ||
}; | ||
|
||
expect(assertValidConfig.bind(null, validConfig)).not.toThrow(); | ||
}); | ||
|
||
it('should throw an error for an invalid config', () => { | ||
const invalidConfig = { | ||
ignoreExamples: 'true', // invalid type | ||
dynamic: false, | ||
jsonSchemaFakerFillProperties: true, | ||
chaos: { | ||
enabled: true, | ||
rate: 50, | ||
codes: [500, 502], | ||
}, | ||
}; | ||
|
||
expect(assertValidConfig.bind(null, invalidConfig)).toThrow(); | ||
}); | ||
|
||
it('should throw an error for a config with missing required properties', () => { | ||
const invalidConfig = { | ||
dynamic: false, | ||
jsonSchemaFakerFillProperties: true, | ||
chaos: { | ||
enabled: true, | ||
rate: 50, | ||
}, | ||
}; | ||
|
||
expect(assertValidConfig.bind(null, invalidConfig)).toThrow(); | ||
}); | ||
|
||
it('should throw an error for a config with additional properties', () => { | ||
const invalidConfig = { | ||
ignoreExamples: true, | ||
dynamic: false, | ||
jsonSchemaFakerFillProperties: true, | ||
extraProperty: 'not allowed', | ||
}; | ||
|
||
expect(assertValidConfig.bind(null, invalidConfig)).toThrow(); | ||
}); | ||
|
||
it('should throw an error for a config with enabled chaos and no/empty codes', () => { | ||
expect( | ||
assertValidConfig.bind(null, { | ||
chaos: { | ||
enabled: true, | ||
rate: 50, | ||
}, | ||
}) | ||
).toThrow(); | ||
|
||
expect( | ||
assertValidConfig.bind(null, { | ||
chaos: { | ||
enabled: true, | ||
rate: 50, | ||
codes: [], | ||
}, | ||
}) | ||
).toThrow(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
====test==== | ||
Given I mock and specify a config with chaos enabled | ||
When I send a request to an operation | ||
Then the config should influence the response | ||
====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: object | ||
properties: | ||
name: | ||
const: Odie | ||
"401": | ||
description: Unauthorized | ||
content: | ||
application/json: | ||
schema: | ||
type: object | ||
properties: | ||
message: | ||
const: Unauthorized | ||
|
||
====config==== | ||
{ | ||
"chaos": { | ||
"enabled": true, | ||
"rate": 100, | ||
"codes": [401] | ||
} | ||
} | ||
====server==== | ||
mock -p 4010 --config ${config} ${document} | ||
====command==== | ||
curl -i http://localhost:4010/pets/2 | ||
====expect==== | ||
HTTP/1.1 401 OK | ||
content-type: application/json | ||
|
||
{"message":"Unauthorized"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
====test==== | ||
Given I mock and specify a broken config | ||
When I send a request to an operation | ||
Then the config should not influence the response | ||
====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: object | ||
properties: | ||
name: | ||
type: string | ||
"401": | ||
description: Unauthorized | ||
====config==== | ||
{ | ||
"chaos": { | ||
"enabled": true, | ||
"rate": 100, | ||
"codes": [] | ||
} | ||
} | ||
====server==== | ||
mock -p 4010 --config ${config} ${document} | ||
====command==== | ||
curl -i http://localhost:4010/pets/2 | ||
====expect==== | ||
HTTP/1.1 200 OK | ||
content-type: application/json | ||
|
||
{"name":"string"} |