Skip to content

Commit

Permalink
Merge pull request #5 from klokoy/constructorWithServer
Browse files Browse the repository at this point in the history
enable constructor to take a pre build server to skip initialisation of server
  • Loading branch information
klokoy committed Aug 25, 2015
2 parents 186b915 + b0f76b7 commit cfbc143
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 64 deletions.
44 changes: 38 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ Using the end method you can write your own assertions in anyway you want.
var hapi-test = require('hapi-test'),
plugin = require('your-plugin'),
assert = require('chai').assert;
hapiTest(plugin)

hapiTest({plugins: [plugin]})
.get('/persons')
.end(function(result) {
assert(result.statusCode === 200);
Expand All @@ -23,17 +23,17 @@ hapiTest(plugin)
If you want to test status code you can simply assert the statusCode number

```javascript
hapiTest(plugin)
hapiTest({plugins: [plugin]})
.get('/persons')
.assert(200)
```

###headers
To test a header value you can do an assert with header name as first parameter and header value as second. Works with strings.

```javascript
//string
hapiTest(plugin)
hapiTest({plugins: [plugin]})
.get('/person')
.assert('connection', 'keep-alive');
```
Expand All @@ -44,11 +44,43 @@ If you are using mocha you can pass in the done function to any assertion as las
```javascript

it('should 200 on persons', function(done) {
hapiTest(plugin)
hapiTest({plugins: [plugin]})
.get('/person')
.assert(200, done)
});
```

###Keep instance of server to speed up tests
If you have multiple tests on the same server / plugins you can create an instance of the server and use this in the constructor. This will speed up the tests as it does not need to create a new server and initialize the plugins for each test.

```javascript
//example using mocha
var hapi-test = require('hapi-test'),
Hapi = require('Hapi'),
plugin = require('your-plugin'),
assert = require('chai').assert;

var server;

before(function (done) {

server = new Hapi.Server();
server.connection({
port: 8888
})

server.register({
name: 'plugin',
version: '0.0.1',
register: plugin.register
}, done);

});

it('can now be used', function (done) {
hapiTest({server: server})
.get('/person')
.assert(200, done);
});

```
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hapi-test",
"version": "3.0.0",
"version": "4.0.0",
"description": "Test hapi plugins with chaining method calls and assertions",
"main": "index.js",
"repository": {
Expand All @@ -18,7 +18,7 @@
"author": "Kim Lokøy",
"license": "MIT",
"dependencies": {
"hapi": "^8.4.0",
"hapi": "^8.*.*",
"lodash": "^3.6.0"
},
"devDependencies": {
Expand Down
90 changes: 50 additions & 40 deletions src/hapiTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ var Hapi = require('hapi'),
_ = require('lodash');


module.exports = function (plugins, options) {
return new HapiTest(plugins, options);
module.exports = function(options) {

return new HapiTest(options);
};

var HapiTest = function (plugins, options) {
var HapiTest = function(options) {
var self = this;

if (_.isArray(plugins)) {
self.plugins = plugins;
if (options.server) {
self.server = options.server;
} else {
self.plugins = [plugins];
self.plugins = options.plugins;
}
//options can be cleared
//requests can be cleared
self.requests = [];
//setup can be kept between calls
self.setup = {};
Expand All @@ -24,31 +25,40 @@ var HapiTest = function (plugins, options) {
return self;
};

HapiTest.prototype._init = function (callback) {
HapiTest.prototype._init = function(callback) {
var self = this;

self.server = new Hapi.Server();
self.server.connection({port:8888})
if (!self.server) {
//If I do not have a server create one
self.server = new Hapi.Server();
self.server.connection({
port: 8888
})

if (self.options && self.options.before) {
self.options.before(self.server);
}
if (self.options && self.options.before) {
self.options.before(self.server);
}

self.plugins.forEach(function (plugin, index) {
self.server.register({
name: 'plugin' + index,
version: '0.0.1',
register: plugin.register
}, function () {
if (index === self.plugins.length - 1) {
callback();
}
self.plugins.forEach(function(plugin, index) {
self.server.register({
name: 'plugin' + index,
version: '0.0.1',
register: plugin.register
}, function() {
if (index === self.plugins.length - 1) {
callback();
}
});
});
})
} else {
//If I have a server there is nothing to init
callback();
}


}

HapiTest.prototype.get = function (url, query) {
HapiTest.prototype.get = function(url, query) {

var request = {
options: {
Expand All @@ -66,7 +76,7 @@ HapiTest.prototype.get = function (url, query) {
return this;
};

HapiTest.prototype.delete = function (url) {
HapiTest.prototype.delete = function(url) {

var request = {
options: {
Expand All @@ -80,7 +90,7 @@ HapiTest.prototype.delete = function (url) {
return this;
};

HapiTest.prototype.post = function (url, payload) {
HapiTest.prototype.post = function(url, payload) {

var request = {
options: {
Expand All @@ -95,7 +105,7 @@ HapiTest.prototype.post = function (url, payload) {
return this;
};

HapiTest.prototype.put = function (url, payload) {
HapiTest.prototype.put = function(url, payload) {

var request = {
options: {
Expand All @@ -110,7 +120,7 @@ HapiTest.prototype.put = function (url, payload) {
return this;
};

HapiTest.prototype.patch = function (url, payload) {
HapiTest.prototype.patch = function(url, payload) {

var request = {
options: {
Expand All @@ -125,7 +135,7 @@ HapiTest.prototype.patch = function (url, payload) {
return this;
};

HapiTest.prototype.assert = function (a, b, c) {
HapiTest.prototype.assert = function(a, b, c) {
var self = this;

var request = _.last(this.requests);
Expand All @@ -134,33 +144,33 @@ HapiTest.prototype.assert = function (a, b, c) {
}

if (_.isNumber(a)) {
request.rejections.push(function (res) {
request.rejections.push(function(res) {
if (res.statusCode === a) {
return false;
} else {
return 'the status code is: ' + res.statusCode + ' but should be: ' + a;
}
});
if (_.isFunction(b)) {
self.end(function (res, errs) {
self.end(function(res, errs) {
b(errs);
});
}
} else if (_.isString(a)) {
request.rejections.push(function (res) {
request.rejections.push(function(res) {
return !res.headers[a].match(new RegExp(b));
});
if (_.isFunction(c)) {
self.end(function (res, errs) {
self.end(function(res, errs) {
c(errs);
});
}
} else if (_.isFunction(a)) {
request.rejections.push(function (res) {
request.rejections.push(function(res) {
return !a(res);
});
if (_.isFunction(b)) {
self.end(function (res, errs) {
self.end(function(res, errs) {
b(errs);
});
}
Expand All @@ -170,19 +180,19 @@ HapiTest.prototype.assert = function (a, b, c) {
};

//Support hapi-auth-cookie
HapiTest.prototype.auth = function (user) {
HapiTest.prototype.auth = function(user) {

this.credentials = user;
return this;

};


HapiTest.prototype.end = function (callback) {
HapiTest.prototype.end = function(callback) {

var self = this;

self._init(function () {
self._init(function() {

//run all request, return result in callback for the last request
function handleRequest(n) {
Expand All @@ -194,11 +204,11 @@ HapiTest.prototype.end = function (callback) {
}


self.server.inject(injectOptions, function (result) {
self.server.inject(injectOptions, function(result) {
//If rejections for this request has been registered run them and collect errs

if (request.rejections) {
request.rejections.forEach(function (rejection) {
request.rejections.forEach(function(rejection) {
var failed = rejection(result);

if (failed) {
Expand Down
Loading

0 comments on commit cfbc143

Please sign in to comment.