Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Evheniy Bystrov committed Feb 20, 2017
1 parent da0d7a4 commit 79564f5
Show file tree
Hide file tree
Showing 5 changed files with 187 additions and 17 deletions.
11 changes: 6 additions & 5 deletions README.md
Expand Up @@ -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');
});
Expand All @@ -57,7 +58,7 @@ app.js


http
.createServer(app.resolve());
.createServer(app.resolve())
.listen(parseInt(process.env.PORT || '3000', 10));

And
Expand Down
17 changes: 15 additions & 2 deletions 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();
}
});
});
};
12 changes: 7 additions & 5 deletions 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": {
Expand Down Expand Up @@ -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",
Expand All @@ -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": "*"
}
}
164 changes: 159 additions & 5 deletions 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;
});

});
Binary file added tests/public/favicon.ico
Binary file not shown.

0 comments on commit 79564f5

Please sign in to comment.