Skip to content
This repository was archived by the owner on Jan 20, 2024. It is now read-only.

Commit 2a9d3bc

Browse files
committed
Add simple unit test for JSON API body validator
1 parent 5a32387 commit 2a9d3bc

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

test/index.js

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,64 @@ describe('osprey method handler', function () {
776776
})
777777
})
778778

779+
describe('json api', function () {
780+
var JSON_SCHEMA = '{"properties":{"x":{"type":"string"}},"required":["x"]}'
781+
it('should reject invalid request bodies', function () {
782+
var app = router()
783+
784+
app.post('/', handler({
785+
body: {
786+
'application/vnd.api+json': {
787+
schema: JSON_SCHEMA
788+
}
789+
}
790+
}))
791+
792+
return popsicle.default({
793+
url: '/',
794+
method: 'post',
795+
body: 'foobar',
796+
headers: {
797+
'Content-Type': 'application/vnd.api+json'
798+
}
799+
})
800+
.use(server(createServer(app)))
801+
.then(function (res) {
802+
expect(res.status).to.equal(400)
803+
})
804+
})
805+
806+
it('should parse valid json', function () {
807+
var app = router()
808+
809+
app.post('/', handler({
810+
body: {
811+
'application/vnd.api+json': {
812+
type: JSON_SCHEMA
813+
}
814+
}
815+
}, '/', 'POST', { RAMLVersion: 'RAML10' }), function (req, res) {
816+
expect(req.body).to.deep.equal([true, false])
817+
818+
res.end('success')
819+
})
820+
821+
return popsicle.default({
822+
url: '/',
823+
method: 'post',
824+
body: [true, false],
825+
headers: {
826+
'Content-Type': 'application/vnd.api+json'
827+
}
828+
})
829+
.use(server(createServer(app)))
830+
.then(function (res) {
831+
expect(res.body).to.equal('success')
832+
expect(res.status).to.equal(200)
833+
})
834+
})
835+
})
836+
779837
describe('json', function () {
780838
var JSON_SCHEMA = '{"properties":{"x":{"type":"string"}},"required":["x"]}'
781839

0 commit comments

Comments
 (0)