diff --git a/dustin/.eslintrc.js b/dustin/.eslintrc.js new file mode 100644 index 0000000..ed2ec67 --- /dev/null +++ b/dustin/.eslintrc.js @@ -0,0 +1,29 @@ +module.exports = { + "env": { + "es6": true, + "node": true, + "mocha": true + }, + "extends": "eslint:recommended", + "parserOptions": { + "sourceType": "module" + }, + "rules": { + "indent": [ + "error", + 2 + ], + "linebreak-style": [ + "error", + "unix" + ], + "quotes": [ + "error", + "single" + ], + "semi": [ + "error", + "always" + ] + } +}; diff --git a/dustin/.gitignore b/dustin/.gitignore new file mode 100644 index 0000000..07e6e47 --- /dev/null +++ b/dustin/.gitignore @@ -0,0 +1 @@ +/node_modules diff --git a/dustin/gulpfile.js b/dustin/gulpfile.js new file mode 100644 index 0000000..fc90c44 --- /dev/null +++ b/dustin/gulpfile.js @@ -0,0 +1,22 @@ +const gulp = require('gulp'); +const mocha = require('gulp-mocha'); +const lint = require('gulp-eslint'); + +gulp.task('linter' , () => { + return gulp.src(['./*.js', './test/*.js', './lib/*.js']) + .pipe(lint()) + .pipe(lint.format()); +}); + +gulp.task('tests', () => { + return gulp.src(['./*.js', './test/*.js', './lib/*.js'], {read: false}) + .pipe(mocha({reporter: 'nyan'})); +}); + +gulp.task('watch', () => { + gulp.watch(['./*.js', './test/*.js', './lib/*.js'], ['linter', 'tests']); +}); + +gulp.task('default', ['linter', 'tests', 'watch'], () => { + +}); diff --git a/dustin/lib/superJSONparser.js b/dustin/lib/superJSONparser.js new file mode 100644 index 0000000..3c16273 --- /dev/null +++ b/dustin/lib/superJSONparser.js @@ -0,0 +1,18 @@ +'use strict'; + +module.exports = (req, res, next) =>{ + var body = ''; + req.on('data', (data) => { + body += data.toString(); + }); + req.on('end', () => { + try { + req.body = JSON.parse(body); + } catch (e) { + e.message = 'Invalid JSON'; + e.statusCode = 422; + return next(e); + } + next(); + }); +}; diff --git a/dustin/package.json b/dustin/package.json new file mode 100644 index 0000000..263e99c --- /dev/null +++ b/dustin/package.json @@ -0,0 +1,26 @@ +{ + "name": "dustin", + "version": "1.0.0", + "description": "", + "main": "index.js", + "directories": { + "test": "test" + }, + "dependencies": {}, + "devDependencies": { + "chai": "^3.5.0", + "chai-http": "^2.0.1", + "gulp": "^3.9.1", + "gulp-eslint": "^2.0.0", + "gulp-mocha": "^2.2.0", + "mocha": "^2.4.5", + "mock-req": "^0.2.0" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node server.js" + }, + "keywords": [], + "author": "", + "license": "ISC" +} diff --git a/dustin/server.js b/dustin/server.js new file mode 100644 index 0000000..2da2459 --- /dev/null +++ b/dustin/server.js @@ -0,0 +1,25 @@ +'use strict'; +const express = require('express'); +const superJSONparser = require('./lib/superJSONparser'); +const app = express(); + +var apiRouter = new express.Router(); + +apiRouter.use(superJSONparser); + +apiRouter.post('/', (req, res, next) => { + res.send('{"Message": "POST received"}'); + res.end(); +}); + +app.use('/api', apiRouter); + +app.use((req, res) => { + res.status(404).json({ + msg: 'page not found' + }); +}); + +app.listen(3000, () => { + console.log('server up on 3000'); +}); diff --git a/dustin/test/integration_test.js b/dustin/test/integration_test.js new file mode 100644 index 0000000..7bc1fee --- /dev/null +++ b/dustin/test/integration_test.js @@ -0,0 +1,36 @@ +'use strict'; +const chai = require('chai'); +const expect = require('chai').expect; +const chaiHTTP = require('chai-http'); +chai.use(chaiHTTP); +const request = chai.request; +require('../server'); + +describe('INTEGRATION TEST: SuperJSONparser should', function () { + it('super return a super error for non JSON', function (done) { + let payload = { + message: 'test message' + }; + request('localhost:3000') + .post('/api') + .send(payload) + .end(function (err, res) { + //console.log(err); + expect(err).to.eql(null); + expect(res).to.have.status(200); + expect(res.text).to.eql('{"Message": "POST received"}'); + done(); + }); + }); + + it('super return a super error for non JSON', function (done) { + let payload = 'dsfa'; + request('localhost:3000') + .post('/api') + .send(payload) + .end(function (err, res) { + expect(err).to.have.status(422); + done(); + }); + }); +}); diff --git a/dustin/test/unit_test.js b/dustin/test/unit_test.js new file mode 100644 index 0000000..179141d --- /dev/null +++ b/dustin/test/unit_test.js @@ -0,0 +1,52 @@ +'use strict'; +const superJSONparser = require('../lib/superJSONparser'); +var MockReq = require('mock-req'); +const expect = require('chai').expect; +var res = {}; + +describe('UNIT TEST: SuperJSONparser should', function () { + it('super parse good JSON', function (done) { + + var req = new MockReq({ + method: 'POST', + url: '/' + }); + let payload = { + hello: 'test' + }; + req.write(payload); + req.end(); + + superJSONparser(req, res, function (e) { + if (e) { + done(e); + } else { + expect(req.body).to.eql(payload); + done(); + } + }); + }); + + it('super return a super error for non JSON', function (done) { + + var req = new MockReq({ + method: 'POST', + url: '/' + }); + + let payload = ('adfasd'); + req.write(payload); + req.end(); + + superJSONparser(req, res, function (e) { + if (e) { + expect(e.message).to.eql('Invalid JSON'); + expect(e.statusCode).to.equal(422); + done(); + } else { + //error + done(new Error('Got good JSON')); + } + }); + }); +});