+1
-1
@@ -74,3 +74,3 @@ | ||
| .then( () => handle && handle(ctx)) | ||
| .catch(rejected); | ||
| .catch(e=>rejected(e,ctx)); | ||
| }; | ||
@@ -77,0 +77,0 @@ } |
+7
-4
| { | ||
| "name": "mof", | ||
| "version": "0.1.0", | ||
| "version": "0.1.1", | ||
| "description": "mof is short for Middleware of Floodesh", | ||
@@ -9,4 +9,4 @@ "main": "lib/app.js", | ||
| }, | ||
| "engines" : { | ||
| "node" : ">=4.0.0" | ||
| "engines": { | ||
| "node": ">=4.0.0" | ||
| }, | ||
@@ -25,4 +25,7 @@ "repository": { | ||
| "devDependencies": { | ||
| "co": "^4.6.0", | ||
| "mocha": "^2.3.4", | ||
| "should": "^8.0.1" | ||
| "should": "^8.0.1", | ||
| "should-sinon": "0.0.5", | ||
| "sinon": "^1.17.6" | ||
| }, | ||
@@ -29,0 +32,0 @@ "dependencies": { |
+5
-1
@@ -9,2 +9,6 @@ `mof` is short for "middleware of [floodesh](https://github.com/bda-research/floodesh)", a distributed web spider. | ||
| Mof requires __node v4.0.0__ or higher version. | ||
| Mof requires __node v4.0.0__ or higher version. | ||
| ## Middleware List | ||
+116
-39
@@ -5,7 +5,11 @@ | ||
| const should = require("should") | ||
| const Mof = require('../..') | ||
| const Mof = require('../../lib/app.js') | ||
| const sinon = require('sinon') | ||
| const co = require('co') | ||
| require('should-sinon') | ||
| describe('test app.use(fn), mainly compose related', () => { | ||
| let app; | ||
| beforeEach(()=> app = new Mof()) | ||
| it("simple test", done => { | ||
| const app =new Mof(); | ||
| const number = []; | ||
@@ -24,6 +28,12 @@ app.use((ctx,next) => { | ||
| number.push(3); | ||
| return next(); | ||
| }); | ||
| const handler = app.callback( () => { | ||
| app.use((ctx,next) => { | ||
| number.should.eql([1,2,3]); | ||
| return next(); | ||
| }); | ||
| const handler = app.callback( (ctx) => { // bottom of bowl | ||
| number.should.eql([1,2,3]); | ||
| done(); | ||
@@ -36,3 +46,2 @@ }); | ||
| it('should compose middleware', done => { | ||
| const app = new Mof(); | ||
| const calls = []; | ||
@@ -61,7 +70,12 @@ | ||
| app.use((ctx, next) => { | ||
| calls.push(8); | ||
| return next(); | ||
| }); | ||
| const handler = app.callback( () => { | ||
| calls.should.eql([1, 2, 3, 4, 5, 6]); | ||
| calls.should.eql([1, 2, 3, 8, 4, 5, 6]); | ||
| done(); | ||
| }); | ||
| handler(); | ||
@@ -71,3 +85,2 @@ }); | ||
| it('should access context in middleware', done => { | ||
| const app = new Mof(); | ||
| app.use((ctx, next) => { | ||
@@ -84,52 +97,116 @@ ctx.name.should.eql('mof'); | ||
| handler(ctx); | ||
| }); | ||
| }); | ||
| it.skip('should work with setTimeout', done => { | ||
| const app = new Mof(); | ||
| const calls = []; | ||
| it('should throw error for non function', () => { | ||
| [null, undefined, 0, false, 'not a function'].forEach(v => (() => app.use(v)).should.throw('middleware must be a function!')); | ||
| }); | ||
| const fn = function(opt){ | ||
| return function(ctx,next){ | ||
| calls.push(1); | ||
| setTimeout(function(){ | ||
| calls.push(3); | ||
| console.log(calls); | ||
| next().then(() => { calls.should.eql([1,2,3,4]);}); | ||
| },1); | ||
| } | ||
| } | ||
| it('should throw error for generator', () => { | ||
| (() => app.use(function *(){})).should.throw(/.+/); | ||
| }); | ||
| app.use(fn()); | ||
| it('should catch if exception is thrown out from middlewares', () => { | ||
| let count = 0; | ||
| app.use( (ctx,next) => { | ||
| calls.push(4); | ||
| ++count; | ||
| return next(); | ||
| }); | ||
| app.use( (ctx,next) => { | ||
| app.use( (ctx, next) => { | ||
| ++count; | ||
| return next(); | ||
| }); | ||
| app.use( (ctx, next) => { | ||
| throw new Error("unexpected error"); | ||
| }); | ||
| let fulfilled = sinon.spy(); | ||
| const handler = app.callback(fulfilled, (err,ctx) =>{ | ||
| should.exists(err); | ||
| err.message.should.eql("unexpected error"); | ||
| should.exists(ctx); | ||
| }); | ||
| handler({}); | ||
| fulfilled.should.not.be.called(); | ||
| }); | ||
| it('should handle async function', (done) => { | ||
| const calls = []; | ||
| app.use(co.wrap(function* (ctx, next) { | ||
| calls.push(1); | ||
| yield new Promise(function(resolve, reject){ | ||
| setTimeout(()=>{ | ||
| calls.push(2); | ||
| resolve(); | ||
| }, 10); | ||
| }); | ||
| yield next(); | ||
| calls.push(7); | ||
| })); | ||
| app.use(co.wrap(function* (ctx, next) { | ||
| calls.push(3); | ||
| let ret = yield function(done){ | ||
| setTimeout(() => { | ||
| calls.push(4); | ||
| done(null, 4); | ||
| }, 10); | ||
| } | ||
| ret.should.eql(4); | ||
| yield next(); | ||
| calls.push(6); | ||
| })); | ||
| app.use((ctx,next) => { | ||
| calls.push(5); | ||
| console.log(calls); | ||
| done(); | ||
| }) | ||
| return next(); | ||
| }); | ||
| const ctx = {name:'mof'}; | ||
| const handler = app.callback(function() { | ||
| calls.push(2); | ||
| console.log(calls); | ||
| calls.should.eql([1,2,3,4,5,6,7]); | ||
| done(); | ||
| }); | ||
| handler(ctx); | ||
| handler(); | ||
| }); | ||
| it('should throw error for non function', () => { | ||
| const app = new Mof(); | ||
| it('should have same result while app.callback called multiple times', (done)=> { | ||
| let p={},ctx1={calls:[],app:p}, ctx2={calls:[],app:p}; | ||
| [null, undefined, 0, false, 'not a function'].forEach(v => (() => app.use(v)).should.throw('middleware must be a function!')); | ||
| }); | ||
| app.use(co.wrap(function* (ctx, next) { | ||
| yield new Promise((resolve)=>{ | ||
| setTimeout(()=> { | ||
| ctx.calls.push(1); | ||
| resolve(); | ||
| },10); | ||
| }); | ||
| return next(); | ||
| })); | ||
| it('should throw error for generator', () => { | ||
| const app = new Mof(); | ||
| app.use( (ctx, next) => { | ||
| ctx.calls.push(2); | ||
| return next(); | ||
| }); | ||
| (() => app.use(function *(){})).should.throw(/.+/); | ||
| let handle = app.callback( (ctx) => { | ||
| ctx.calls.should.eql([1,2]); | ||
| }); | ||
| handle(ctx1); | ||
| handle = app.callback((ctx) => { | ||
| ctx.calls.should.eql([1,2]); | ||
| done(); | ||
| }); | ||
| handle(ctx2); | ||
| }); | ||
| }); |
Sorry, the diff of this file is not supported yet
11315
16.72%402
17.89%13
44.44%5
150%