Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Tests and implementation for Issue #8 #44

Closed
wants to merge 1 commit into from

3 participants

@tj tj commented on the diff
@@ -140,22 +140,29 @@ Resource.prototype.map = function(method, path, fn){
, fn: fn
};
- // apply the route
- this.app[method](route, function(req, res, next){
- req.format = req.params.format || req.format || self.format;
- if (req.format) res.contentType(req.format);
- if ('object' == typeof fn) {
- if (req.format && fn[req.format]) {
- fn[req.format](req, res, next);
- } else if (fn.default) {
- fn.default(req, res, next);
+ // apply Array of middleware
+ if (fn instanceof Array) {
+ // TODO: Make arrays of middleware also work with formats
@tj Owner
tj added a note

what's this referring to?

@elliotf
elliotf added a note

It seems like he's saying that we will want to be able to pass middleware to format-style callbacks. Right now it just skips past middleware if it's a format-style callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 3, 2012
  1. Added tests and implementation for middleware using the existing Expr…

    Hunter Loftis authored
    …ess Array of Functions API.
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 15 deletions.
  1. +21 −14 index.js
  2. +12 −1 test/fixtures/cat.js
  3. +9 −0 test/resource.test.js
View
35 index.js
@@ -140,22 +140,29 @@ Resource.prototype.map = function(method, path, fn){
, fn: fn
};
- // apply the route
- this.app[method](route, function(req, res, next){
- req.format = req.params.format || req.format || self.format;
- if (req.format) res.contentType(req.format);
- if ('object' == typeof fn) {
- if (req.format && fn[req.format]) {
- fn[req.format](req, res, next);
- } else if (fn.default) {
- fn.default(req, res, next);
+ // apply Array of middleware
+ if (fn instanceof Array) {
+ // TODO: Make arrays of middleware also work with formats
@tj Owner
tj added a note

what's this referring to?

@elliotf
elliotf added a note

It seems like he's saying that we will want to be able to pass middleware to format-style callbacks. Right now it just skips past middleware if it's a format-style callback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ this.app[method](route, fn);
+ }
+ else {
+ // apply the route
+ this.app[method](route, function(req, res, next){
+ req.format = req.params.format || req.format || self.format;
+ if (req.format) res.contentType(req.format);
+ if ('object' == typeof fn) {
+ if (req.format && fn[req.format]) {
+ fn[req.format](req, res, next);
+ } else if (fn.default) {
+ fn.default(req, res, next);
+ } else {
+ res.send(406);
+ }
} else {
- res.send(406);
+ fn(req, res, next);
}
- } else {
- fn(req, res, next);
- }
- });
+ });
+ }
return this;
};
View
13 test/fixtures/cat.js
@@ -5,4 +5,15 @@ exports.index = function(req, res){
exports.new = function(req, res){
res.send('new cat');
-};
+};
+
+exports.filter = function(req, res, next) {
+ req.usertype = 'cat owner';
+ next();
+};
+
+exports.edit = [
+ exports.filter,
+ function(req, res, next) {
+ res.send('usertype: ' + req.usertype);
+ }];
View
9 test/resource.test.js
@@ -328,5 +328,14 @@ module.exports = {
assert.response(app,
{ url: '/api/cat/new' },
{ body: 'new cat' });
+ },
+
+ 'test middleware by array': function() {
+ var app = express.createServer();
+ var cat = app.resource('api/cat', require('./fixtures/cat'));
+
+ assert.response(app,
+ { url: '/api/cat/1/edit' },
+ { body: 'usertype: cat owner' });
}
};
Something went wrong with that request. Please try again.