diff --git a/package.json b/package.json index 12abf87..a8b80b1 100644 --- a/package.json +++ b/package.json @@ -52,9 +52,12 @@ "babel-plugin-transform-object-assign": "^6.3.13", "babel-polyfill": "^6.5.0", "babel-preset-es2015": "^6.3.13", + "chai": "^3.5.0", "feathers": "^1.2.0", "jshint": "^2.8.0", "mocha": "^2.3.4", - "request": "^2.69.0" + "request": "^2.69.0", + "sinon": "^1.17.3", + "sinon-chai": "^2.8.0" } } diff --git a/test/error-handler.test.js b/test/error-handler.test.js index a211a42..b785f2e 100644 --- a/test/error-handler.test.js +++ b/test/error-handler.test.js @@ -1,24 +1,115 @@ +/*jshint expr: true, unused: false*/ + if(!global._babelPolyfill) { require('babel-polyfill'); } import feathers from 'feathers'; -import assert from 'assert'; +import chai, { expect } from 'chai'; +import sinon from 'sinon'; +import sinonChai from 'sinon-chai'; import request from 'request'; import fs from 'fs'; import { join } from 'path'; import { errors } from '../src'; import handler from '../src/error-handler'; +chai.use(sinonChai); + +const content = 'Error'; + +let htmlHandler = sinon.spy(function(error, req, res, next) { + res.send(content); +}); + +const jsonHandler = sinon.spy(function(error, req, res, next) { + res.json(error); +}); + describe('feathers-errors', () => { it('is CommonJS compatible', () => { - assert.equal(typeof require('../lib/error-handler'), 'function'); + expect(typeof require('../lib/error-handler')).to.equal('function'); }); it('can be required at the root', () => { - assert.equal(typeof require('../handler'), 'function'); + expect(typeof require('../handler')).to.equal('function'); }); it('is import compatible', () => { - assert.equal(typeof handler, 'function'); + expect(typeof handler).to.equal('function'); + }); + + describe('supports custom handlers', function() { + before(function() { + this.app = feathers() + .get('/error', function(req, res, next) { + next(new Error('Something went wrong')); + }) + .use(handler({ + html: htmlHandler, + json: jsonHandler + })); + + this.server = this.app.listen(5050); + }); + + after(function(done) { + this.server.close(done); + }); + + describe('HTML handler', () => { + const options = { + url: 'http://localhost:5050/error', + headers: { + 'Content-Type': 'text/html', + 'Accept': 'text/html' + } + }; + + it('is called', done => { + request(options, (error, res, body) => { + expect(htmlHandler).to.be.called; + done(); + }); + }); + + it('can send a custom response', done => { + request(options, (error, res, body) => { + expect(body).to.equal(content); + done(); + }); + }); + }); + + describe('JSON handler', () => { + const options = { + url: 'http://localhost:5050/error', + headers: { + 'Content-Type': 'application/json', + 'Accept': 'application/json' + } + }; + + it('is called', done => { + request(options, (error, res, body) => { + expect(jsonHandler).to.be.called; + done(); + }); + }); + + it('can send a custom response', done => { + const expected = JSON.stringify({ + name: 'GeneralError', + message: 'Something went wrong', + code: 500, + className: 'general-error', + data: {}, + errors: {} + }); + request(options, (error, res, body) => { + expect(body).to.deep.equal(expected); + done(); + }); + }); + }); }); describe('use as app error handler', function() { @@ -61,8 +152,8 @@ describe('feathers-errors', () => { url: 'http://localhost:5050/error', json: true }, (error, res, body) => { - assert.equal(res.statusCode, 500); - assert.deepEqual(body, { + expect(res.statusCode).to.equal(500); + expect(body).to.deep.equal({ name: 'GeneralError', message: 'Something went wrong', code: 500, @@ -75,7 +166,7 @@ describe('feathers-errors', () => { }); it.skip('still has a stack trace', () => { - assert.equal(handler, 'function'); + expect(handler).to.equal('function'); }); }); @@ -89,8 +180,8 @@ describe('feathers-errors', () => { 'Accept': 'text/html' } }, (error, res, body) => { - assert.equal(res.statusCode, 404); - assert.equal(html.toString(), body); + expect(res.statusCode).to.equal(404); + expect(html.toString()).to.equal(body); done(); }); }); @@ -105,8 +196,8 @@ describe('feathers-errors', () => { 'Accept': 'text/html' } }, (error, res, body) => { - assert.equal(res.statusCode, 500); - assert.equal(html.toString(), body); + expect(res.statusCode).to.equal(500); + expect(html.toString()).to.equal(body); done(); }); }); @@ -120,8 +211,8 @@ describe('feathers-errors', () => { 'Content-Type': 'text/html' } }, (error, res, body) => { - assert.equal(res.statusCode, 404); - assert.equal(html.toString(), body); + expect(res.statusCode).to.equal(404); + expect(html.toString()).to.equal(body); done(); }); }); @@ -135,8 +226,8 @@ describe('feathers-errors', () => { 'Accept': 'text/html' } }, (error, res, body) => { - assert.equal(res.statusCode, 404); - assert.equal(html.toString(), body); + expect(res.statusCode).to.equal(404); + expect(html.toString()).to.equal(body); done(); }); }); @@ -153,8 +244,8 @@ describe('feathers-errors', () => { }, json: true }, (error, res, body) => { - assert.equal(res.statusCode, 500); - assert.deepEqual(body, { + expect(res.statusCode).to.equal(500); + expect(body).to.deep.equal({ name: 'GeneralError', message: 'Something went wrong', code: 500, @@ -175,8 +266,8 @@ describe('feathers-errors', () => { }, json: true }, (error, res, body) => { - assert.equal(res.statusCode, 404); - assert.deepEqual(body, { name: 'NotFound', + expect(res.statusCode).to.equal(404); + expect(body).to.deep.equal({ name: 'NotFound', message: 'File not found', code: 404, className: 'not-found', @@ -195,8 +286,8 @@ describe('feathers-errors', () => { }, json: true }, (error, res, body) => { - assert.equal(res.statusCode, 400); - assert.deepEqual(body, { name: 'BadRequest', + expect(res.statusCode).to.equal(400); + expect(body).to.deep.equal({ name: 'BadRequest', message: 'Invalid Password', code: 400, className: 'bad-request', @@ -221,8 +312,8 @@ describe('feathers-errors', () => { }, json: true }, (error, res, body) => { - assert.equal(res.statusCode, 400); - assert.deepEqual(body, { name: 'BadRequest', + expect(res.statusCode).to.equal(400); + expect(body).to.deep.equal({ name: 'BadRequest', message: 'Invalid Password', code: 400, className: 'bad-request', @@ -247,8 +338,8 @@ describe('feathers-errors', () => { }, json: true }, (error, res, body) => { - assert.equal(res.statusCode, 400); - assert.deepEqual(body, { name: 'BadRequest', + expect(res.statusCode).to.equal(400); + expect(body).to.deep.equal({ name: 'BadRequest', message: 'Invalid Password', code: 400, className: 'bad-request', @@ -283,8 +374,8 @@ describe('feathers-errors', () => { }] }); - assert.equal(res.statusCode, 400); - assert.deepEqual(body, expected); + expect(res.statusCode).to.equal(400); + expect(body).to.deep.equal(expected); done(); }); });