From 79564f5b869ef22f4b39af2576a440a238186406 Mon Sep 17 00:00:00 2001 From: Evheniy Bystrov Date: Mon, 20 Feb 2017 15:47:00 +0000 Subject: [PATCH] tests --- README.md | 11 +-- index.js | 17 +++- package.json | 12 +-- tests/index.js | 164 +++++++++++++++++++++++++++++++++++++-- tests/public/favicon.ico | Bin 0 -> 1150 bytes 5 files changed, 187 insertions(+), 17 deletions(-) create mode 100644 tests/public/favicon.ico diff --git a/README.md b/README.md index 8d0fcd8..95ae749 100644 --- a/README.md +++ b/README.md @@ -32,23 +32,24 @@ app.js const App = require('yeps'); const app = new App(); const error = require('yeps-error'); - const logger = require('yeps-logger'); const Router = require('yeps-router'); const router = new Router(); const wrapper = require('yeps-express-wrapper'); // express middleware const bodyParser = require('body-parser'); + const favicon = require('serve-favicon'); + const path = require('path'); - + + app.then(wrapper(favicon(path.join(__dirname, 'public', 'favicon.ico')))); app.all([ error(), - logger(), wrapper(bodyParser.json()), ]); router.get('/').then(async ctx => { - // ctx.req.body + console.log(ctx.req.body); ctx.res.writeHead(200); ctx.res.end('test'); }); @@ -57,7 +58,7 @@ app.js http - .createServer(app.resolve()); + .createServer(app.resolve()) .listen(parseInt(process.env.PORT || '3000', 10)); And diff --git a/index.js b/index.js index 34c79c9..c91053a 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,19 @@ const debug = require('debug')('yeps:wrapper'); module.exports = fn => async context => { - debug(fn); - context; + debug('Wrapper created'); + return new Promise((resolve, reject) => { + context.res.on('finish', () => { + debug('Response finished'); + reject(); + }); + fn(context.req, context.res, error => { + debug(error); + if (error) { + reject(error); + } else { + resolve(); + } + }); + }); }; diff --git a/package.json b/package.json index 9ec384c..b86ca5d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "yeps-express-wrapper", - "version": "0.0.1", + "version": "0.0.2", "description": "YEPS express wrapper", "main": "index.js", "scripts": { @@ -43,10 +43,8 @@ "files": [ "index.js" ], - "dependencies": { - "yeps": "*" - }, "devDependencies": { + "body-parser": "^1.16.1", "chai": "^3.5.0", "chai-http": "^3.0.0", "coveralls": "^2.11.16", @@ -60,6 +58,10 @@ "npm-run-all": "^4.0.1", "nsp": "^2.6.2", "promise-pause-timeout": "0.0.1", - "rimraf": "^2.5.4" + "rimraf": "^2.5.4", + "serve-favicon": "^2.4.0", + "yeps": "*", + "yeps-error": "*", + "yeps-router": "*" } } diff --git a/tests/index.js b/tests/index.js index 145d5ea..6938266 100644 --- a/tests/index.js +++ b/tests/index.js @@ -1,7 +1,161 @@ +const App = require('yeps'); +const error = require('yeps-error'); +const http = require('http'); +const chai = require('chai'); +const chaiHttp = require('chai-http'); +const wrapper = require('..'); +const expect = chai.expect; +const bodyParser = require('body-parser'); +const favicon = require('serve-favicon'); +const path = require('path'); + +chai.use(chaiHttp); +let app; + describe('YEPS express wrapper test', () => { - it('should test next()'); - it('should test next(error)'); - it('should test without next = res.end()'); - it('bodyParser'); - it('favicon'); + + beforeEach(() => { + app = new App(); + }); + + it('should test next()', async () => { + let isTestFinished1 = false; + let isTestFinished2 = false; + let isTestFinished3 = false; + + app.then(wrapper((req, res, next) => { + isTestFinished1 = true; + next(); + })); + app.then(async ctx => { + isTestFinished2 = true; + ctx.res.writeHead(200); + ctx.res.end('test'); + }); + + await chai.request(http.createServer(app.resolve())) + .get('/') + .send() + .then(res => { + expect(res).to.have.status(200); + expect(res.text).to.be.equal('test'); + isTestFinished3 = true; + }); + + expect(isTestFinished1).is.true; + expect(isTestFinished2).is.true; + expect(isTestFinished3).is.true; + }); + + it('should test next(error)', async () => { + let isTestFinished1 = false; + let isTestFinished2 = false; + let isTestFinished3 = false; + + app.all([ + error(), + ]); + + app.then(wrapper((req, res, next) => { + isTestFinished1 = true; + next(new Error('error')); + })); + app.then(async ctx => { + isTestFinished2 = true; + ctx.res.writeHead(200); + ctx.res.end('test'); + }); + + await chai.request(http.createServer(app.resolve())) + .get('/') + .send() + .catch(err => { + expect(err).to.have.status(500); + expect(err.message).to.be.equal('Internal Server Error'); + isTestFinished3 = true; + }); + + expect(isTestFinished1).is.true; + expect(isTestFinished2).is.false; + expect(isTestFinished3).is.true; + }); + + it('should test without next = res.end()', async () => { + let isTestFinished1 = false; + let isTestFinished2 = false; + let isTestFinished3 = false; + + app.then(wrapper((req, res) => { + isTestFinished1 = true; + res.writeHead(200); + res.end('next'); + })); + app.then(async ctx => { + isTestFinished2 = true; + ctx.res.writeHead(200); + ctx.res.end('test'); + }); + + await chai.request(http.createServer(app.resolve())) + .get('/') + .send() + .then(res => { + expect(res).to.have.status(200); + expect(res.text).to.be.equal('next'); + isTestFinished3 = true; + }); + + expect(isTestFinished1).is.true; + expect(isTestFinished2).is.false; + expect(isTestFinished3).is.true; + }); + + it('body-parser', async () => { + let isTestFinished1 = false; + let isTestFinished2 = false; + + app.then(wrapper(bodyParser.json())); + app.then(async ctx => { + isTestFinished1 = true; + ctx.res.writeHead(200, {'Content-Type': 'application/json'}); + ctx.res.end(JSON.stringify(ctx.req.body)); + }); + + await chai.request(http.createServer(app.resolve())) + .get('/') + .set('Content-Type', 'application/json') + .send('{"user":"test"}') + .then(res => { + expect(res).to.have.status(200); + expect(res.text).to.be.equal('{"user":"test"}'); + expect(res.body).is.an('object'); + expect(res.body).to.have.property('user'); + expect(res.body.user).is.not.empty; + expect(res.body.user).to.be.equal('test'); + isTestFinished2 = true; + }); + + expect(isTestFinished1).is.true; + expect(isTestFinished2).is.true; + }); + + it('serve-favicon', async () => { + let isTestFinished = false; + + app.then(wrapper(favicon(path.join(__dirname, 'public', 'favicon.ico')))); + + await chai.request(http.createServer(app.resolve())) + .get('/favicon.ico') + .send() + .then(res => { + expect(res).to.have.status(200); + expect(res.headers['content-type']).to.be.equal('image/x-icon'); + expect(res.headers.etag).is.not.empty; + expect(res.headers['cache-control']).is.not.empty; + isTestFinished = true; + }); + + expect(isTestFinished).is.true; + }); + }); diff --git a/tests/public/favicon.ico b/tests/public/favicon.ico new file mode 100644 index 0000000000000000000000000000000000000000..9786efcc637cab2ffaa5415bce8d48c0a572cfda GIT binary patch literal 1150 zcma)6v1-FW43%l2V>5MaycPTfo(kEz7W@zWfv!$J!HZWfUcIBIHrJqa?Kfk3-GMA zFd91p`R>M?(+aK5`_&mmQD6_>(57js{WWn1t#b}8d_)*#!KP_)Ns zkceiU=LK7?aZ*{ySXf{e}3b`RD09W`T8G?>WKuU(Utig&YQYe@ClZ zk>jqb{@*h6fX;1T~rz^!o&Jvg|>2cpw6 F(FKoT@KXQ) literal 0 HcmV?d00001