Skip to content

Commit

Permalink
Merge branch 'master' into v1.6.0
Browse files Browse the repository at this point in the history
  • Loading branch information
theninj4 committed May 16, 2016
2 parents 2559ba4 + 9af79cb commit de1874f
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 6 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
10 changes: 9 additions & 1 deletion lib/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
});
};

Expand Down
10 changes: 5 additions & 5 deletions test/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
});
});
Expand All @@ -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();
});
});
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 de1874f

Please sign in to comment.