diff --git a/example/handlers/photoHandler.js b/example/handlers/photoHandler.js index 83a7da02..2fe79261 100644 --- a/example/handlers/photoHandler.js +++ b/example/handlers/photoHandler.js @@ -2,3 +2,4 @@ var jsonApi = require("../.."); module.exports = new jsonApi.MemoryHandler(); +module.exports.delete = null; diff --git a/lib/handlerEnforcer.js b/lib/handlerEnforcer.js index d1003349..dfebd2dd 100644 --- a/lib/handlerEnforcer.js +++ b/lib/handlerEnforcer.js @@ -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; diff --git a/lib/router.js b/lib/router.js index 48719b4b..6c026b12 100644 --- a/lib/router.js +++ b/lib/router.js @@ -13,6 +13,7 @@ var bodyParser = require("body-parser"); var cookieParser = require("cookie-parser"); var jsonApi = require("./jsonApi.js"); var debug = require("./debugging.js"); +var responseHelper = require("./responseHelper.js"); var url = require("url"); app.use(function(req, res, next) { @@ -113,7 +114,14 @@ router.authenticate = function(request, res, callback) { router._authFunction(request, function(err) { if (!err) return callback(); - res.status(401).end(); + var errorWrapper = { + status: "401", + code: "UNAUTHORIZED", + title: "Authentication Failed", + detail: err || "You are not authorised to access this resource." + }; + var payload = responseHelper.generateError(request, errorWrapper); + res.status(401).json(payload); }); }; diff --git a/test/authentication.js b/test/authentication.js index a95f403d..ca405a52 100644 --- a/test/authentication.js +++ b/test/authentication.js @@ -2,7 +2,7 @@ var request = require("request"); var assert = require("assert"); var jsonApiTestServer = require("../example/server.js"); - +var helpers = require("./helpers.js"); describe("Testing jsonapi-server", function() { describe("authentication", function() { @@ -14,10 +14,10 @@ describe("Testing jsonapi-server", function() { "blockMe": "please" } }; - request(data, function(err, res) { + request(data, function(err, res, json) { assert.equal(err, null); assert.equal(res.statusCode, "401", "Expecting 401"); - + helpers.validateError(json); done(); }); }); @@ -30,10 +30,10 @@ describe("Testing jsonapi-server", function() { "cookie": "blockMe=please" } }; - request(data, function(err, res) { + request(data, function(err, res, json) { assert.equal(err, null); assert.equal(res.statusCode, "401", "Expecting 401"); - + helpers.validateError(json); done(); }); }); diff --git a/test/unavailableFunctions.js b/test/unavailableFunctions.js new file mode 100644 index 00000000..821e81b3 --- /dev/null +++ b/test/unavailableFunctions.js @@ -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(); + }); +});