diff --git a/lib/router.js b/lib/router.js index c338cfd..9a99dcc 100644 --- a/lib/router.js +++ b/lib/router.js @@ -21,7 +21,6 @@ module.exports = function (req, res, next) { , path = '/' ; - // parse url into references and collections parsed.forEach(function (part, i) { (i % 2 ? references : collections).push(part); @@ -30,8 +29,10 @@ module.exports = function (req, res, next) { path += collections[0]; // route to the first collection - resources.get({path: path}).first(function (err, resource) { - if(internals[path]) { + resources.get({path: path}).first(function (err, resource) { + + + if(internals[path]) { var rawHdr = req.headers['x-dssh-key'] , authErr = {status: 401} , strength @@ -59,6 +60,7 @@ module.exports = function (req, res, next) { require: internals[path], path: path }; + next(); } }) diff --git a/lib/server.js b/lib/server.js index a3a0eb5..a8716c5 100644 --- a/lib/server.js +++ b/lib/server.js @@ -28,8 +28,16 @@ middleware.listen = function (callback) { // remote flag server.use(function (req, res, next) { + res.header("Access-Control-Allow-Origin", '*'); + res.header('Access-Control-Allow-Credentials', 'true'); + res.header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS'); req.isRemote = true; - next(); + if(req.method === 'OPTIONS') { + res.header("Access-Control-Allow-Headers", req.header("Access-Control-Request-Headers")); + res.send('ok'); + } else { + next(); + } }); // proxy requests into the current mdoq stack diff --git a/lib/storage.js b/lib/storage.js index 8ba64d0..5821b51 100644 --- a/lib/storage.js +++ b/lib/storage.js @@ -8,9 +8,13 @@ module.exports = require('mdoq') .use(function (req, res, next) { var path = req.resource && req.resource.path; this.url = url + (path || this.url); + next(); }) .require('mdoq-mongodb') + .use(function (req, res, next) { + next(); + }) ; /** diff --git a/test/resources.test.js b/test/resources.test.js index 3950e4c..5f6714d 100644 --- a/test/resources.test.js +++ b/test/resources.test.js @@ -42,6 +42,31 @@ describe('Resources', function(){ }) }) + describe('PUT /resources/', function(){ + it('should updated the resources that match the query', function(done) { + resources.get(function (e, all) { + var res = all[0]; + + res.order = 777; + resources.get({_id: res._id}).put(res, function (err, upd) { + // FIXES NESTED MDOQ-HTTP BUG :( + resources.req = {}; + resources.get(function (error, chgd) { + var i; + + // REMOVE ONCE MDOQ-HTTP IS PATCHED! + while(i = chgd.shift()) { + if(i._id == res._id) break; + } + + expect(i.order).to.equal(777); + done(err); + }) + }) + }) + }) + }) + describe('DELETE /resources', function(){ it('should remove all resources or those that match the query', function(done) { resources.del(function (err) { diff --git a/test/support.js b/test/support.js index 3fc3a94..ec463ac 100644 --- a/test/support.js +++ b/test/support.js @@ -69,7 +69,7 @@ beforeEach(function(done){ server.listen(function () { clear(function () { resources.post(data.resources.todos, function (e) { - resources.post(data.resources.users, function (err) { + resources.post(data.resources.users, function (err, b, req, res) { done(err || e); }) }) diff --git a/test/validation.test.js b/test/validation.test.js index 496eaa3..568e314 100644 --- a/test/validation.test.js +++ b/test/validation.test.js @@ -30,4 +30,18 @@ describe('Validation', function(){ }) }) }) + + describe('PUT /todos/', function(){ + it('should update a single item', function(done) { + todos.post({title: 'a random todo'}, function (e, t) { + t.title = 'foobar'; + todos.use('/' + t._id).put(t, function (error, todo) { + todos.use('/' + todo._id).get(function (err) { + expect(t.title).to.equal('foobar'); + done(err); + }) + }) + }) + }) + }) }) \ No newline at end of file