Step 4 - Verify the provider
Move to step 4
git checkout step4
- Javascript
npm install
Learning Objectives
Step | Title | Concept Covered | Learning objectives | Further Reading |
---|---|---|---|---|
step 4 | Verify the consumer pact with the Provider API | Provider side pact test |
|
We need to make the pact file (the contract) that was produced from the consumer test available to the Provider module. This will help us verify that the provider can meet the requirements as set out in the contract. For now, we'll hard code the path to where it is saved in the consumer test, in step 11 we investigate a better way of doing this.
Now let's make a start on writing Pact tests to validate the consumer contract:
In provider/product/product.pact.test.js
:
- Javascript
const { Verifier } = require('@pact-foundation/pact');
const path = require('path');
// Setup provider server to verify
const app = require('express')();
app.use(require('./product.routes'));
const server = app.listen("8080");
describe("Pact Verification", () => {
it("validates the expectations of ProductService", () => {
const opts = {
logLevel: "INFO",
providerBaseUrl: "http://127.0.0.1:8080",
provider: "ProductService",
providerVersion: "1.0.0",
pactUrls: [
path.resolve(__dirname, '../../consumer/pacts/FrontendWebsite-ProductService.json')
]
};
return new Verifier(opts).verifyProvider().then(output => {
console.log(output);
}).finally(() => {
server.close();
});
})
});
// add it under scripts
"test:pact": "npx jest --testTimeout=30000 --testMatch \"**/*.pact.test.js\""
We now need to validate the pact generated by the consumer is valid, by executing it against the running service provider, which should fail:
❯ npm run test:pact --prefix provider
Verifying a pact between FrontendWebsite and ProductService
get product with ID 10
returns a response which
has status code 200 (FAILED)
includes headers
"Content-Type" with value "application/json; charset=utf-8" (FAILED)
has a matching body (FAILED)
get all products
returns a response which
has status code 200 (OK)
includes headers
"Content-Type" with value "application/json; charset=utf-8" (OK)
has a matching body (OK)
Failures:
1) Verifying a pact between FrontendWebsite and ProductService Given product with ID 10 exists - get product with ID 10
1.1) has a matching body
expected 'application/json;charset=utf-8' body but was 'text/html;charset=utf-8'
1.2) has status code 200
expected 200 but was 404
1.3) includes header 'Content-Type' with value 'application/json; charset=utf-8'
Expected header 'Content-Type' to have value 'application/json; charset=utf-8' but was 'text/html; charset=utf-8'
The test has failed, as the expected path /products/{id}
is returning 404. We incorrectly believed our provider was following a RESTful design, but the authors were too lazy to implement a better routing solution 🤷🏻♂️.
The correct endpoint which the consumer should call is /product/{id}
.
Move on to step 5