Permalink
Browse files

Migrate to lab

  • Loading branch information...
1 parent 53363b3 commit ffed41434695856166638e90e215fec385a07e29 @hueniverse hueniverse committed Mar 18, 2013
Showing with 256 additions and 238 deletions.
  1. +1 −1 LICENSE
  2. +8 −9 Makefile
  3. +90 −90 lib/index.js
  4. +4 −8 package.json
  5. +153 −130 test/index.js
View
@@ -8,7 +8,7 @@ modification, are permitted provided that the following conditions are met:
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
- * Neither the name of Eran Hammer nor the
+ * Neither the name of Walmart nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
View
@@ -1,12 +1,11 @@
-REPORTER = dot
-
test:
- @NODE_ENV=test ./node_modules/.bin/mocha --recursive --reporter $(REPORTER) --ignore-leaks --timeout 3000
-
-test-cov:
- @NODE_ENV=test ./node_modules/.bin/mocha --require blanket --recursive --ignore-leaks --timeout 3000 -R travis-cov
-
+ @./node_modules/.bin/lab
+test-cov:
+ @./node_modules/.bin/lab -r threshold -t 100
test-cov-html:
- @NODE_ENV=test ./node_modules/.bin/mocha --require blanket --recursive --ignore-leaks --timeout 3000 -R html-cov > coverage.html
+ @./node_modules/.bin/lab -r html -o coverage.html
+complexity:
+ @./node_modules/.bin/cr -o complexity.md -f markdown lib
+
+.PHONY: test test-cov test-cov-html complexity
-.PHONY: test test-cov test-cov-html
View
@@ -10,7 +10,7 @@ var Stream = require('stream');
var internals = {};
-internals.Request = function (options) {
+internals.Request = function (options) {
var self = this;
@@ -33,156 +33,156 @@ internals.Request = function (options) {
this._shot.isPaused = false;
this._shot.simulate = options.simulate || {};
- process.nextTick(function () {
+ process.nextTick(function () {
- internals.play.call(self);
+ internals.play.call(self);
});
- return this;
+ return this;
};
Util.inherits(internals.Request, Stream);
-internals.play = function () {
+internals.play = function () {
- if (this._shot.isPaused) {
- return;
+ if (this._shot.isPaused) {
+ return;
}
- if (this._shot.isPlayed) {
- return;
+ if (this._shot.isPlayed) {
+ return;
}
this._shot.isPlayed = true;
if (this._shot.payload &&
- this._shot.listeners.data) {
+ this._shot.listeners.data) {
- for (var i = 0, il = this._shot.listeners.data.length; i < il; ++i) {
- this._shot.listeners.data[i](this._shot.payload);
- }
+ for (var i = 0, il = this._shot.listeners.data.length; i < il; ++i) {
+ this._shot.listeners.data[i](this._shot.payload);
+ }
}
- if (this._shot.simulate.error) {
+ if (this._shot.simulate.error) {
var error = new Error('Simulated');
- for (i = 0, il = this._shot.listeners.error.length; i < il; ++i) {
- this._shot.listeners.error[i](error);
- }
+ for (i = 0, il = this._shot.listeners.error.length; i < il; ++i) {
+ this._shot.listeners.error[i](error);
+ }
}
- if (this._shot.simulate.close) {
- for (i = 0, il = this._shot.listeners.close.length; i < il; ++i) {
- this._shot.listeners.close[i]();
- }
+ if (this._shot.simulate.close) {
+ for (i = 0, il = this._shot.listeners.close.length; i < il; ++i) {
+ this._shot.listeners.close[i]();
+ }
}
if (this._shot.listeners.end &&
this._shot.simulate.end !== false) { // 'end' defaults to true
- for (i = 0, il = this._shot.listeners.end.length; i < il; ++i) {
- this._shot.listeners.end[i]();
- }
- }
+ for (i = 0, il = this._shot.listeners.end.length; i < il; ++i) {
+ this._shot.listeners.end[i]();
+ }
+ }
};
-internals.Request.prototype.on = internals.Request.prototype.addListener = function (event, callback) {
+internals.Request.prototype.on = internals.Request.prototype.addListener = function (event, callback) {
this._shot.listeners[event] = this._shot.listeners[event] || [];
- this._shot.listeners[event].push(callback);
+ this._shot.listeners[event].push(callback);
};
-internals.Request.prototype.pause = function () {
+internals.Request.prototype.pause = function () {
- if (this._shot.isPaused) {
- return;
+ if (this._shot.isPaused) {
+ return;
}
- this._shot.isPaused = true;
+ this._shot.isPaused = true;
};
-internals.Request.prototype.resume = function () {
+internals.Request.prototype.resume = function () {
- if (!this._shot.isPaused) {
- return;
+ if (!this._shot.isPaused) {
+ return;
}
this._shot.isPaused = false;
- internals.play.call(this);
+ internals.play.call(this);
};
-internals.Request.prototype.setEncoding = function () {
+internals.Request.prototype.setEncoding = function () {
};
-internals.Request.prototype.destroy = function () {
+internals.Request.prototype.destroy = function () {
- this._shot.listeners = {};
+ this._shot.listeners = {};
};
-internals.Response = function (req, onEnd) {
+internals.Response = function (req, onEnd) {
Http.ServerResponse.call(this, { method: req.method, httpVersionMajor: 1, httpVersionMinor: 1 });
- this._shot = {
+ this._shot = {
req: req,
- onEnd: onEnd
+ onEnd: onEnd
};
- return this;
+ return this;
};
Util.inherits(internals.Response, Http.ServerResponse);
-internals.Response.prototype.writeHead = function () {
+internals.Response.prototype.writeHead = function () {
var headers = ((arguments.length === 2 && typeof arguments[1] === 'object') ? arguments[1] : (arguments.length === 3 ? arguments[2] : {}));
var result = Http.ServerResponse.prototype.writeHead.apply(this, arguments);
this._headers = this._headers || {};
var keys = Object.keys(headers);
- for (var i = 0, il = keys.length; i < il; ++i) {
- this._headers[keys[i]] = headers[keys[i]];
+ for (var i = 0, il = keys.length; i < il; ++i) {
+ this._headers[keys[i]] = headers[keys[i]];
}
- return result;
+ return result;
};
-internals.Response.prototype.write = function (chunk, encoding) {
+internals.Response.prototype.write = function (chunk, encoding) {
Http.ServerResponse.prototype.write.call(this, chunk, encoding);
- return true;
+ return true;
};
-internals.Response.prototype.end = function (data, encoding) {
+internals.Response.prototype.end = function (data, encoding) {
var self = this;
- if (!this._shot) {
- return;
+ if (!this._shot) {
+ return;
}
Http.ServerResponse.prototype.end.call(this, data, encoding);
this.emit('finish'); // Will not be emitted internally on an inactive socket
// Prepare response object
- var res = {
- raw: {
+ var res = {
+ raw: {
req: this._shot.req,
- res: this
+ res: this
},
headers: this._headers,
- statusCode: this.statusCode
+ statusCode: this.statusCode
};
// Parse payload
@@ -191,65 +191,65 @@ internals.Response.prototype.end = function (data, encoding) {
if (self.output &&
self.output instanceof Array &&
- self.output.length) {
+ self.output.length) {
- for (var i = 0, il = self.output.length; i < il; ++i) {
- if (self.output[i] instanceof Buffer) {
- output += self.output[i].toString();
+ for (var i = 0, il = self.output.length; i < il; ++i) {
+ if (self.output[i] instanceof Buffer) {
+ output += self.output[i].toString();
}
- else {
- output += new Buffer(self.output[i], self.outputEncodings[i]).toString();
- }
- }
+ else {
+ output += new Buffer(self.output[i], self.outputEncodings[i]).toString();
+ }
+ }
}
var sep = output.indexOf('\r\n\r\n');
- var payloadBlock = output.slice(sep + 4);
- var headerBlock = output.slice(0, sep);
-
- if (headerBlock.indexOf('Transfer-Encoding: chunked') !== -1) {
- var rest = payloadBlock;
- res.payload = '';
-
- while (rest) {
- var next = rest.indexOf('\r\n');
- var size = parseInt(rest.slice(0, next), 16);
- if (size === 0) {
- break;
- }
-
- res.payload += rest.substr(next + 2, size);
- rest = rest.slice(next + 2 + size + 2);
- }
- }
- else {
- res.payload = payloadBlock;
- }
+ var payloadBlock = output.slice(sep + 4);
+ var headerBlock = output.slice(0, sep);
+
+ if (headerBlock.indexOf('Transfer-Encoding: chunked') !== -1) {
+ var rest = payloadBlock;
+ res.payload = '';
+
+ while (rest) {
+ var next = rest.indexOf('\r\n');
+ var size = parseInt(rest.slice(0, next), 16);
+ if (size === 0) {
+ break;
+ }
+
+ res.payload += rest.substr(next + 2, size);
+ rest = rest.slice(next + 2 + size + 2);
+ }
+ }
+ else {
+ res.payload = payloadBlock;
+ }
// Callback response
var onEnd = this._shot.onEnd;
delete this._shot;
- onEnd(res);
+ onEnd(res);
};
-internals.Response.prototype.destroy = function () {
+internals.Response.prototype.destroy = function () {
};
-exports.inject = function (dispatchFunc, options, callback) {
+exports.inject = function (dispatchFunc, options, callback) {
var req = new internals.Request(options);
var res = new internals.Response(req, callback);
- dispatchFunc(req, res);
+ dispatchFunc(req, res);
};
-exports.isInjection = function (obj) {
+exports.isInjection = function (obj) {
- return (obj instanceof internals.Request || obj instanceof internals.Response);
+ return (obj instanceof internals.Request || obj instanceof internals.Response);
};
View
@@ -1,7 +1,7 @@
{
"name": "shot",
"description": "Injects a fake HTTP request/response into a node HTTP server",
- "version": "0.1.2",
+ "version": "0.1.3",
"author": "Eran Hammer <eran@hueniverse.com> (http://hueniverse.com)",
"contributors":[
],
@@ -19,15 +19,11 @@
"dependencies": {
},
"devDependencies": {
- "mocha": "1.x.x",
- "chai": "1.x.x",
- "blanket": "1.0.x",
- "travis-cov": "0.2.x"
+ "lab": "0.0.x",
+ "complexity-report": "0.x.x"
},
"scripts": {
- "test": "make test && make test-cov",
- "blanket": { "pattern": "//^((?!\/node_modules\/)(?!\/test\/).)*$/ig", "onlyCwd": true, "data-cover-flags": { "branchTracking": true } },
- "travis-cov": { "threshold": 100 }
+ "test": "make test-cov"
},
"licenses": [
{
Oops, something went wrong.

0 comments on commit ffed414

Please sign in to comment.