Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

node-consumer-pact-interceptor

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-consumer-pact-interceptor - npm Package Compare versions

Comparing version 2.0.2 to 2.1.0

67

index.js

@@ -5,9 +5,12 @@ 'use strict';

var async = require('async');
var _ = require('lodash')
/**
* @param {string} providerState A state in pact under test
* @param {object} pact The pact specification
* @param {string/regex} The url to intercept
* @param cb The callback to fire on *each* interaction
* @param {function} setState The client state
* @param testSeriesCallback The callback to return test results
*/
module.exports = function(pact, url, setState, testSeriesCallback) {
module.exports = function(providerState, pact, url, setState, testSeriesCallback) {
if(!pact || !pact.provider || !pact.consumer || !pact.provider.name || !pact.consumer.name) {

@@ -21,32 +24,27 @@ throw "When creating an interceptor it's necessary to provide a consumer and provider";

}
else {
else if(_.some(pact.interactions, {provider_state: providerState})) { // provider state exists
var interaction = _.find(pact.interactions, function(o) { return o.provider_state == providerState; });
var asyncArray = [];
pact.interactions.forEach(function(interaction) {
asyncArray.push(function(testCallback){
var interceptor = intercept(pact);
interceptor.start(url, interaction, function(err) {
if(err){ // There was a failure in pact assertion(s)
interceptor.teardown();
testCallback(null, {
interaction: interaction,
failure: err
});
}
else {
interceptor.teardown();
testCallback(null, {
interaction: interaction,
failure: false
});
}
});
setState();
})
asyncArray.push(function(testCallback){
var interceptor = intercept(pact);
interceptor.start(url, interaction, function(err) {
if(err){ // There was a failure in pact assertion(s)
interceptor.teardown();
testCallback(null, {
interaction: interaction,
failure: err
});
}
else {
interceptor.teardown();
testCallback(null, {
interaction: interaction,
failure: false
});
}
});
setState(null, interaction, function(err){
//console.log("after interception", err)
});
})
});
async.series(asyncArray, function(err, results){

@@ -64,6 +62,6 @@ if(err){

}
else{
console.log(index, " Test success: ", test.interaction.description);
console.log("\t", test.interaction.provider_state);
}
// else{
// console.log(index, " Test success: ", test.interaction.description);
// console.log("\t", test.interaction.provider_state);
// }
});

@@ -74,2 +72,5 @@ testSeriesCallback(null, results)

}
else {
throw "No matching provider_state found in pact";
}
};
{
"name": "node-consumer-pact-interceptor",
"version": "2.0.2",
"version": "2.1.0",
"description": "A means to intercept outgoing requests for the purpose of validating consumer pacts",

@@ -25,3 +25,3 @@ "main": "index.js",

"lodash": "^3.10.1",
"node-consumer-pact-validation": "^1.0.3",
"node-consumer-pact-validation": "1.1.0",
"mitm": "^1.2.0",

@@ -28,0 +28,0 @@ "request": "^2.67.0",

@@ -9,3 +9,3 @@ ### Node Consumer Pact interceptor

See the `examples/passing-example` for the fastest way to get started.
See the `test/test.js` for the fastest way to get started.

@@ -12,0 +12,0 @@ ##### Workflow:

@@ -11,8 +11,9 @@ {

"description": "A request for an example resource",
"provider_state": "The provider has an example resource available",
"provider_state": "The provider has resource 1234 available",
"request": {
"method": "get",
"headers": {
"authorization": "some Auth header"
}
"authorization": "header 1234"
},
"path": "/1234"
},

@@ -39,8 +40,37 @@ "response": {

"description": "A request for an example resource",
"provider_state": "The provider has an example resource available",
"provider_state": "The provider has resource abcd available",
"request": {
"method": "get",
"headers": {
"authorization": "some Auth header"
"authorization": "header abcd"
},
"path": "/abcd"
},
"response": {
"status": 200,
"headers": {
"content-type": "application/json"
},
"body": {
"abc": "abcd"
}
}
},
{
"description": "A request for post",
"provider_state": "The provider receives a post request",
"request": {
"method": "post",
"path": "/blah",
"headers": {
"authorization": "some auth header for post"
},
"body": {
"some post data": [
{
"id": "1234",
"status": "new"
}
]
}
},

@@ -53,5 +83,27 @@ "response": {

"body": {
"abc": "abc"
"things": [
{
"id": "1234",
"data": {
"foo": "baz"
},
"status": "new"
}
]
}
}
},
{
"description": "A request for an example resource",
"provider_state": "The provider returns 404",
"request": {
"method": "get",
"headers": {
"authorization": "some auth header"
},
"path": "/snap"
},
"response": {
"status": 404
}
}

@@ -58,0 +110,0 @@ ],

@@ -9,25 +9,72 @@ 'use strict';

var expect = require('chai').expect;
var intercept = require('../index.js');
var verify = require('../index.js');
var _ = require('lodash')
var pact = require('./simple-GET-pact.json');
var pactSpec = require('./simple-GET-pact.json');
var httpReq;
var httpBody;
describe('When the provider receives a post request', function(){
var response;
var providerState = 'The provider receives a post request';
describe('When the request matches the specification', function(){
before(function(){
before(function(done){
var params = {
method: "post",
url: "http://somedomain.com/blah",
headers: { authorization: "some auth header for post" },
json: true,
body: {
"some post data": [
{
"id": "1234",
"status": "new"
}
]
}
};
var setState = function(){
request(params, function(err, res){
if(err) {
console.log(err);
}
else {
response = res;
}
});
};
verify(providerState, pact, /.*/, setState, function(err, testResults){
if(err){
console.error(err); //Failure in setting up tests
}
done();
});
});
it('Should intercept the request and provide the specified statusCode', function(){
var interaction = _.find(pact.interactions, function(o) { return o.provider_state == providerState; });
expect(response.body).to.eql(interaction.response.body);
expect(response.statusCode).to.equal(interaction.response.status);
});
});
describe('When the provider receives a request for resource 1234', function(){
var response;
var providerState = 'The provider has resource 1234 available';
before(function(done){
var params = {
method: "get",
url: "http://somedomain.com",
headers: { authorization: "some Auth header" },
url: "http://somedomain.com/1234",
headers: { authorization: "header 1234" },
json: true
};
var setState = function(err, interaction, cb){
request(params, function(err, req, body){
var setState = function(){
request(params, function(err, res){
if(err) {
//done(err);
console.log(err);
}
else {
httpReq = req;
httpBody = body;
response = res;
}

@@ -37,6 +84,7 @@ });

intercept(pactSpec, /.*/, setState, function(err, testResults){
verify(providerState, pact, /.*/, setState, function(err, testResults){
if(err){
console.error(err); //Failure in setting up tests
}
done();
});

@@ -46,4 +94,85 @@ });

it('Should intercept the request and provide the specified statusCode', function(){
expect(httpReq.statusCode).to.eql(pactSpec.interactions[0].response.status);
var interaction = _.find(pact.interactions, function(o) { return o.provider_state == providerState; });
expect(response.body).to.eql(interaction.response.body);
expect(response.statusCode).to.equal(interaction.response.status);
});
});
describe('When the provider receives a request for resource abcd', function(){
var response;
var providerState = 'The provider has resource abcd available';
before(function(done){
var params = {
method: "get",
url: "http://somedomain.com/abcd",
headers: { authorization: "header abcd" },
json: true
};
var setState = function(){
request(params, function(err, res){
if(err) {
console.log(err);
}
else {
response = res;
}
});
};
verify(providerState, pact, /.*/, setState, function(err, testResults){
if(err){
console.error(err); //Failure in setting up tests
}
done();
});
});
it('Should intercept the request and provide the specified statusCode', function(){
var interaction = _.find(pact.interactions, function(o) { return o.provider_state == providerState; });
expect(response.body).to.eql(interaction.response.body);
expect(response.statusCode).to.equal(interaction.response.status);
});
});
describe('When the provider returns a 404', function(){
var response;
var providerState = 'The provider returns 404';
before(function(done){
var params = {
method: "get",
url: "http://somedomain.com/snap",
headers: { authorization: "some auth header" },
json: true
};
var setState = function(){
request(params, function(err, res){
if(err) {
console.log(err);
}
else {
response = res;
}
});
};
verify(providerState, pact, /.*/, setState, function(err, testResults){
if(err){
console.error(err); //Failure in setting up tests
}
done();
});
});
it('Should intercept the request and provide the specified statusCode', function(){
var interaction = _.find(pact.interactions, function(o) { return o.provider_state == providerState; });
expect(response.body).to.eql(interaction.response.body);
expect(response.statusCode).to.equal(interaction.response.status);
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc