Stefan Tertan
11/02/2022, 5:04 PMconst path = require('path');
const {
MatchersV3: {like, regex},
MessageConsumerPact,
SpecificationVersion,
asynchronousBodyHandler,
} = require('@pact-foundation/pact');
const {KAFKA_TOPIC, streamHandler} = require('../kafka/consumer');
const messagePact = new MessageConsumerPact({
consumer: process.env.CONSUMER || 'AuditService',
provider: 'CardService_Async',
logLevel: 'warn',
dir: path.resolve(process.cwd(), 'pacts'),
spec: SpecificationVersion.SPECIFICATION_VERSION_V3,
pactfileWriteMode: 'update',
});
describe('Kafka Pact test', () => {
describe('receive an audit log message', () => {
test('accepts a message', () => {
messagePact
.expectsToReceive('a message event update')
.withMetadata({
'content-type': 'application/json',
'x-kafka-topic': KAFKA_TOPIC,
})
.withContent({
viewer: like('John Doe'),
card: {
id: like('08'),
type: regex(/^(CREDIT_CARD|PERSONAL_LOAN)$/, 'CREDIT_CARD'),
name: like('MyFlexiPay'),
version: like('v1'),
},
})
.verify(asynchronousBodyHandler(({content}) => streamHandler(content)));
});
});
});
And the following verification code:
const commonVerifierConfig = (commonEnv) => {
return {
logLevel: 'warn',
providerVersion: commonEnv.GIT_COMMIT_SHA,
providerVersionBranch: commonEnv.GIT_BRANCH,
publishVerificationResult: commonEnv.PUBLISH_VERIFICATION_RESULTS,
// Similar to state handlers?
messageProviders: {
'a message event update': () => JSON.parse(buildMessage(
'John Doe',
{id: '08', type: 'CREDIT_CARD', name: 'MyFlexiPay', version: 'v1'},
).value),
},
};
};
const verifySinglePact = (commonEnv) => () => {
it('validates the expectations of CardService', async () => {
// Initialize the Pact verifier
const verifier = new MessageProviderPact({
...commonVerifierConfig(commonEnv),
pactUrls: [process.env.PACT_URL],
});
// Verify pacts
const output = await verifier.verify();
console.log(output);
});
};
with buildMessage being:
const buildMessage = (viewer, card) => ({
value: JSON.stringify({viewer, card}),
headers: {
'content-type': 'application/json',
'x-kafka-topic': KAFKA_TOPIC,
},
});
The validation for the message body passes, but the one for the message metadata fails.
I have attached the generated contract.Stefan Tertan
11/02/2022, 5:13 PMmetadata is present in the contract, it shows undefined in PactflowStefan Tertan
11/03/2022, 10:46 AM4.0.0 for pactSpecification, even though I set spec: SpecificationVersion.SPECIFICATION_VERSION_V3, when generating it. In my pact (v4), metadata is spelt with lowercase d , whereas in Pactflow and v3 example (see screenshot) it is spelt with capital D -> metaDataStefan Tertan
11/03/2022, 10:47 AMStefan Tertan
11/03/2022, 12:27 PMmetaData is old .... when using spec: specificationVersion.SPECIFICATION_VERSION_V2
I get this in the pact:
"metadata": {
"content-type": "application/json",
"contentType": "application/json",
"x-kafka-topic": "audit-log"
}Matt (pactflow.io / pact-js / pact-go)
metadata is the correct key, not metaData
It’s possible Pactflow is not properly handling/parsing the V4 contract.
See also https://github.com/pact-foundation/pact-specification/tree/version-4#asynchronousmessages
In any case, would you mind please raising a bug so we can investigate? There are a few things going on here that will need investigatingStefan Tertan
11/04/2022, 1:16 PMMatt (pactflow.io / pact-js / pact-go)