🚀 Socket Launch Week 🚀 Day 1: Introducing .NET Support in Socket.Learn More

hock

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hock - npm Package Compare versions

Comparing version

to
1.0.0

@@ -12,4 +12,2 @@ var http = require('http'),

* @param {object} [options] options for your Hock server
* @param {Number} [options] an optional port for your Hock server
* @param {Number} [options.port] port number for your Hock server
* @param {boolean} [options.throwOnUnmatched] Tell Hock to throw if

@@ -21,13 +19,6 @@ * receiving a request without a match (Default=true)

var Hock = module.exports = function (options) {
if (typeof options === 'number') {
this.port = options;
options = {};
}
else if (typeof options === 'object' && options.port) {
this.port = options.port;
}
options = options || {};
this._throwOnUnmatched = (typeof options.throwOnUnmatched === 'boolean' ? options.throwOnUnmatched : true);
this._assertions = [];
this._started = false;
this.handler = Hock.prototype.handler.bind(this);
};

@@ -108,3 +99,3 @@

if (!err) {
return cb && cb()
return cb && cb();
}

@@ -121,47 +112,2 @@

/**
* Hock.close
*
* @description stop listening and shutdown the Hock server
*
* @param callback
*/
Hock.prototype.close = function (callback) {
this._server.close(callback);
};
/**
* Hock.address
*
* @description retrieve the address for the hock server
* @returns {*}
*/
Hock.prototype.address = function() {
return this._server.address();
};
/**
* Hock._initialize
*
* @description The internal helper function to start the Hock server
*
* @param {Function} callback This is fired when the server is listening
* @private
*/
Hock.prototype._initialize = function (callback) {
var self = this;
if (self._started) {
callback(new Error('Server is already listening'));
return;
}
self._server = http.createServer(this._handleRequest());
self._server.listen(self.port, function () {
self._started = true;
callback(null, self);
});
};
/**
* Hock.get

@@ -359,5 +305,5 @@ *

/**
* Hock._handleReqeust
* Hock.handler
*
* @description internal helper function for handling requests
* @description Handle incoming requests
*

@@ -367,42 +313,40 @@ * @returns {Function}

*/
Hock.prototype._handleRequest = function () {
Hock.prototype.handler = function (req, res) {
var self = this;
return function (req, res) {
var matchIndex = null;
var matchIndex = null;
req.body = '';
req.body = '';
req.on('data', function (data) {
req.body += data.toString();
});
req.on('data', function (data) {
req.body += data.toString();
});
req.on('end', function () {
req.on('end', function () {
for (var i = 0; i < self._assertions.length; i++) {
if (self._assertions[i].isMatch(req)) {
matchIndex = i;
break;
}
for (var i = 0; i < self._assertions.length; i++) {
if (self._assertions[i].isMatch(req)) {
matchIndex = i;
break;
}
}
if (matchIndex === null) {
if (self._throwOnUnmatched) {
throw new Error('No Match For: ' + req.method + ' ' + req.url);
}
if (matchIndex === null) {
if (self._throwOnUnmatched) {
throw new Error('No Match For: ' + req.method + ' ' + req.url);
}
console.error('No Match For: ' + req.method + ' ' + req.url);
if (req.method === 'PUT' || req.method === 'POST') {
console.error(req.body);
}
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('No Matching Response!\n');
console.error('No Match For: ' + req.method + ' ' + req.url);
if (req.method === 'PUT' || req.method === 'POST') {
console.error(req.body);
}
else {
if (self._assertions[matchIndex].sendResponse(res)) {
self._assertions.splice(matchIndex, 1)[0];
}
res.writeHead(500, { 'Content-Type': 'text/plain' });
res.end('No Matching Response!\n');
}
else {
if (self._assertions[matchIndex].sendResponse(res)) {
self._assertions.splice(matchIndex, 1)[0];
}
});
}
}
});
};

@@ -424,3 +368,3 @@

*/
var createHock = function(options, callback) {
var createHock = function(options) {
// options is optional

@@ -432,3 +376,2 @@ if (typeof options === 'function') {

var hock = new Hock(options);
hock._initialize(callback);
return hock;

@@ -435,0 +378,0 @@ };

{
"name": "hock",
"description": "A mocking server for HTTP requests",
"version": "0.2.5",
"version": "1.0.0",
"author": "Maciej Małecki <me@mmalecki.com>",

@@ -6,0 +6,0 @@ "contributors": [

@@ -13,13 +13,14 @@ # hock [![Build Status](https://secure.travis-ci.org/mmalecki/hock.png?branch=master)](http://travis-ci.org/mmalecki/hock)

var hock = require('hock'),
var http = require('http'),
hock = require('hock'),
request = require('request');
hock.createHock(function(err, hockServer) {
var port = hockServer.address().port;
var mock = hock.createHock();
mock
.get('/some/url')
.reply(200, 'Hello!');
hockServer
.get('/some/url')
.reply(200, 'Hello!');
request('http://localhost:' + port + '/some/url', function(err, res, body) {
var server = http.createServer(mock.handler);
server.listen(1337, function () {
request('http://localhost:' + 1337 + '/some/url', function(err, res, body) {
console.log(body);

@@ -31,12 +32,2 @@ });

A port can be optionally specified when creating the server:
```Javascript
hock.createHock(12345, function(err, hockServer) {
....
});
```
Unlike Nock, you create a `Hock` server with a callback based factory method. Behind the scenes, this spins up the new HTTP service, and begins listening to requests.
## HTTP Methods

@@ -43,0 +34,0 @@

@@ -1,2 +0,3 @@

var should = require('should'),
var http = require('http'),
should = require('should'),
request = require('request'),

@@ -9,11 +10,12 @@ hock = require('../');

var server;
var hockInstance;
var httpServer;
describe("With minimum requests", function () {
beforeEach(function (done) {
hock.createHock(function (err, hockServer) {
hockInstance = hock.createHock();
httpServer = http.createServer(hockInstance.handler).listen(PORT, function(err) {
should.not.exist(err);
should.exist(hockServer);
should.exist(hockInstance);
server = hockServer;
done();

@@ -24,3 +26,3 @@ });

it('should succeed with once', function (done) {
server
hockInstance
.get('/url')

@@ -30,3 +32,3 @@ .once()

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -36,3 +38,3 @@ should.exist(res);

JSON.parse(body).should.eql({ 'hock': 'ok' });
server.done();
hockInstance.done();
done();

@@ -43,3 +45,3 @@ });

it('should fail with min: 2 and a single request', function (done) {
server
hockInstance
.get('/url')

@@ -49,3 +51,3 @@ .min(2)

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -56,3 +58,3 @@ should.exist(res);

(function() {
server.done();
hockInstance.done();
}).should.throw();

@@ -64,3 +66,3 @@ done();

it('should succeed with min:2 and 2 requests', function (done) {
server
hockInstance
.get('/url')

@@ -70,3 +72,3 @@ .min(2)

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -77,3 +79,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -84,3 +86,3 @@ should.exist(res);

server.done();
hockInstance.done();
done();

@@ -92,3 +94,3 @@ });

it('should succeed with max:2 and 1 request', function (done) {
server
hockInstance
.get('/url')

@@ -98,3 +100,3 @@ .max(2)

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -105,3 +107,3 @@ should.exist(res);

server.done();
hockInstance.done();
done();

@@ -112,3 +114,3 @@ });

it('should succeed with max:2 and 2 requests', function (done) {
server
hockInstance
.get('/url')

@@ -118,3 +120,3 @@ .max(2)

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -125,3 +127,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -132,3 +134,3 @@ should.exist(res);

server.done();
hockInstance.done();
done();

@@ -140,3 +142,3 @@ });

it('should succeed with min:2, max:3 and 2 requests', function (done) {
server
hockInstance
.get('/url')

@@ -147,3 +149,3 @@ .min(2)

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -154,3 +156,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -161,3 +163,3 @@ should.exist(res);

server.done();
hockInstance.done();
done();

@@ -169,3 +171,3 @@ });

it('should succeed with min:2, max:Infinity and 2 requests', function (done) {
server
hockInstance
.get('/url')

@@ -176,3 +178,3 @@ .min(2)

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -183,3 +185,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -190,3 +192,3 @@ should.exist(res);

server.done();
hockInstance.done();
done();

@@ -198,3 +200,3 @@ });

it('should succeed with 2 different routes with different min, max values', function (done) {
server
hockInstance
.get('/url')

@@ -208,3 +210,3 @@ .min(2)

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -215,3 +217,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/asdf', function (err, res, body) {
request('http://localhost:' + PORT + '/asdf', function (err, res, body) {
should.not.exist(err);

@@ -222,3 +224,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -229,3 +231,3 @@ should.exist(res);

server.done();
hockInstance.done();
done();

@@ -239,7 +241,7 @@ });

it('should succeed with a single call', function (done) {
server
hockInstance
.get('/url')
.replyWithFile(200, process.cwd() + '/test/data/hello.txt');
request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -249,3 +251,3 @@ should.exist(res);

body.should.equal('this\nis\nmy\nsample\n');
server.done(function (err) {
hockInstance.done(function (err) {
should.not.exist(err);

@@ -258,3 +260,3 @@ done();

it('should succeed with a multiple calls', function (done) {
server
hockInstance
.get('/url')

@@ -264,3 +266,3 @@ .twice()

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -271,3 +273,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -277,3 +279,3 @@ should.exist(res);

body.should.equal('this\nis\nmy\nsample\n');
server.done(function (err) {
hockInstance.done(function (err) {
should.not.exist(err);

@@ -290,3 +292,3 @@ done();

it('should fail with no requests', function (done) {
server
hockInstance
.get('/url')

@@ -297,3 +299,3 @@ .many()

(function() {
server.done();
hockInstance.done();
}).should.throw();

@@ -304,3 +306,3 @@ done();

it('should succeed with many requests', function (done) {
server
hockInstance
.get('/url')

@@ -310,3 +312,3 @@ .many()

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -317,3 +319,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -324,3 +326,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -331,3 +333,3 @@ should.exist(res);

server.done();
hockInstance.done();
done();

@@ -343,3 +345,3 @@ });

it('should succeed with no requests', function (done) {
server
hockInstance
.get('/url')

@@ -353,3 +355,3 @@ .any()

it('should succeed with many requests', function (done) {
server
hockInstance
.get('/url')

@@ -359,3 +361,3 @@ .any()

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -366,3 +368,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -373,3 +375,3 @@ should.exist(res);

request('http://localhost:' + server.address().port + '/url', function (err, res, body) {
request('http://localhost:' + PORT + '/url', function (err, res, body) {
should.not.exist(err);

@@ -380,3 +382,3 @@ should.exist(res);

server.done();
hockInstance.done();
done();

@@ -390,5 +392,5 @@ });

afterEach(function (done) {
server.close(done);
httpServer.close(done);
});
});
});

@@ -1,2 +0,3 @@

var should = require('should'),
var http = require('http'),
should = require('should'),
request = require('request'),

@@ -9,11 +10,12 @@ hock = require('../');

var server;
var hockInstance;
var httpServer;
describe("with available ports", function() {
before(function(done) {
hock.createHock(function(err, hockServer) {
hockInstance = hock.createHock();
httpServer = http.createServer(hockInstance.handler).listen(PORT, function(err) {
should.not.exist(err);
should.exist(hockServer);
should.exist(hockInstance);
server = hockServer;
done();

@@ -24,7 +26,7 @@ });

it('should correctly respond to an HTTP GET request', function(done) {
server
hockInstance
.get('/url')
.reply(200, { 'hock': 'ok' });
request('http://localhost:' + server.address().port + '/url', function(err, res, body) {
request('http://localhost:' + PORT + '/url', function(err, res, body) {
should.not.exist(err);

@@ -40,3 +42,3 @@ should.exist(res);

it('should correctly respond to an HTTP POST request', function (done) {
server
hockInstance
.post('/post', { 'hock': 'post' })

@@ -46,3 +48,3 @@ .reply(201, { 'hock': 'created' });

request({
uri: 'http://localhost:' + server.address().port + '/post',
uri: 'http://localhost:' + PORT + '/post',
method: 'POST',

@@ -62,3 +64,3 @@ json: {

it('should correctly respond to an HTTP PUT request', function (done) {
server
hockInstance
.put('/put', { 'hock': 'put' })

@@ -68,3 +70,3 @@ .reply(204, { 'hock': 'updated' });

request({
uri: 'http://localhost:' +server.address().port+ '/put',
uri: 'http://localhost:' +PORT+ '/put',
method: 'PUT',

@@ -84,3 +86,3 @@ json: {

it('should correctly respond to an HTTP DELETE request', function (done) {
server
hockInstance
.delete('/delete')

@@ -90,3 +92,3 @@ .reply(202, { 'hock': 'deleted' });

request({
uri: 'http://localhost:' +server.address().port+ '/delete',
uri: 'http://localhost:' +PORT+ '/delete',
method: 'DELETE'

@@ -104,3 +106,3 @@ }, function (err, res, body) {

it('should correctly respond to an HTTP HEAD request', function (done) {
server
hockInstance
.head('/head')

@@ -110,3 +112,3 @@ .reply(200, '', { 'Content-Type': 'plain/text' });

request({
uri: 'http://localhost:' +server.address().port+ '/head',
uri: 'http://localhost:' +PORT+ '/head',
method: 'HEAD'

@@ -125,3 +127,3 @@ }, function (err, res, body) {

it('unmatched requests should throw', function () {
server
hockInstance
.head('/head')

@@ -131,3 +133,3 @@ .reply(200, '', { 'Content-Type': 'plain/text' });

(function() {
server.done();
hockInstance.done();
}).should.throw();

@@ -137,3 +139,3 @@ });

it('unmatched requests should call done callback with err', function (done) {
server
hockInstance
.head('/head')

@@ -146,33 +148,6 @@ .reply(200, '', { 'Content-Type': 'plain/text' })

});
});
describe("with hard coded ports", function() {
before(function(done) {
hock.createHock(PORT, function(err, hockServer) {
should.not.exist(err);
should.exist(hockServer);
server = hockServer;
done();
});
after(function (done) {
httpServer.close(done);
});
it('should correctly respond to an HTTP GET request', function(done) {
server
.get('/url')
.reply(200, { 'hock': 'ok' });
request('http://localhost:' + PORT + '/url', function(err, res, body) {
should.not.exist(err);
should.exist(res);
res.statusCode.should.equal(200);
JSON.parse(body).should.eql({ 'hock': 'ok' });
done();
});
});
after(function(done) {
server.close(done);
});
});

@@ -182,7 +157,7 @@

before(function(done) {
hock.createHock(PORT, function(err, hockServer) {
hockInstance = hock.createHock();
httpServer = http.createServer(hockInstance.handler).listen(PORT, function(err) {
should.not.exist(err);
should.exist(hockServer);
should.exist(hockInstance);
server = hockServer;
done();

@@ -193,3 +168,3 @@ });

it('should correctly use regex', function(done) {
server
hockInstance
.filteringPathRegEx(/password=[^&]*/g, 'password=XXX')

@@ -210,3 +185,3 @@ .get('/url?password=XXX')

it('should correctly use functions', function(done) {
server
hockInstance
.filteringPath(function (p) {

@@ -230,3 +205,3 @@ p.should.equal('/url?password=artischocko');

after(function(done) {
server.close(done);
httpServer.close(done);
});

@@ -237,7 +212,7 @@ });

before(function(done) {
hock.createHock(PORT, function(err, hockServer) {
hockInstance = hock.createHock();
httpServer = http.createServer(hockInstance.handler).listen(PORT, function (err) {
should.not.exist(err);
should.exist(hockServer);
should.exist(hockInstance);
server = hockServer;
done();

@@ -248,3 +223,3 @@ });

it('should allow testing for url', function(done) {
server
hockInstance
.get('/url?password=foo')

@@ -255,5 +230,5 @@ .reply(200, { 'hock': 'ok' })

server.hasRoute('GET', '/url?password=foo').should.equal(true);
server.hasRoute('GET', '/arti').should.equal(true);
server.hasRoute('GET', '/notexist').should.equal(false);
hockInstance.hasRoute('GET', '/url?password=foo').should.equal(true);
hockInstance.hasRoute('GET', '/arti').should.equal(true);
hockInstance.hasRoute('GET', '/notexist').should.equal(false);
done();

@@ -263,3 +238,3 @@ });

it('matches the header', function(done) {
server
hockInstance
.get('/url?password=foo')

@@ -270,6 +245,6 @@ .reply(200, { 'hock': 'ok' })

server
hockInstance
.hasRoute('GET', '/bla?password=foo', null, { 'content-type': 'plain/text' })
.should.equal(false);
server
hockInstance
.hasRoute('GET', '/artischocko', null, { 'foo-type': 'artischocke' })

@@ -282,3 +257,3 @@ .should.equal(true);

it('matches the body', function(done) {
server
hockInstance
.get('/url?password=foo')

@@ -289,4 +264,4 @@ .reply(200, { 'hock': 'ok' })

server.hasRoute('GET', '/bla?password=foo', 'testing').should.equal(false);
server.hasRoute('POST', '/artischocko', 'enteente').should.equal(true);
hockInstance.hasRoute('GET', '/bla?password=foo', 'testing').should.equal(false);
hockInstance.hasRoute('POST', '/artischocko', 'enteente').should.equal(true);

@@ -297,5 +272,5 @@ done();

after(function(done) {
server.close(done);
httpServer.close(done);
});
});
});