floodesh-lib
Version:
core library for floodesh, which contains core request, response and context class, also used for flowesh
84 lines (70 loc) • 1.64 kB
JavaScript
const should = require('should')
const path = require('path')
const sinon = require('sinon')
require('should-sinon')
const co = require('co')
const App = require('../lib/core.js')
describe('floodesh-lib',()=>{
let app;
beforeEach(()=>{
app = new App();
});
it('should end up normally', done=>{
let calls = [];
app.use((ctx,next)=>{
calls.push(1);
return next();
});
app.use((ctx,next)=>{
calls.push(2);
return next();
});
app.use((ctx,next)=>{
calls.push(3);
return next();
});
app.enqueue({});
app.on('complete',()=>{
calls.should.eql([1,2,3]);
done();
});
});
it('should fire up an error when throw an exception',done=>{
app.use(()=>{
throw new Error("wtf");
});
app.enqueue({uri:"http://www.baidu.com"});
app.on('error', (e,ctx)=>{
should.exists(e);
e.message.should.eql('wtf');
// need check ctx.
//should.exists(ctx);
done();
});
});
it('should have ctx info',done=>{
app.use(co.wrap(require('mof-request')()));
app.use((ctx, next) => {
ctx.path.should.eql('/');
ctx.origin.should.eql("https://www.baidu.com");
ctx.protocol.should.eql("https:");
return next();
})
app.enqueue({uri:"https://www.baidu.com"});
app.on('complete',()=>done());
});
it('should fire up an error when rejected in promise', done=>{
app.use(co.wrap(function* (ctx,next){
yield new Promise((resolve, reject)=>{
reject(new Error("err"));
});
return next();
}));
app.enqueue({});
app.on('error', e=>{
should.exist(e);
done();
});
});
} )