Skip to content

Commit

Permalink
Add Wreck support for version 11 and above (#550)
Browse files Browse the repository at this point in the history
* Add Wreck support for version 11 and above (Closes #549)
  • Loading branch information
lostthetrail authored and arb committed May 17, 2017
1 parent 0a89dca commit 8cbda47
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 5 deletions.
25 changes: 23 additions & 2 deletions lib/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,31 @@ class Monitor {
};

if (this.settings.wreck) {
this._wreckHandler = (error, request, response, start, uri) => {
this._wreckHandler = function (error) {

const args = Array.from(arguments).slice(1);
let request;
let response;
let start;
let uri;

// Wreck 11+ emits response event with signature (err, {req, res, start, uri})
const isWreckVersion11OrHigher = (args.length === 1);
if (isWreckVersion11OrHigher) {
request = args[0].req;
response = args[0].res;
start = args[0].start;
uri = args[0].uri;
}
else {
request = args[0];
response = args[1];
start = args[2];
uri = args[3];
}

this.push(() => new Utils.WreckResponse(error, request, response, start, uri));
};
}.bind(this);
}

}
Expand Down
47 changes: 44 additions & 3 deletions test/monitor.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,17 @@ describe('Monitor', () => {

Wreck.get(server.info.uri + '/?q=test', (err, res) => {

expect(err).to.not.exist();
expect(res.statusCode).to.equal(500);
// Wreck v11+ report 4xx & 5xx as errors
const isWreckVersion11OrHigher = (err && err.isBoom);
if (isWreckVersion11OrHigher) {
expect(err).to.exist();
expect(err.output.payload.statusCode).to.equal(500);
}
else {
expect(err).to.not.exist();
expect(res.statusCode).to.equal(500);
}

setTimeout(() => {

const res1 = out1.data;
Expand Down Expand Up @@ -975,7 +984,7 @@ describe('Monitor', () => {
], done);
});

it('has a standard "wreck" event schema', { plan: 3 }, (done) => {
it('has a standard "wreck" event schema for version 10 and below', { plan: 3 }, (done) => {

const server = new Hapi.Server();
server.connection();
Expand Down Expand Up @@ -1029,5 +1038,37 @@ describe('Monitor', () => {
}
], done);
});

it('has a standard "wreck" event schema for version 11 and above', { plan: 2 }, (done) => {

const server = new Hapi.Server();
server.connection();

const out = new GoodReporter.Writer(true);
const monitor = internals.monitorFactory(server, {
reporters: { foo: [out] },
wreck: true
});

AsyncSeries([
server.start.bind(server),
monitor.start.bind(monitor),
(callback) => {

// Manually emit Wreck 11+ style event to prevent complexity of duplicate wreck versions in a test suite.
const wreck = Symbol.for('wreck');
process[wreck].emit('response', null, { req: {}, res: {}, start: 0, uri: {} });

expect(out.data.length).to.be.greaterThan(0);
const result = out.data.find((event) => {

return event.event === 'wreck';
});
expect(result).to.be.an.instanceof(Utils.WreckResponse);

server.stop(callback);
}
], done);
});
});
});

0 comments on commit 8cbda47

Please sign in to comment.