Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug where param handlers were run out of order #282

Merged
merged 5 commits into from
Sep 29, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 16 additions & 21 deletions lib/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,29 +170,24 @@ Layer.prototype.param = function (param, fn) {
};
middleware.param = param;

params.forEach(function (p, i) {
var prev = params[i - 1];

if (param === p.name) {
// insert param middleware in order params appear in path
if (prev) {
if (!stack.some(function (m, i) {
if (m.param === prev.name) {
return stack.splice(i, 0, middleware);
}
})) {
stack.some(function (m, i) {
if (!m.param) {
return stack.splice(i, 0, middleware);
}
});
}
} else {
stack.unshift(middleware);
}
}
var names = params.map(function (p) {
return p.name;
});

var x = names.indexOf(param);
if (x > -1) {
// iterate through the stack, to figure out where to place the handler fn
stack.some(function (fn, i) {
// param handlers are always first, so when we find an fn w/o a param property, stop here
// if the param handler at this part of the stack comes after the one we are adding, stop here
if (!fn.param || names.indexOf(fn.param) > x) {
// inject this param handler right before the current item
stack.splice(i, 0, middleware);
return true; // then break the loop
}
});
}

return this;
};

Expand Down
78 changes: 78 additions & 0 deletions test/lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,45 @@ describe('Router', function() {
});

it('runs parameter middleware in order of URL appearance', function(done) {
var app = koa();
var router = new Router();
router
.param('first', function *(id, next) {
this.ranFirst = true;
if (this.user) {
this.ranFirst = false;
}
if (!id) return this.status = 404;
yield next;
})
.param('user', function *(id, next) {
this.user = { name: 'alex' };
if (this.ranFirst) {
this.user.ordered = 'parameters';
}
if (!id) return this.status = 404;
yield next;
})
.get('/:first/users/:user', function *(next) {
this.body = this.user;
});

request(http.createServer(
app
.use(router.routes())
.callback()))
.get('/first/users/3')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
res.should.have.property('body');
res.body.should.have.property('name', 'alex');
res.body.should.have.property('ordered', 'parameters');
done();
});
});

it('runs parameter middleware in order of URL appearance even when added in reverse order', function(done) {
var app = koa();
var router = new Router();
router
Expand Down Expand Up @@ -1085,6 +1124,45 @@ describe('Router', function() {
});
});

it('runs parameter middleware in order of URL appearance even when added in random order', function(done) {
var app = koa();
var router = new Router();
router
// intentional random order
.param('a', function *(id, next) {
this.state.loaded = [ id ];
yield next;
})
.param('d', function *(id, next) {
this.state.loaded.push(id);
yield next;
})
.param('c', function *(id, next) {
this.state.loaded.push(id);
yield next;
})
.param('b', function *(id, next) {
this.state.loaded.push(id);
yield next;
})
.get('/:a/:b/:c/:d', function *(next) {
this.body = this.state.loaded;
});

request(http.createServer(
app
.use(router.routes())
.callback()))
.get('/1/2/3/4')
.expect(200)
.end(function(err, res) {
if (err) return done(err);
res.should.have.property('body');
res.body.should.eql([ '1', '2', '3', '4' ]);
done();
});
});

it('runs parent parameter middleware for subrouter', function (done) {
var app = koa();
var router = new Router();
Expand Down