Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node v0.10 #5

Merged
merged 9 commits into from
Apr 5, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
language: node_js

node_js:
- 0.8
- 0.10

113 changes: 38 additions & 75 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internals.Request = function (options) {

var self = this;

Stream.call(this);
Stream.Readable.call(this);

// options: method, url, payload, headers

Expand All @@ -26,103 +26,53 @@ internals.Request = function (options) {

// Use _shot namespace to avoid collision with Node

this._shot = {};
this._shot.payload = options.payload;
this._shot.listeners = {};
this._shot.isPlayed = false;
this._shot.isPaused = false;
this._shot.simulate = options.simulate || {};

process.nextTick(function () {

internals.play.call(self);
});
this._shot = {
payload: options.payload || null,
listeners: {},
isDone: false,
simulate: options.simulate || {}
};

return this;
};

Util.inherits(internals.Request, Stream);
Util.inherits(internals.Request, Stream.Readable);


internals.play = function () {
internals.Request.prototype._read = function (size) {

if (this._shot.isPaused) {
return;
}
var self = this;

if (this._shot.isPlayed) {
if (self._shot.isDone) {
self.push(null);
return;
}

this._shot.isPlayed = true;
self._shot.isDone = true;

if (this._shot.payload &&
this._shot.listeners.data) {
setImmediate(function () {

for (var i = 0, il = this._shot.listeners.data.length; i < il; ++i) {
this._shot.listeners.data[i](this._shot.payload);
if (self._shot.payload) {
self.push(self._shot.payload);
}
}

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);
if (self._shot.simulate.error) {
self.emit('error', new Error('Simulated'));
}
}

if (this._shot.simulate.close) {
for (i = 0, il = this._shot.listeners.close.length; i < il; ++i) {
this._shot.listeners.close[i]();
if (self._shot.simulate.close) {
self.emit('close');
}
}

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]();
if (self._shot.simulate.end !== false) { // 'end' defaults to true
self.push(null);
}
}
};


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);
};


internals.Request.prototype.pause = function () {

if (this._shot.isPaused) {
return;
}

this._shot.isPaused = true;
};


internals.Request.prototype.resume = function () {

if (!this._shot.isPaused) {
return;
}

this._shot.isPaused = false;
internals.play.call(this);
};


internals.Request.prototype.setEncoding = function () {

});
};


internals.Request.prototype.destroy = function () {

this._shot.listeners = {};
};


Expand Down Expand Up @@ -203,7 +153,8 @@ internals.Response.prototype.end = function (data, encoding) {
}
}

var sep = output.indexOf('\r\n\r\n');
var CRLF = '\r\n';
var sep = output.indexOf(CRLF + CRLF);
var payloadBlock = output.slice(sep + 4);
var headerBlock = output.slice(0, sep);

Expand All @@ -212,15 +163,27 @@ internals.Response.prototype.end = function (data, encoding) {
res.payload = '';

while (rest) {
var next = rest.indexOf('\r\n');
var next = rest.indexOf(CRLF);
var size = parseInt(rest.slice(0, next), 16);
if (size === 0) {
rest = rest.slice(3);
break;
}

res.payload += rest.substr(next + 2, size);
rest = rest.slice(next + 2 + size + 2);
}

if (rest) {
var headers = rest.split(CRLF);
headers.forEach(function (header) {

var parts = header.split(':');
if (parts.length === 2) {
self._headers[parts[0].trim().toLowerCase()] = parts[1].trim();
}
});
}
}
else {
res.payload = payloadBlock;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "shot",
"description": "Injects a fake HTTP request/response into a node HTTP server",
"version": "0.1.3",
"version": "0.2.0",
"author": "Eran Hammer <eran@hueniverse.com> (http://hueniverse.com)",
"contributors":[
],
Expand All @@ -14,12 +14,12 @@
"test"
],
"engines": {
"node": "0.8.x"
"node": "0.10.x"
},
"dependencies": {
},
"devDependencies": {
"lab": "0.0.x",
"lab": "0.1.x",
"complexity-report": "0.x.x"
},
"scripts": {
Expand Down
Loading