Comparing version 1.0.10 to 1.0.11
{ | ||
"name": "pactum", | ||
"version": "1.0.10", | ||
"version": "1.0.11", | ||
"description": "REST API endpoint testing tool with a mock server & compatible with pact.io for contract testing", | ||
@@ -23,3 +23,4 @@ "main": "src/index.js", | ||
"pact-js", | ||
"pact.io" | ||
"pact.io", | ||
"pactum" | ||
], | ||
@@ -26,0 +27,0 @@ "repository": { |
@@ -27,2 +27,3 @@ # pactum | ||
* [Pact Interaction](#pact-interaction) | ||
* [Mock Server](#mock-server) | ||
@@ -526,2 +527,3 @@ ## Usage | ||
| ----------------------- | ------- | -------- | -------------------------- | | ||
| id | string | optional | id of the interaction | | ||
| consumer | string | optional | name of the consumer | | ||
@@ -609,2 +611,3 @@ | provider | string | optional | name of the provider | | ||
| ----------------------- | ------- | -------- | -------------------------- | | ||
| id | string | optional | id of the interaction | | ||
| consumer | string | required | name of the consumer | | ||
@@ -639,2 +642,3 @@ | provider | string | required | name of the provider | | ||
Use `addDefaultMockInteraction()` and `addDefaultPactInteraction()` to add default interactions to the mock server. | ||
To add multiple use `addDefaultMockInteractions()` and `addDefaultPactInteractions()`. | ||
@@ -641,0 +645,0 @@ ```javascript |
@@ -15,2 +15,3 @@ const Interaction = require('../models/interaction'); | ||
* @typedef {object} Interaction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} [consumer] - name of the consumer | ||
@@ -80,3 +81,3 @@ * @property {string} [provider] - name of the provider | ||
/** | ||
* add an mock interaction to default list | ||
* add a mock interaction to default list | ||
* @param {Interaction} interaction - mock interaction | ||
@@ -91,5 +92,23 @@ */ | ||
/** | ||
* add an mock interaction to default list | ||
* @param {Interaction} interaction - mock interaction | ||
* add mock interactions to default list | ||
* @param {Interaction[]} interactions - mock interactions array | ||
*/ | ||
addDefaultMockInteractions(interactions) { | ||
if (!Array.isArray(interactions)) { | ||
// use a new type of error | ||
throw new PactumConfigurationError(`Invalid mock interactions array passed - ${interactions}`); | ||
} | ||
const ids = []; | ||
for (let i = 0; i < interactions.length; i++) { | ||
const interactionObj = new Interaction(interactions[i], true); | ||
_server.addDefaultInteraction(interactionObj.id, interactionObj); | ||
ids.push(interactionObj.id); | ||
} | ||
return ids; | ||
} | ||
/** | ||
* add a pact interaction to default list | ||
* @param {Interaction} interaction - pact interaction | ||
*/ | ||
addDefaultPactInteraction(interaction) { | ||
@@ -104,2 +123,20 @@ const interactionObj = new Interaction(interaction); | ||
/** | ||
* add pact interactions to default list | ||
* @param {Interaction[]} interactions - mock interactions array | ||
*/ | ||
addDefaultPactInteractions(interactions) { | ||
if (!Array.isArray(interactions)) { | ||
// use a new type of error | ||
throw new PactumConfigurationError(`Invalid pact interactions array passed - ${interactions}`); | ||
} | ||
const ids = []; | ||
for (let i = 0; i < interactions.length; i++) { | ||
const interactionObj = new Interaction(interactions[i], false); | ||
_server.addDefaultInteraction(interactionObj.id, interactionObj); | ||
ids.push(interactionObj.id); | ||
} | ||
return ids; | ||
} | ||
/** | ||
* removes specified default interaction from server | ||
@@ -106,0 +143,0 @@ * @param {string} interactionId - id of the interaction |
@@ -10,4 +10,19 @@ const fs = require('fs'); | ||
pacts: new Map(), | ||
/** @type {Map<string, number>} */ | ||
interactionExerciseCounter: new Map(), | ||
/** | ||
* increments interaction exercise count | ||
* @param {string} id - interaction id | ||
*/ | ||
updateInteractionExerciseCounter(id) { | ||
if (this.interactionExerciseCounter.has(id)) { | ||
const count = this.interactionExerciseCounter.get(id); | ||
this.interactionExerciseCounter.set(id, ++count); | ||
} else { | ||
this.interactionExerciseCounter.set(id, 1); | ||
} | ||
}, | ||
/** | ||
* add interaction in the store | ||
@@ -17,3 +32,3 @@ * @param {Interaction} interaction | ||
addInteraction(interaction) { | ||
const { provider, state, uponReceiving, mock } = interaction; | ||
const { id, provider, state, uponReceiving, mock } = interaction; | ||
const consumer = interaction.consumer || config.pact.consumer; | ||
@@ -30,2 +45,3 @@ if (consumer && provider && state && uponReceiving && !mock) { | ||
const pactInteraction = new PactInteraction(); | ||
pactInteraction.id = id; | ||
pactInteraction.providerState = state; | ||
@@ -51,2 +67,12 @@ pactInteraction.description = uponReceiving; | ||
} | ||
for (let i = 0; i < pact.interactions.length; i++) { | ||
const interaction = pact.interactions[i]; | ||
const id = interaction.id; | ||
if (!this.interactionExerciseCounter.has(id)) { | ||
console.log('PACTUM', 'Pact interaction not exercised'); | ||
console.log('PACTUM', 'Request', interaction.request); | ||
console.log('PACTUM', 'Response', { status: interaction.response.status, body: interaction.response.body }); | ||
} | ||
delete interaction.id; | ||
} | ||
fs.writeFileSync(`${dir}/${key}.json`, JSON.stringify(pact, null, 2)); | ||
@@ -53,0 +79,0 @@ } |
@@ -17,2 +17,3 @@ const Spec = require('./models/spec'); | ||
* @typedef {object} Interaction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} [consumer] - name of the consumer | ||
@@ -19,0 +20,0 @@ * @property {string} [provider] - name of the provider |
@@ -46,4 +46,4 @@ const helper = require('../helpers/helper'); | ||
this.validateInteraction(rawInteraction, mock); | ||
const { port, consumer, provider, state, uponReceiving, withRequest, willRespondWith } = rawInteraction; | ||
this.id = helper.getRandomId(); | ||
const { id, port, consumer, provider, state, uponReceiving, withRequest, willRespondWith } = rawInteraction; | ||
this.id = id || helper.getRandomId(); | ||
this.port = port || config.mock.port; | ||
@@ -50,0 +50,0 @@ this.mock = mock; |
@@ -151,2 +151,3 @@ const express = require('express'); | ||
if (interaction) { | ||
store.updateInteractionExerciseCounter(interaction.id); | ||
interaction.exercised = true; | ||
@@ -153,0 +154,0 @@ interactionExercised = true; |
@@ -16,2 +16,3 @@ const rp = require('request-promise'); | ||
* @typedef {object} Interaction | ||
* @property {string} [id] - unique id of the interaction | ||
* @property {string} [consumer] - name of the consumer | ||
@@ -18,0 +19,0 @@ * @property {string} [provider] - name of the provider |
89040
2009
685