Basic pact contract pre-validation
npm install contract-validationA convenience tool for checking consumer contracts (supports Pact) against openapi contract.
Requires Nodejs to be installed: https://nodejs.org/en/download/
Make sure the command npm is available in terminals (npm = node package manager). More documentation can be found here https://docs.npmjs.com/
Required npm packages. Execute these lines in a terminal:
npm install -g @stoplight/prism-cli
npm install -g contract-validation
#### In case of upgrades
npm upgrade -g @stoplight/prism-cli
npm upgrade -g contract-validation
To run the the test you need a pact contract and an openapi contract.
You can download an openapi contract by using curl.
curl http://localhost:8080/api/openapi.json -O
Execute script:
check-contract --contract consumer-contract.json --openapi openapi-contract.json
The check-contract script runs a prism server internally. The prism server doc can be found here: https://stoplight.io/p/docs/gh/stoplightio/prism
The prism server takes as input an openapi contract and can act as a mocking http server.
To start the mocking server, execute:
prism mock openapi.json
You can then execute the check-contract script without the openapi.json file:
Execute script:
check-contract --contract consumer-contract.json
#### Open api contract
A valid openapi contract is required for prism to do its job. Formats supported are json and yaml.
#### Consumer contract json formats
The consumer contracts has to be in the following format (a subset of a Pact contract):
[
{
request: {
path: string,
method: string,
body: object,
headers: object
},
response: {
body: object
}
}
]
Example:
[
{
"request": {
"method": "POST",
"path": "/product",
"headers": {
"X-Company-header": "data",
"Accept": "application/json",
"Content-Type": "application/json"
},
"body": {
"productId": "1"
}
},
"response": {
"body": {
"productName": "spade"
}
}
}
]