node message consumer example
This commit is contained in:
35
pact/pact-node-messages/common/hero-created-message.js
Normal file
35
pact/pact-node-messages/common/hero-created-message.js
Normal file
@@ -0,0 +1,35 @@
|
||||
class HeroCreatedMessage {
|
||||
|
||||
constructor(name, superpower, universe, id) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.superpower = superpower;
|
||||
this.universe = universe;
|
||||
}
|
||||
|
||||
static validateUniverse(message) {
|
||||
if (typeof message.universe !== 'string') {
|
||||
throw new Error(`Hero universe must be a string! Invalid value: ${message.universe}`)
|
||||
}
|
||||
}
|
||||
|
||||
static validateSuperpower(message) {
|
||||
if (typeof message.superpower !== 'string') {
|
||||
throw new Error(`Hero superpower must be a string! Invalid value: ${message.superpower}`)
|
||||
}
|
||||
}
|
||||
|
||||
static validateName(message) {
|
||||
if (typeof message.name !== 'string') {
|
||||
throw new Error(`Hero name must be a string! Invalid value: ${message.name}`);
|
||||
}
|
||||
}
|
||||
|
||||
static validateId(message) {
|
||||
if (typeof message.id !== 'number') {
|
||||
throw new Error(`Hero id must be a number! Invalid value: ${message.id}`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = HeroCreatedMessage;
|
||||
15
pact/pact-node-messages/consumer/hero-event-handler.js
Normal file
15
pact/pact-node-messages/consumer/hero-event-handler.js
Normal file
@@ -0,0 +1,15 @@
|
||||
const HeroCreatedMessage = require('../common/hero-created-message');
|
||||
|
||||
function handleHeroCreatedEvent(message) {
|
||||
|
||||
HeroCreatedMessage.validateId(message);
|
||||
HeroCreatedMessage.validateName(message);
|
||||
HeroCreatedMessage.validateSuperpower(message);
|
||||
HeroCreatedMessage.validateUniverse(message);
|
||||
|
||||
// pass message into business logic
|
||||
// note that the business logic should be mocked away for the contract test
|
||||
|
||||
}
|
||||
|
||||
module.exports = handleHeroCreatedEvent;
|
||||
35
pact/pact-node-messages/consumer/hero-event-handler.spec.js
Normal file
35
pact/pact-node-messages/consumer/hero-event-handler.spec.js
Normal file
@@ -0,0 +1,35 @@
|
||||
const {MessageConsumerPact, Matchers, synchronousBodyHandler} = require('@pact-foundation/pact');
|
||||
const handleHeroCreatedEvent = require('./hero-event-handler');
|
||||
const path = require('path');
|
||||
|
||||
describe("message consumer", () => {
|
||||
|
||||
const messagePact = new MessageConsumerPact({
|
||||
consumer: "node-message-consumer",
|
||||
provider: "node-message-provider",
|
||||
dir: path.resolve(process.cwd(), "pacts"),
|
||||
pactfileWriteMode: "update",
|
||||
logLevel: "info",
|
||||
});
|
||||
|
||||
describe("'hero created' message Handler", () => {
|
||||
|
||||
it("should accept a valid 'hero created' message", (done) => {
|
||||
return messagePact
|
||||
.expectsToReceive("a 'hero created' message")
|
||||
.withContent({
|
||||
id: Matchers.like(42),
|
||||
name: Matchers.like("Superman"),
|
||||
superpower: Matchers.like("flying"),
|
||||
universe: Matchers.term({generate: "DC", matcher: "^(DC|Marvel)$"})
|
||||
})
|
||||
.withMetadata({
|
||||
"content-type": "application/json",
|
||||
})
|
||||
.verify(synchronousBodyHandler(handleHeroCreatedEvent))
|
||||
.then(done());
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
2596
pact/pact-node-messages/package-lock.json
generated
Normal file
2596
pact/pact-node-messages/package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
17
pact/pact-node-messages/package.json
Normal file
17
pact/pact-node-messages/package.json
Normal file
@@ -0,0 +1,17 @@
|
||||
{
|
||||
"name": "pact-node-messages",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"test": "mocha consumer/*.spec.js",
|
||||
"pact-message": "pact-message update {}"
|
||||
},
|
||||
"author": "Tom Hombergs",
|
||||
"license": "MIT",
|
||||
"devDependencies": {
|
||||
"@pact-foundation/pact": "^7.0.3",
|
||||
"chai": "^4.2.0",
|
||||
"mocha": "^5.2.0"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user