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 1.0.1 to 1.1.0

13

lib/interceptor.js

@@ -17,3 +17,8 @@ 'use strict';

var urlToIntercept = url.parse(interceptorUrl);
if (typeof interceptorUrl === 'string'){
var urlToIntercept = url.parse(interceptorUrl);
}
else if(interceptorUrl instanceof RegExp){
var regexUrl = interceptorUrl;
}
var expectedRequest = pact.interactions[0].request;

@@ -23,3 +28,7 @@ var expectedResponse = pact.interactions[0].response;

mitm.on("request", function(req, res) {
if(req.headers.host === urlToIntercept.host && req.url === urlToIntercept.path){
var matchesOnRegex = regexUrl && req.url.match(regexUrl);
var matchesOnString = urlToIntercept && req.headers.host === urlToIntercept.host && req.url === urlToIntercept.path;
if(matchesOnRegex || matchesOnString){
var requestData = "";

@@ -26,0 +35,0 @@ req.on('data', function(data){

@@ -11,5 +11,21 @@ 'use strict';

module.exports = function(responseSpec, res){
res.statusCode = responseSpec.status;
res.writeHead(200, responseSpec.headers);
res.end(JSON.stringify(responseSpec.body));
var body;
var headers = responseSpec.headers;
var statusCode = responseSpec.status;
//Send statuscode and headers:
res.writeHead(statusCode, headers);
//Send body:
if(responseSpec.body && typeof responseSpec.body === 'string'){
body = responseSpec.body;
}
else if(responseSpec.body){ //Assume it's JSON
body = JSON.stringify(responseSpec.body);
res.end(body);
}
else { //No body to be sent
res.end();
}
};

2

package.json
{
"name": "node-consumer-pact-interceptor",
"version": "1.0.1",
"version": "1.1.0",
"description": "A means to intercept outgoing requests for the purpose of validating consumer pacts",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -33,2 +33,53 @@ 'use strict';

describe('When the request matches the specification and a regex', function(){
beforeEach(function(done){
intercept.start(/.*/, function(err, pactData){
if(err) {
done(err);
}
else {
pact = pactData;
}
});
var params = {
method: pactSpec.interactions[0].request.method,
url: "http://somedomain.com" + pactSpec.interactions[0].request.path,
headers: pactSpec.interactions[0].request.headers,
body: pactSpec.interactions[0].request.body,
json: true
};
request(params, function(err, req, body){
if(err) {
done(err);
}
else {
httpReq = req;
httpBody = body;
done();
}
});
});
it('Should intercept the request and provide the specified statusCode', function(){
expect(httpReq.statusCode).to.eql(pactSpec.interactions[0].response.status);
});
for(var header in pactSpec.interactions[0].response.headers) {
it('Should provide appropriate response header: ' + header, function(){
expect(httpReq.headers[header]).to.eql(pactSpec.interactions[0].response.headers[header]);
});
}
it('Should provide appropriate response body', function(){
expect(httpBody).to.eql(pactSpec.interactions[0].response.body);
});
it('Should intercept the request and return the pact object after verification', function(){
expect(pact).to.eql(pactSpec);
});
});
describe('When the request matches the specification', function(){

@@ -35,0 +86,0 @@

Sorry, the diff of this file is not supported yet

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