Skip to content

Commit

Permalink
Allow controllers with the word "Controller" in the name (e.g. `Micro…
Browse files Browse the repository at this point in the history
…ControllerController`).

refs balderdashy#4042
  • Loading branch information
sgress454 committed Mar 22, 2017
1 parent 1dbc810 commit 032af73
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/router/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,8 @@ Router.prototype.getActionIdentityForTarget = function(target) {
throw flaverr('E_NOT_ACTION_TARGET', new Error('If target is a dictionary, it must contain an `action` property!'));
}

// Bail if the action starts with `somethingController` or contains characters other than letters,
// numbers, dashes and forward slashes.
if (actionIdentity.match(/^\w+Controller/) || !actionIdentity.match(/^\w+[a-zA-Z0-9\/-]*$/)) {
// Bail if the action contains characters other than letters, numbers, dashes and forward slashes.
if (!actionIdentity.match(/^\w+[a-zA-Z0-9\/-]*$/)) {
var didYouMean = '';
// If the action didn't contain weird characters, make a suggestion by removing "Controller" and
// replacing dots with slashes.
Expand Down Expand Up @@ -438,9 +437,8 @@ Router.prototype.getActionIdentityForTarget = function(target) {
// Normalize the action identity by removing `Controller` and replacing `.` with `/`
actionIdentity = target.replace(/Controller/,'').replace(/\./g,'/');

// If the result still has "Controller" in it, and contains anything other than letters, numbers,
// dashes, underscores or forward-slashes, bail.
if (actionIdentity.match(/Controller\//) || !actionIdentity.match(/^\w+[a-zA-Z0-9\/\-_]*$/)) {
// If the result contains anything other than letters, numbers, dashes, underscores or forward-slashes, bail.
if (!actionIdentity.match(/^\w+[a-zA-Z0-9\/\-_]*$/)) {
throw flaverr('E_NOT_ACTION_TARGET', new Error(
'\nCould not parse invalid action `' + target + '`.\n'+
'See http://sailsjs.com/docs/concepts/routes/custom-routes#?controller-action-target-syntax\n'+
Expand Down
71 changes: 71 additions & 0 deletions test/unit/controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,75 @@ describe('controllers :: ', function() {

});

describe('With a controller with `Controller` in the name', function() {

var curDir, tmpDir, sailsApp;
var warn;
var warnings = [];

before(function(done) {
// Cache the current working directory.
curDir = process.cwd();
// Create a temp directory.
tmpDir = tmp.dirSync({gracefulCleanup: true, unsafeCleanup: true});
// Switch to the temp directory.
process.chdir(tmpDir.name);
// Create a top-level legacy controller file with `Controller` in the name.
Filesystem.writeSync({
force: true,
destination: 'api/controllers/MicroControllerController.js',
string: 'module.exports = { \'check\': function(req, res) { return res.send(\'mate\'); } };'
}).execSync();

// Write a routes.js file
Filesystem.writeSync({
force: true,
destination: 'config/routes.js',
string: 'module.exports.routes = ' + JSON.stringify({
'GET /microcontroller/:id/check': 'MicroControllerController.check',
'GET /microcontroller/:id/check2': { controller: 'MicroControllerController', action: 'check' },
'GET /microcontroller/:id/check3': 'microcontroller/check'
})
}).execSync();

// Load the Sails app.
(new Sails()).load({hooks: {security: false, grunt: false, views: false, blueprints: false, policies: false, pubsub: false}, log: {level: 'error'}}, function(err, _sails) {
sailsApp = _sails;
return done(err);
});
});

after(function(done) {
sailsApp.lower(function() {
process.chdir(curDir);
return done();
});
});

it('should bind a route using \'MicroControllerController.check\'', function(done) {
sailsApp.request('GET /microcontroller/123/check', {}, function (err, resp, data) {
assert(!err, err);
assert.deepEqual(data, 'mate');
done();
});
});

it('should bind a route using { controller: \'MicroControllerController\', action: \'check\' }', function(done) {
sailsApp.request('GET /microcontroller/123/check2', {}, function (err, resp, data) {
assert(!err, err);
assert.deepEqual(data, 'mate');
done();
});
});

it('should bind a route using \'microcontroller/check\'', function(done) {
sailsApp.request('GET /microcontroller/123/check3', {}, function (err, resp, data) {
assert(!err, err);
assert.deepEqual(data, 'mate');
done();
});
});

});

});

0 comments on commit 032af73

Please sign in to comment.