Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions osprey-method-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
54 changes: 54 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"]}'

Expand Down