'use strict'

const test = require('tap').test
const fastify = require('..')()

test('Fastify should throw on wrong options', t => {
  t.plan(2)
  try {
    const f = require('..')('lol') // eslint-disable-line
    t.fail()
  } catch (e) {
    t.is(e.message, 'Options must be an object')
    t.pass()
  }
})

test('Fastify should throw on multiple assignment to the same route', t => {
  t.plan(1)
  fastify.get('/', () => {})
  fastify.get('/', () => {})

  fastify.ready(err => {
    t.is(err.message, 'GET already set for /')
  })
})

test('Should throw on unsupported method', t => {
  t.plan(1)
  try {
    fastify.route({
      method: 'TROLL',
      url: '/',
      schema: {},
      handler: function (req, reply) {}
    })
    t.fail()
  } catch (e) {
    t.pass()
  }
})

test('Should throw on missing handler', t => {
  t.plan(1)
  try {
    fastify.route({
      method: 'GET',
      url: '/'
    })
    t.fail()
  } catch (e) {
    t.pass()
  }
})

test('Should throw if one method is unsupported', t => {
  t.plan(1)
  try {
    fastify.route({
      method: ['GET', 'TROLL'],
      url: '/',
      schema: {},
      handler: function (req, reply) {}
    })
    t.fail()
  } catch (e) {
    t.pass()
  }
})

test('Should throw on duplicate content type parser', t => {
  t.plan(1)

  function customParser (req, done) { done(null, '') }

  fastify.addContentTypeParser('application/qq', customParser)
  try {
    fastify.addContentTypeParser('application/qq', customParser)
    t.fail()
  } catch (e) {
    t.pass()
  }
})

test('Should throw on duplicate decorator', t => {
  t.plan(1)

  const fooObj = {}

  fastify.decorate('foo', fooObj)
  try {
    fastify.decorate('foo', fooObj)
    t.fail()
  } catch (e) {
    t.pass()
  }
})

test('Should throw on duplicate decorator encapsulation', t => {
  t.plan(1)

  const foo2Obj = {}

  fastify.decorate('foo2', foo2Obj)

  fastify.register(function (fastify, opts, next) {
    try {
      fastify.decorate('foo2', foo2Obj)
      t.fail()
    } catch (e) {
      t.pass()
    }
    next()
  })
})

@@ -2049,16 +2049,556 @@
  next()%0A  %7D)%0A%7D)%0A
+%0Atest('Should throw on duplicate request decorator', t =%3E %7B%0A  t.plan(1)%0A%0A  const fooObj = %7B%7D%0A%0A  fastify.decorateRequest('foo', fooObj)%0A  try %7B%0A    fastify.decorateRequest('foo', fooObj)%0A    // TODO: this should be t.fail()!%0A    t.pass()%0A  %7D catch (e) %7B%0A    t.pass()%0A  %7D%0A%7D)%0A%0Atest('Should throw on duplicate reply decorator', t =%3E %7B%0A  t.plan(1)%0A%0A  const fooObj = %7B%7D%0A%0A  fastify.decorateReply('foo', fooObj)%0A  try %7B%0A    fastify.decorateReply('foo', fooObj)%0A    // TODO: this should be t.fail()!%0A    t.pass()%0A  %7D catch (e) %7B%0A    t.pass()%0A  %7D%0A%7D)%0A

Add test - Should throw on duplicate request/reply decorator