Skip to content

Commit

Permalink
Cleanup for #30
Browse files Browse the repository at this point in the history
  • Loading branch information
hueniverse committed May 30, 2015
1 parent 07255e2 commit ef34750
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 46 deletions.
1 change: 0 additions & 1 deletion index.js

This file was deleted.

30 changes: 0 additions & 30 deletions lib/buffer-equal.js

This file was deleted.

22 changes: 18 additions & 4 deletions lib/index.js
Expand Up @@ -5,7 +5,6 @@ var Stream = require('stream');
var Util = require('util');
var Url = require('url');
var Hoek = require('hoek');
var BufferEqual = require('./buffer-equal');


// Declare internals
Expand Down Expand Up @@ -54,7 +53,7 @@ internals.Request = function (options) {
var payload = options.payload || null;
if (payload &&
typeof payload !== 'string' &&
payload instanceof Buffer === false) {
!Buffer.isBuffer(payload)) {

payload = JSON.stringify(payload);
this.headers['content-type'] = this.headers['content-type'] || 'application/json';
Expand Down Expand Up @@ -265,18 +264,21 @@ internals.finish = function (response, req, onEnd) {
};
};


internals.splitBufferInTwo = function (buffer, seperator) {

for (var i = 0; i < buffer.length - seperator.length; i++) {
if (BufferEqual(buffer.slice(i, i + seperator.length), seperator)) {
for (var i = 0, max = buffer.length - seperator.length; i < max; ++i) {
if (internals.bufferEqual(buffer.slice(i, i + seperator.length), seperator)) {
var part1 = buffer.slice(0, i);
var part2 = buffer.slice(i + seperator.length);
return [part1, part2];
}
}

return [buffer, new Buffer(0)];
};


exports.inject = function (dispatchFunc, options, callback) {

options = (typeof options === 'string' ? { url: options } : options);
Expand All @@ -292,3 +294,15 @@ exports.isInjection = function (obj) {

return (obj instanceof internals.Request || obj instanceof internals.Response);
};


internals.bufferEqual = function (a, b) {

for (var i = 0, il = a.length; i < il; ++i) {
if (a[i] !== b[i]) {
return false;
}
}

return true;
};
16 changes: 5 additions & 11 deletions package.json
@@ -1,17 +1,17 @@
{
"name": "shot",
"description": "Injects a fake HTTP request/response into a node HTTP server",
"version": "1.5.0",
"version": "1.5.1",
"repository": "git://github.com/hapijs/shot",
"main": "index",
"main": "lib/index.js",
"keywords": [
"utilities",
"http",
"debug",
"test"
],
"engines": {
"node": ">=0.10.30"
"node": ">=0.10.32"
},
"dependencies": {
"hoek": "2.x.x"
Expand All @@ -21,14 +21,8 @@
"code": "1.x.x"
},
"scripts": {
"test": "lab -a code -L",
"test-cov": "lab -a code -t 100 -L",
"test": "lab -a code -t 100 -L",
"test-cov-html": "lab -a code -r html -o coverage.html -L"
},
"licenses": [
{
"type": "BSD",
"url": "http://github.com/hapijs/shot/raw/master/LICENSE"
}
]
"license": "BSD-3-Clause"
}
15 changes: 15 additions & 0 deletions test/index.js
Expand Up @@ -343,6 +343,21 @@ describe('inject()', function () {
});
});

it('echos buffer payload', function (done) {

var dispatch = function (req, res) {

res.writeHead(200, { 'content-type': req.headers['content-type'] });
req.pipe(res);
};

Shot.inject(dispatch, { method: 'post', url: '/test', payload: new Buffer('test!') }, function (res) {

expect(res.payload).to.equal('test!');
done();
});
});

it('echos object payload with non-english utf-8 string', function (done) {

var dispatch = function (req, res) {
Expand Down

0 comments on commit ef34750

Please sign in to comment.