New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

doppleganger

Package Overview
Dependencies
Maintainers
3
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

doppleganger - npm Package Compare versions

Comparing version 0.0.0 to 0.0.1

lib/compare.js

51

index.js
var http = require('http'),
url = require('url'),
_ = require('lodash'),
PresentAndEqualMatcher = require('./lib/present-and-equal-matcher'),
oldRequest;
oldRequest = http.request;
http.request = function(options) {
if (typeof options === 'string') {
options = url.parse(options);
}
console.log(options);
};
var Doppleganger = function(baseDomain) {
var baseDomain, matchBody, responseData, finisher;
var baseDomain, matchBody, responseData;
this.baseDomain = baseDomain;
this.matchers = [];
};

@@ -25,4 +18,5 @@

Doppleganger.prototype.get = function(matchBody) {
this.matchBody = matchBody; //consider encapsulating this into an object of its own
//presentAndEqual is the first matcher
Doppleganger.prototype.presentAndEqual = function(matchBody) {
this.matchers.push(new PresentAndEqualMatcher(matchBody));
return this;

@@ -36,9 +30,30 @@ };

Doppleganger.prototype.end = function(ender) {
ender({}, this.responseData);
};
oldRequest = http.request;
module.exports = function(baseDomain) {
var doppleGanger = new Doppleganger(baseDomain);
return doppleGanger;
var doppleganger = new Doppleganger(baseDomain);
http.request = function(options, cb) {
var errorStr = '';
var err;
if (typeof options === 'string') {
options = url.parse(options, true);
}
//iterate through the matchers
errorStr = doppleganger.matchers.reduce(function(errorStr, matcher) {
return errorStr + matcher.match(options);
}, errorStr);
if (errorStr.length > 0) {
err = new Error(errorStr);
}
cb(err, doppleganger.responseData);
};
return doppleganger;
};
{
"name": "doppleganger",
"version": "0.0.0",
"version": "0.0.1",
"description": "",

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

@@ -1,41 +0,49 @@

var http = require('http'),
url = require('url'),
oldRequest;
var _ = require('lodash'),
compare = require('./compare');
oldRequest = http.request;
var PresentAndEqualMatcher = module.exports = function(matchBody) {
this.matchBody = matchBody;
}
http.request = function(options) {
if (typeof options === 'string') {
options = url.parse(options);
PresentAndEqualMatcher.prototype.match = function(options) {
var self = this;
var errorStr = '';
var requiredQueryKeys = Object.keys(this.matchBody.query || {});
var queryKeys = Object.keys(options.query);
var diff = _.difference(requiredQueryKeys, queryKeys)
if (diff.length > 0) {
errorStr += options.path +' is missing query parameters: ' + diff.join(',') + '\n';
}
console.log(options);
};
requiredQueryKeys.forEach(function(key) {
if (!compare(self.matchBody.query[key], options.query[key])) {
errorStr += (options.path + ' contains [' + key + '] but values do not match: ' +
self.matchBody.query[key] + ' !== ' + options.query[key]) + '\n';
}
});
var Doppleganger = function(baseDomain) {
var baseDomain, matchBody, responseData, finisher;
this.baseDomain = baseDomain;
};
if (self.matchBody.params && self.matchBody.params.length) {
var filterParam = function(param) { return param !== null && param !== ''; };
var requiredParams = self.matchBody.params;
var paramBits = requiredParams.split(/\//g).filter(filterParam);
var pathBits = options.pathname.split(/\//g).filter(filterParam);
Doppleganger.base = function(baseURL) {
this.baseURL = baseURL;
return this;
};
if (paramBits.length !== pathBits.length) {
errorStr += options.path + ' contains [' + pathBits.length + '] parameters, but this does not match the [' +
paramBits.length + '] parameters in the match route.\n';
} else {
Doppleganger.get = function(matchBody) {
this.matchBody = matchBody; //consider encapsulating this into an object of its own
return this;
};
paramBits.forEach(function(parameter, index) {
if (parameter[0] !== ":" &&
parameter !== pathBits[index]) {
Doppleganger.respond = function(responseData) {
this.responseData = responseData;
return this;
};
errorStr += options.path + ' contains [' + pathBits[index] + '] at index [' + index + '], ' +
'but the route string asks for [' + parameter + '].\n';
}
});
}
}
Doppleganger.end = function(ender) {
ender({}, {}); //fill this in later
return errorStr;
};
module.exports = function(baseDomain) {
var doppleGanger = new Doppleganger(baseDomain);
return doppleGanger;

@@ -9,2 +9,3 @@ var dg = require('./../'),

.get( { query: { one: 1 }} )
.respond( { header: { 'code': 200 }, body: '{ two: 2 }' } )
.end(function(err, res) {

@@ -11,0 +12,0 @@ expect(res).to.be.ok;

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