diff --git a/osprey-method-handler.js b/osprey-method-handler.js index cf5a0df..c574203 100644 --- a/osprey-method-handler.js +++ b/osprey-method-handler.js @@ -52,6 +52,7 @@ standardHeaders.request.forEach(function (header) { */ var BODY_HANDLERS = [ ['application/json', jsonBodyHandler], + ['application/vnd.api+json', jsonBodyHandler], ['text/xml', xmlBodyHandler], ['application/x-www-form-urlencoded', urlencodedBodyHandler], ['multipart/form-data', formDataBodyHandler] diff --git a/package-lock.json b/package-lock.json index c4eaa71..484429f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "osprey-method-handler", - "version": "0.12.3", + "version": "0.12.4", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 8b40b86..c823e59 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "osprey-method-handler", - "version": "0.12.3", + "version": "0.12.4", "description": "Middleware for validating requests and responses based on a RAML method object", "main": "osprey-method-handler.js", "files": [ diff --git a/test/index.js b/test/index.js index 9c17230..09112c9 100644 --- a/test/index.js +++ b/test/index.js @@ -800,6 +800,60 @@ describe('osprey method handler', function () { }) }) + describe('json api', function () { + var JSON_SCHEMA = '{"properties":{"x":{"type":"string"}},"required":["x"]}' + it('should reject invalid request bodies', function () { + var app = router() + + app.post('/', handler({ + body: { + 'application/vnd.api+json': { + schema: JSON_SCHEMA + } + } + })) + + return makeFetcher(app).fetch('/', { + method: 'POST', + body: 'foobar', + headers: { + 'Content-Type': 'application/vnd.api+json' + } + }) + .then(function (res) { + expect(res.status).to.equal(400) + }) + }) + + it('should parse valid json', function () { + var app = router() + + app.post('/', handler({ + body: { + 'application/vnd.api+json': { + type: JSON_SCHEMA + } + } + }, '/', 'POST', { RAMLVersion: 'RAML10' }), function (req, res) { + expect(req.body).to.deep.equal([true, false]) + + res.end('success') + }) + + return makeFetcher(app).fetch('/', { + method: 'POST', + body: JSON.stringify([true, false]), + headers: { + 'Content-Type': 'application/vnd.api+json' + } + }) + .then(function (res) { + expect(res.body).to.equal('success') + expect(res.status).to.equal(200) + }) + }) + }) + describe('json', function () { var JSON_SCHEMA = '{"properties":{"x":{"type":"string"}},"required":["x"]}'