Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Tests and implementation for custom mapped actions with strings

  • Loading branch information...
commit 5b9282e1343727b3f4863f1c8959d2b67fe575ae 1 parent 0f2abab
Hunter Loftis authored
Showing with 35 additions and 3 deletions.
  1. +2 −1  index.js
  2. +33 −2 test/resource.test.js
View
3  index.js
@@ -41,7 +41,7 @@ var Resource = module.exports = function Resource(name, actions, app) {
this.name = name;
this.app = app;
this.routes = {};
- actions = actions || {};
+ this.actions = actions = actions || {};
this.base = actions.base || '/';
if ('/' != this.base[this.base.length - 1]) this.base += '/';
this.format = actions.format;
@@ -122,6 +122,7 @@ Resource.prototype.map = function(method, path, fn){
if (method instanceof Resource) return this.add(method);
if ('function' == typeof path) fn = path, path = '';
if ('object' == typeof path) fn = path, path = '';
+ if ('string' === typeof fn) fn = this.actions[fn];
if ('/' == path[0]) path = path.substr(1);
else path = path ? this.param + '/' + path : this.param;
method = method.toLowerCase();
View
35 test/resource.test.js
@@ -169,7 +169,7 @@ module.exports = {
var forum = app.resource('forums', require('./fixtures/forum'));
var thread = app.resource('threads', require('./fixtures/thread'));
- var ret = user.add(forum);
+ var ret = user.map(forum);
ret.should.equal(user);
var ret = forum.add(thread);
@@ -301,7 +301,10 @@ module.exports = {
},
logout: function(req, res){
res.end('logout');
- }
+ }/*,
+ show: function(req, res) {
+ res.end('login and logout must be registered ahead of show to work');
+ }*/
};
var users = app.resource('users', actions, { load: load });
@@ -362,5 +365,33 @@ module.exports = {
{ url: '/forums/1/threads/50.json' },
{ body: '{"thread":"50","forum":"1","role":"thread owner"}'
, headers: { 'Content-Type': 'application/json' } });
+ },
+
+ 'test mapping custom actions with strings': function() {
+ var app = express.createServer();
+
+ var actions = {
+ login: function(req, res){
+ res.end('login');
+ },
+ logout: function(req, res){
+ res.end('logout');
+ }/*,
+ show: function(req, res) {
@tj
tj added a note

so these dont work? as long as express-resource is doing the app.get call for them before the others the route match precedence should be fine

@hunterloftis Owner

Yeah that's what I thought too but I tried the same thing above (line 305) with standard everything else and it broke. It was like 3am though so I just didn't want to start debugging something; I'll take a fresh look later.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ res.end('login and logout must be registered ahead of show to work');
+ }*/
+ };
+
+ app.resource('users', actions)
+ .map('all', '/login', 'login')
+ .map('all', '/logout', 'logout');
+
+ assert.response(app,
+ { url: '/users/login' },
+ { body: 'login' });
+
+ assert.response(app,
+ { url: '/users/logout' },
+ { body: 'logout' });
}
};
@tj

so these dont work? as long as express-resource is doing the app.get call for them before the others the route match precedence should be fine

@hunterloftis

Yeah that's what I thought too but I tried the same thing above (line 305) with standard everything else and it broke. It was like 3am though so I just didn't want to start debugging something; I'll take a fresh look later.

Please sign in to comment.
Something went wrong with that request. Please try again.