doppleganger
Advanced tools
Comparing version 0.0.0 to 0.0.1
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; |
8914
8
158
3