Skip to content

Commit

Permalink
Merge pull request #124 from holidayextras/disable-unwanted-actions
Browse files Browse the repository at this point in the history
Prevent crash when requesting unavailable functionality
  • Loading branch information
theninj4 committed May 16, 2016
2 parents 67a311c + 21dc309 commit 9af79cb
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions example/handlers/photoHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
var jsonApi = require("../..");

module.exports = new jsonApi.MemoryHandler();
module.exports.delete = null;
1 change: 1 addition & 0 deletions lib/handlerEnforcer.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ handlerEnforcer._wrapHandler = function(handlers, operation, outCount) {
}

var original = handlers[operation];
if (!original) return null;
return function() {
var argsIn = Array.prototype.slice.call(arguments);
var requestParams = argsIn[0].params;
Expand Down
32 changes: 32 additions & 0 deletions test/unavailableFunctions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
"use strict";
var request = require("request");
var assert = require("assert");
var helpers = require("./helpers.js");
var jsonApiTestServer = require("../example/server.js");


describe("Testing jsonapi-server", function() {
describe("unavailable functions", function() {
it("responds with a clear error", function(done) {
var data = {
method: "delete",
url: "http://localhost:16006/rest/photos/14"
};
request(data, function(err, res, json) {
assert.equal(err, null);
json = helpers.validateError(json);
assert.equal(res.statusCode, "403", "Expecting 403");
assert.equal(json.errors[0].detail, "The requested resource 'photos' does not support 'delete'");

done();
});
});
});

before(function() {
jsonApiTestServer.start();
});
after(function() {
jsonApiTestServer.close();
});
});

0 comments on commit 9af79cb

Please sign in to comment.