From 5a32387d9d32eba520d47a8b53480bb523da10a3 Mon Sep 17 00:00:00 2001 From: Artem Kostiuk Date: Mon, 16 Jul 2018 12:10:51 +0300 Subject: [PATCH 1/4] Add support for JSON API content-type --- osprey-method-handler.js | 1 + 1 file changed, 1 insertion(+) diff --git a/osprey-method-handler.js b/osprey-method-handler.js index 7d876da..cce1b4f 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] From 2a9d3bc097e95606b7665ded0fa536feff595c2b Mon Sep 17 00:00:00 2001 From: Artem Kostiuk Date: Tue, 17 Jul 2018 10:52:24 +0300 Subject: [PATCH 2/4] Add simple unit test for JSON API body validator --- test/index.js | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/test/index.js b/test/index.js index 022c725..4844923 100644 --- a/test/index.js +++ b/test/index.js @@ -776,6 +776,64 @@ 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 popsicle.default({ + url: '/', + method: 'post', + body: 'foobar', + headers: { + 'Content-Type': 'application/vnd.api+json' + } + }) + .use(server(createServer(app))) + .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 popsicle.default({ + url: '/', + method: 'post', + body: [true, false], + headers: { + 'Content-Type': 'application/vnd.api+json' + } + }) + .use(server(createServer(app))) + .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"]}' From 272b5af9cdad701c994d18f0a977bd8ee15ec777 Mon Sep 17 00:00:00 2001 From: Jonathan Stoikovitch Date: Tue, 9 Jul 2019 16:36:58 -0700 Subject: [PATCH 3/4] 0.12.4 --- package-lock.json | 2 +- package.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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": [ From 2362684c5ad5d660134e4f1b06367ec4e4082c9e Mon Sep 17 00:00:00 2001 From: Artem Kostiuk Date: Wed, 10 Jul 2019 12:31:06 +0300 Subject: [PATCH 4/4] Rework tests for new popsicle --- test/index.js | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/test/index.js b/test/index.js index a885e1a..09112c9 100644 --- a/test/index.js +++ b/test/index.js @@ -813,15 +813,13 @@ describe('osprey method handler', function () { } })) - return popsicle.default({ - url: '/', - method: 'post', + return makeFetcher(app).fetch('/', { + method: 'POST', body: 'foobar', headers: { 'Content-Type': 'application/vnd.api+json' } }) - .use(server(createServer(app))) .then(function (res) { expect(res.status).to.equal(400) }) @@ -842,15 +840,13 @@ describe('osprey method handler', function () { res.end('success') }) - return popsicle.default({ - url: '/', - method: 'post', - body: [true, false], + return makeFetcher(app).fetch('/', { + method: 'POST', + body: JSON.stringify([true, false]), headers: { 'Content-Type': 'application/vnd.api+json' } }) - .use(server(createServer(app))) .then(function (res) { expect(res.body).to.equal('success') expect(res.status).to.equal(200)