Pact JS End-to-End Example
Using a simple animal dating API, we demonstrate the following Pact features:
- Consumer testing and pact file generation, including advanced features like:
- Sharing Pacts by publishing to and retrieving from a Pact Broker
- Provider side verification
This comprises a complete E2E example that can be used as a basis for projects.
The Example Project
[Matching API] -> [Profile API]+(DB)
Provider (Profile API)
Provides Animal profile information, including interests, zoo location and other personal details.
Consumer (Matching API)
Given an animal profile, recommends a suitable partner based on similar interests.
Running the tests
npm install
npm test:consumer
- Run consumer testsnpm test:publish
- Publish contracts to the brokernpm test:provider
- Run provider tests
Running the API
If you want to experiment with the API to get an understanding:
npm run api
- Runs the both provider and consumer API
or individually :
npm run provider
- Runs the provider API (animal service)npm run consumer
- Runs the consumer API (matching service)
Animal Profile API
The APIs are described below, including a bunch of cURL statements to invoke them. There is also a Postman Collection.
GET /animals
curl -X GET "http://localhost:8081/animals"
GET /animals/:id
curl -X GET "http://localhost:8081/animals/1"
GET /animals/available
curl -X GET http://localhost:8081/animals/available
POST /animals
curl -X POST -H "Content-Type: application/json" -d '{
"first_name": "aoeu",
"last_name": "aoeu",
"age": 21,
"gender": "M",
"location": {
"description": "Melbourne Zoo",
"country": "Australia",
"post_code": 3000
},
"eligibility": {
"available": true,
"previously_married": false
},
"interests": [
"walks in the garden/meadow",
"munching on a paddock bomb",
"parkour"
]
}' "http://localhost:8081/animals"
Matching service
GET /suggestions/:id
curl -X GET http://localhost:8080/suggestions/1
Viewing contracts with the Pact Broker
A test Pact Boker is running at https://test.pact.dius.com.au:
- Username:
dXfltyFMgNOFZAxr8io9wJ37iUpY42M
- Password:
O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1
Or use the API:
curl -v -u 'dXfltyFMgNOFZAxr8io9wJ37iUpY42M:O5AIZWxelWbLvqMd8PkAVycBJh2Psyg1' https://test.pact.dius.com.au