Comparing version 0.1.7 to 0.1.9
@@ -10,4 +10,4 @@ var Cookies = require('cookies'); | ||
/** | ||
* Mock request object for sending to the router without needing a server. | ||
**/ | ||
* Mock request object for sending to the router without needing a server. | ||
**/ | ||
var MockRequest = function(options) { | ||
@@ -17,13 +17,21 @@ if (!(this instanceof MockRequest)) { | ||
} | ||
EventEmitter.call(this, options); | ||
this.body = ''; | ||
this.httpVersion = '1.1'; | ||
this.url = options.url || '/'; | ||
this.query = url.parse(this.url, true).query; | ||
this.headers = this._headers = options.headers || {}; | ||
this.headers['transfer-coding'] = 'chunked'; | ||
this.headers = this._headers = {}; | ||
// setHeader now applies toLowerCase the names to mirror native node behaviour | ||
var self = this; | ||
_.each(options.headers, function(v, k) { | ||
self.setHeader(k, v); | ||
}); | ||
this.setHeader('transfer-encoding', 'chunked'); | ||
this.method = options.method || 'GET'; | ||
this.connection = {}; | ||
this.connection = this.socket = {}; | ||
// this.buffer = []; | ||
@@ -33,3 +41,3 @@ | ||
_.forEach(_.keys(options.cookies || {}) ,function(key) { | ||
_.forEach(_.keys(options.cookies || {}), function(key) { | ||
cookieBuffer.push(key + '=' + options.cookies[key]); | ||
@@ -49,9 +57,7 @@ }); | ||
return ret; | ||
} else if (clobber || !this.headers.hasOwnProperty(name)) { | ||
this.headers[name.toLowerCase()] = value; | ||
} else { | ||
this.headers[name.toLowerCase()] += ',' + value; | ||
} | ||
else if (clobber || !this.headers.hasOwnProperty(name)) { | ||
this.headers[name] = value; | ||
} | ||
else { | ||
this.headers[name] += ',' + value; | ||
} | ||
}; | ||
@@ -62,4 +68,3 @@ | ||
return http.ClientRequest.prototype.getHeader.call(this, name); | ||
} | ||
else { | ||
} else { | ||
return this.headers[name]; | ||
@@ -75,6 +80,8 @@ } | ||
this.body += str; | ||
}; | ||
}; | ||
MockRequest.prototype.end = function(str) { | ||
this.write(str); | ||
if (str) { | ||
this.write(str); | ||
} | ||
this.emit('data', new Buffer(this.body)); | ||
@@ -84,2 +91,2 @@ this.emit('end'); | ||
module.exports = MockRequest; | ||
module.exports = MockRequest; |
{ | ||
"name": "hammock", | ||
"version": "0.1.7", | ||
"version": "0.1.9", | ||
"description": "Node.js mock / polyfill http object library for http req / res", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -30,3 +30,3 @@ var test = require('tape'); | ||
req.on('data', function(body) { | ||
t.equals(body, 'foobar', 'Body should have been pushed from mock req.'); | ||
t.equals(body.toString(), 'foobar', 'Body should have been pushed from mock req.'); | ||
t.end(); | ||
@@ -37,2 +37,2 @@ }); | ||
req.end('bar'); | ||
}); | ||
}); |
8012
204