fastify
Version:
Fast and low overhead web framework, for Node.js
67 lines (54 loc) • 1.6 kB
JavaScript
const t = require('tap')
const test = t.test
const sget = require('simple-get').concat
const Fastify = require('../fastify')
process.removeAllListeners('warning')
test('contentTypeParser should add a custom async parser', t => {
t.plan(3)
const fastify = Fastify()
fastify.post('/', (req, reply) => {
reply.send(req.body)
})
fastify.options('/', (req, reply) => {
reply.send(req.body)
})
fastify.addContentTypeParser('application/jsoff', async function (req, payload) {
const res = await new Promise((resolve, reject) => resolve(payload))
return res
})
fastify.listen({ port: 0 }, err => {
t.error(err)
t.teardown(() => fastify.close())
t.test('in POST', t => {
t.plan(3)
sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port,
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/jsoff'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
})
})
t.test('in OPTIONS', t => {
t.plan(3)
sget({
method: 'OPTIONS',
url: 'http://localhost:' + fastify.server.address().port,
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/jsoff'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
})
})
})
})