Skip to content

Commit

Permalink
Clean up unit tests. Closes hapijs#403.
Browse files Browse the repository at this point in the history
- Moves tests to the correct files.
- Simplified some of the more complex tests.
  • Loading branch information
arb committed Nov 24, 2015
1 parent 3b34534 commit d9502c4
Show file tree
Hide file tree
Showing 7 changed files with 554 additions and 658 deletions.
1 change: 1 addition & 0 deletions package.json
Expand Up @@ -29,6 +29,7 @@
"devDependencies": {
"code": "2.x.x",
"hapi": "10.x.x",
"insync": "2.1.x",
"lab": "7.x.x"
},
"scripts": {
Expand Down
19 changes: 19 additions & 0 deletions test/fixtures/server.cert
@@ -0,0 +1,19 @@
-----BEGIN CERTIFICATE-----
MIIDBjCCAe4CCQDvLNml6smHlTANBgkqhkiG9w0BAQUFADBFMQswCQYDVQQGEwJV
UzETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lkZ2l0
cyBQdHkgTHRkMB4XDTE0MDEyNTIxMjIxOFoXDTE1MDEyNTIxMjIxOFowRTELMAkG
A1UEBhMCVVMxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoMGEludGVybmV0
IFdpZGdpdHMgUHR5IEx0ZDCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB
ANFKslwwqlgyqaDUECv33a9DpBuIFug1Gn8Xbnx/RppF/86Cs4P0hS6z4qc0hiDS
lrcjL6O5j416qlYBNdCwyN1RVfCEen5wEU/gBfAluRzATxrf7H0FuFuKbrwR5AcV
kltRL23nIDRCEvYUxrx15Bc5uMSdnvQx6dsaFQI0RAu9weyWxYXOWRhnISsPghZg
IjcrFNA5gYEHGnNHoNqVqE/mBpk3kI+rEVzuu59scv4QNQ7jegOFgSt8DNzuAZ0x
tHTW1lBG3u8gG1eYBMquexoSSHmMUb73lQ2l/dC6AKjOHFB9Ouq3IjjdFGwx1diz
/yWh+Y8wY1Mgpyiy6ObJ5W8CAwEAATANBgkqhkiG9w0BAQUFAAOCAQEAoSc6Skb4
g1e0ZqPKXBV2qbx7hlqIyYpubCl1rDiEdVzqYYZEwmst36fJRRrVaFuAM/1DYAmT
WMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf
axrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj
DI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs
xsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5
3J5DTjAU55deBQ==
-----END CERTIFICATE-----
27 changes: 27 additions & 0 deletions test/fixtures/server.key
@@ -0,0 +1,27 @@
-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz
g/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s
fQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF
hc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6
A4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06
6rciON0UbDHV2LP/JaH5jzBjUyCnKLLo5snlbwIDAQABAoIBAQDJm7YC3pJJUcxb
c8x8PlHbUkJUjxzZ5MW4Zb71yLkfRYzsxrTcyQA+g+QzA4KtPY8XrZpnkgm51M8e
+B16AcIMiBxMC6HgCF503i16LyyJiKrrDYfGy2rTK6AOJQHO3TXWJ3eT3BAGpxuS
12K2Cq6EvQLCy79iJm7Ks+5G6EggMZPfCVdEhffRm2Epl4T7LpIAqWiUDcDfS05n
NNfAGxxvALPn+D+kzcSF6hpmCVrFVTf9ouhvnr+0DpIIVPwSK/REAF3Ux5SQvFuL
jPmh3bGwfRtcC5d21QNrHdoBVSN2UBLmbHUpBUcOBI8FyivAWJhRfKnhTvXMFG8L
waXB51IZAoGBAP/E3uz6zCyN7l2j09wmbyNOi1AKvr1WSmuBJveITouwblnRSdvc
sYm4YYE0Vb94AG4n7JIfZLKtTN0xvnCo8tYjrdwMJyGfEfMGCQQ9MpOBXAkVVZvP
e2k4zHNNsfvSc38UNSt7K0HkVuH5BkRBQeskcsyMeu0qK4wQwdtiCoBDAoGBANF7
FMppYxSW4ir7Jvkh0P8bP/Z7AtaSmkX7iMmUYT+gMFB5EKqFTQjNQgSJxS/uHVDE
SC5co8WGHnRk7YH2Pp+Ty1fHfXNWyoOOzNEWvg6CFeMHW2o+/qZd4Z5Fep6qCLaa
FvzWWC2S5YslEaaP8DQ74aAX4o+/TECrxi0z2lllAoGAdRB6qCSyRsI/k4Rkd6Lv
w00z3lLMsoRIU6QtXaZ5rN335Awyrfr5F3vYxPZbOOOH7uM/GDJeOJmxUJxv+cia
PQDflpPJZU4VPRJKFjKcb38JzO6C3Gm+po5kpXGuQQA19LgfDeO2DNaiHZOJFrx3
m1R3Zr/1k491lwokcHETNVkCgYBPLjrZl6Q/8BhlLrG4kbOx+dbfj/euq5NsyHsX
1uI7bo1Una5TBjfsD8nYdUr3pwWltcui2pl83Ak+7bdo3G8nWnIOJ/WfVzsNJzj7
/6CvUzR6sBk5u739nJbfgFutBZBtlSkDQPHrqA7j3Ysibl3ZIJlULjMRKrnj6Ans
pCDwkQKBgQCM7gu3p7veYwCZaxqDMz5/GGFUB1My7sK0hcT7/oH61yw3O8pOekee
uctI1R3NOudn1cs5TAy/aypgLDYTUGQTiBRILeMiZnOrvQQB9cEf7TFgDoRNCcDs
V/ZWiegVB/WY7H0BkCekuq5bHwjgtJTpvHGqQ9YD7RhE8RSYOhdQ/Q==
-----END RSA PRIVATE KEY-----
39 changes: 21 additions & 18 deletions test/helper.js
Expand Up @@ -2,29 +2,32 @@

const internals = {};

module.exports = internals.Reporter = function (events, config, datahandler) {
class Reporter {
constructor(events, config, datahandler) {

this.events = events;
this.messages = [];
this.handler = datahandler || function () {};
};
this.events = events;
this.messages = [];
this.handler = datahandler || function () {};
}
init(stream, emitter, callback) {

internals.Reporter.prototype.init = function (stream, emitter, callback) {
const self = this;

const self = this;
stream.on('data', (data) => {

stream.on('data', (data) => {
if (self.events[data.event]) {
self.messages.push(data);
self.handler(data);
}
});

if (self.events[data.event]) {
self.messages.push(data);
self.handler(data);
}
});
emitter.once('stop', () => {

emitter.once('stop', () => {
self.stopped = true;
});

self.stopped = true;
});
callback();
}
}

callback();
};
module.exports = Reporter;
123 changes: 61 additions & 62 deletions test/index.js
Expand Up @@ -4,21 +4,10 @@

const Code = require('code');
const Hapi = require('hapi');
const Https = require('https');
const Lab = require('lab');
const Wreck = require('wreck');

// Done for testing because Wreck is a singleton and every test run ads one event to it
Wreck.setMaxListeners(0);

const GoodReporter = require('./helper');


// Declare internals

const internals = {
agent: new Https.Agent({ maxSockets: 6 })
};
const Monitor = require('../lib/monitor');


// Test shortcuts
Expand All @@ -29,76 +18,86 @@ const describe = lab.describe;
const it = lab.it;


describe('Plugin', () => {
describe('good', () => {

it('reports on outbound wreck requests', (done) => {
it('exposes the Monitor object', (done) => {

const server = new Hapi.Server();
const tlsOptions = {
labels: ['https'],
port: 0,
key: '-----BEGIN RSA PRIVATE KEY-----\nMIIEpAIBAAKCAQEA0UqyXDCqWDKpoNQQK/fdr0OkG4gW6DUafxdufH9GmkX/zoKz\ng/SFLrPipzSGINKWtyMvo7mPjXqqVgE10LDI3VFV8IR6fnART+AF8CW5HMBPGt/s\nfQW4W4puvBHkBxWSW1EvbecgNEIS9hTGvHXkFzm4xJ2e9DHp2xoVAjREC73B7JbF\nhc5ZGGchKw+CFmAiNysU0DmBgQcac0eg2pWoT+YGmTeQj6sRXO67n2xy/hA1DuN6\nA4WBK3wM3O4BnTG0dNbWUEbe7yAbV5gEyq57GhJIeYxRvveVDaX90LoAqM4cUH06\n6rciON0UbDHV2LP/JaH5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\nWMhU+yTfA+vCS4tql9b9zUhPw/IDHpBDWyR01spoZFBF/hE1MGNpCSXXsAbmCiVf\naxrIgR2DNketbDxkQx671KwF1+1JOMo9ffXp+OhuRo5NaGIxhTsZ+f/MA4y084Aj\nDI39av50sTRTWWShlN+J7PtdQVA5SZD97oYbeUeL7gI18kAJww9eUdmT0nEjcwKs\nxsQT1fyKbo7AlZBY4KSlUMuGnn0VnAsB9b+LxtXlDfnjyM8bVQx1uAfRo0DO8p/5\n3J5DTjAU55deBQ==\n-----END CERTIFICATE-----\n'
const plugin = {
register: require('../lib/index').register,
options: {
reporters: [{
reporter: GoodReporter,
events: { response: '*' }
}]
}
};
const server = new Hapi.Server();
server.register(plugin, (err) => {

server.connection({ tls: tlsOptions });
server.connection({ port: 0, labels: ['http'] });
expect(err).to.not.exist();
expect(server.plugins.good.monitor).to.be.an.object();
done();
});
});

server.route({
method: 'GET',
path: '/',
handler: function (request, reply) {
it('starts the Monitor object during registration', (done) => {

reply('ok');
const plugin = {
register: require('../lib/index').register,
options: {
reporters: [{
reporter: GoodReporter,
events: { response: '*' }
}]
}
});
};
const server = new Hapi.Server();
let called = false;
const start = Monitor.prototype.start;
Monitor.prototype.start = (callback) => {

server.select('http').route({
method: 'GET',
path: '/http',
handler: function (request, reply) {
Monitor.prototype.start = start;
called = true;
callback();
};

reply('ok');
}
server.register(plugin, (err) => {

expect(err).to.not.exist();
expect(called).to.be.true();
done();
});
});

let emitted;
const options = {};
options.reporters = [new GoodReporter({
wreck: '*'
}, null, (event) => {

if (!emitted) {
expect(event.event).to.equal('wreck');
expect(event.request.protocol).to.equal('https:');
expect(event.request.host).to.exist();
expect(event.request.path).to.equal('/');
emitted = true;
}
else {
expect(event.event).to.equal('wreck');
expect(event.request.protocol).to.equal('http:');
expect(event.request.host).to.exist();
expect(event.request.path).to.equal('/http');
done();
}
})];
it('stops the monitor when the server stops', (done) => {

const plugin = {
register: require('..'),
options: options
register: require('../lib/index').register,
options: {
reporters: [{
reporter: GoodReporter,
events: { response: '*' }
}]
}
};
const server = new Hapi.Server();
let called = false;
const stop = Monitor.prototype.stop;
Monitor.prototype.stop = () => {

Monitor.prototype.stop = stop;
called = true;
};


server.register(plugin, (err) => {

expect(err).to.not.exist();

server.start(() => {
server.stop(() => {

Wreck.get('https://127.0.0.1:' + server.connections[0].info.port + '/', { rejectUnauthorized: false }, () => {

Wreck.get('http://127.0.0.1:' + server.connections[1].info.port + '/http', () => {});
});
expect(called).to.be.true();
done();
});
});
});
Expand Down

0 comments on commit d9502c4

Please sign in to comment.