You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							132 lines
						
					
					
						
							3.1 KiB
						
					
					
				
			
		
		
	
	
							132 lines
						
					
					
						
							3.1 KiB
						
					
					
				'use strict'
 | 
						|
 | 
						|
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(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' }))
 | 
						|
      })
 | 
						|
    })
 | 
						|
  })
 | 
						|
})
 | 
						|
 | 
						|
test('contentTypeParser should add a custom async parser - deprecated syntax', t => {
 | 
						|
  t.plan(5)
 | 
						|
  const fastify = Fastify()
 | 
						|
 | 
						|
  process.on('warning', onWarning)
 | 
						|
  function onWarning (warning) {
 | 
						|
    t.equal(warning.name, 'FastifyDeprecation')
 | 
						|
    t.equal(warning.code, 'FSTDEP003')
 | 
						|
  }
 | 
						|
 | 
						|
  fastify.post('/', (req, reply) => {
 | 
						|
    reply.send(req.body)
 | 
						|
  })
 | 
						|
 | 
						|
  fastify.options('/', (req, reply) => {
 | 
						|
    reply.send(req.body)
 | 
						|
  })
 | 
						|
 | 
						|
  fastify.addContentTypeParser('application/jsoff', async function (req) {
 | 
						|
    const res = await new Promise((resolve, reject) => resolve(req))
 | 
						|
    return res
 | 
						|
  })
 | 
						|
 | 
						|
  fastify.listen(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' }))
 | 
						|
        process.removeListener('warning', onWarning)
 | 
						|
      })
 | 
						|
    })
 | 
						|
  })
 | 
						|
})
 |