ronantakizawa/codereview-bench huggingface dataset:
Repository: fastify/fastify
File: test/context-config.test.js (JavaScript)

Code before:

  let response = await fastify.inject({
    method: 'GET',
    url: '/get'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config without exposeHeadRoutes', async t => {
  t.plan(9)
  const fastify = Fastify({ exposeHeadRoutes: false })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler

Review comment (suggestion):
```suggestion
  t.assert.strictEqual(response.statusCode, 200)
```

Code after:
    url: '/get'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config without exposeHeadRoutes', async t => {
  t.plan(6)
  const fastify = Fastify({ exposeHeadRoutes: false })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/get'
  })
Repository: fastify/fastify
File: test/context-config.test.js (JavaScript)

Code before:
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/get'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config without exposeHeadRoutes', async t => {
  t.plan(9)
  const fastify = Fastify({ exposeHeadRoutes: false })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,

Review comment (suggestion):
```suggestion
  t.assert.strictEqual(response.statusCode, 200)
```

Code after:
  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/get'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config without exposeHeadRoutes', async t => {
  t.plan(6)
  const fastify = Fastify({ exposeHeadRoutes: false })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
Repository: fastify/fastify
File: test/context-config.test.js (JavaScript)

Code before:
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/get'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config without exposeHeadRoutes', async t => {
  t.plan(9)
  const fastify = Fastify({ exposeHeadRoutes: false })


Review comment (suggestion):
```suggestion
  t.assert.strictEqual(response.statusCode, 200)
```

Code after:

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/get'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config without exposeHeadRoutes', async t => {
  t.plan(6)
  const fastify = Fastify({ exposeHeadRoutes: false })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
Repository: fastify/fastify
File: test/content-type.test.js (JavaScript)

Code before:
  })
  fastify.addHook('onSend', async function (request, reply, payload) {
    count++
    t.assert.strictEqual(typeof payload, 'string')
    switch (count) {
      case 1: {
        // should guess the correct content-type based on payload
        t.assert.strictEqual(reply.getHeader('content-type'), 'text/plain; charset=utf-8')
        throw Error('kaboom')
      }
      case 2: {
        // should guess the correct content-type based on payload
        t.assert.strictEqual(reply.getHeader('content-type'), 'application/json; charset=utf-8')
        return payload
      }
      default: {
        t.fail('should not reach')
      }
    }
  })
  fastify.get('/', function (request, reply) {
    reply.send('plain-text')
  })

  const { statusCode, body } = await fastify.inject({ method: 'GET', path: '/' })
  t.assert.equal(statusCode, 400)
  t.assert.strictEqual(body, JSON.stringify({ foo: 'bar' }))
})


Review comment (suggestion):
```suggestion
  t.assert.strictEqual(statusCode, 400)
```

Code after:
  })
  fastify.addHook('onSend', async function (request, reply, payload) {
    count++
    t.assert.strictEqual(typeof payload, 'string')
    switch (count) {
      case 1: {
        // should guess the correct content-type based on payload
        t.assert.strictEqual(reply.getHeader('content-type'), 'text/plain; charset=utf-8')
        throw Error('kaboom')
      }
      case 2: {
        // should guess the correct content-type based on payload
        t.assert.strictEqual(reply.getHeader('content-type'), 'application/json; charset=utf-8')
        return payload
      }
      default: {
        t.fail('should not reach')
      }
    }
  })
  fastify.get('/', function (request, reply) {
    reply.send('plain-text')
  })

  const { statusCode, body } = await fastify.inject({ method: 'GET', path: '/' })
  t.assert.strictEqual(statusCode, 400)
  t.assert.strictEqual(body, JSON.stringify({ foo: 'bar' }))
})
Repository: fastify/fastify
File: test/context-config.test.js (JavaScript)

Code before:
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config with exposeHeadRoutes', async t => {
  t.plan(9)
  const fastify = Fastify({ exposeHeadRoutes: true })


Review comment (suggestion):
```suggestion
  t.assert.strictEqual(response.statusCode, 200)
```

Code after:
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config with exposeHeadRoutes', async t => {
  t.plan(6)
  const fastify = Fastify({ exposeHeadRoutes: true })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
Repository: fastify/fastify
File: test/context-config.test.js (JavaScript)

Code before:
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config with exposeHeadRoutes', async t => {
  t.plan(9)
  const fastify = Fastify({ exposeHeadRoutes: true })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,

Review comment (suggestion):
```suggestion
  t.assert.strictEqual(response.statusCode, 200)
```

Code after:
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config with exposeHeadRoutes', async t => {
  t.plan(6)
  const fastify = Fastify({ exposeHeadRoutes: true })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })
Repository: fastify/fastify
File: test/context-config.test.js (JavaScript)

Code before:

  let response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.ifError(response.error)
  t.assert.equal(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config with exposeHeadRoutes', async t => {
  t.plan(9)
  const fastify = Fastify({ exposeHeadRoutes: true })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler

Review comment (suggestion):
```suggestion
  t.assert.strictEqual(response.statusCode, 200)
```

Code after:

  let response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config with exposeHeadRoutes', async t => {
  t.plan(6)
  const fastify = Fastify({ exposeHeadRoutes: true })

  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
Repository: fastify/fastify
File: test/context-config.test.js (JavaScript)

Code before:
  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.ifError(response.error)
  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.ifError(response.error)
  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config with exposeHeadRoutes', async t => {
  t.plan(9)
  const fastify = Fastify({ exposeHeadRoutes: true })

Review comment (suggestion):
All the `ifError` condition are confusing and we can remove it because `response.error` is always `undefined` + (and the `t.plan()` needs to be decreased accordingly

```suggestion
```

Code after:
  fastify.get('/get', {
    schema: schema.schema,
    config: Object.assign({}, schema.config)
  }, handler)

  fastify.route({
    method: 'GET',
    url: '/route',
    schema: schema.schema,
    handler,
    config: Object.assign({}, schema.config)
  })

  fastify.route({
    method: 'GET',
    url: '/no-config',
    schema: schema.schema,
    handler
  })

  let response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/route'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))

  response = await fastify.inject({
    method: 'GET',
    url: '/no-config'
  })

  t.assert.strictEqual(response.statusCode, 200)
  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })
})

test('config with exposeHeadRoutes', async t => {
  t.plan(6)
  const fastify = Fastify({ exposeHeadRoutes: true })

  fastify.get('/get', {
    schema: schema.schema,
Repository: fastify/fastify
File: test/route.3.test.js (JavaScript)

Code before:
  const fastify = Fastify()
  function validatorCompiler ({ schema, method, url, httpPart }) {
    // Needed to extract the params part,
    // without the JSON-schema encapsulation
    // that is automatically added by the short
    // form of params.
    schema = joi.object(schema.properties)

    return validateHttpData

    function validateHttpData (data) {
      return schema.validate(data)
    }
  }

  fastify.setValidatorCompiler(validatorCompiler)

  fastify.route({
    path: '/foo/:an_id',
    method: 'GET',
    schema: {
      params: { an_id: joi.number() }
    },
    handler (req, res) {
      console.log(req.params)
      t.assert.deepEqual(req.params, { an_id: 42 })
      res.send({ hello: 'world' })
    }
  })

  fastify.inject({
    method: 'GET',
    url: '/foo/42'
  }, (err, res) => {
    t.assert.ifError(err)
    t.assert.strictEqual(res.statusCode, 200)
    t.assert.deepStrictEqual(JSON.parse(res.payload), { hello: 'world' })
    done()
  })
})

test('multiple routes with one schema', (t, done) => {
  t.plan(2)

  const fastify = Fastify()

  const schema = {
    query: {
      type: 'object',
      properties: {
        id: { type: 'number' }

Review comment (suggestion):
```suggestion
      t.assert.deepEqual(req.params, { an_id: 42 })
```

Code after:
  const fastify = Fastify()
  function validatorCompiler ({ schema, method, url, httpPart }) {
    // Needed to extract the params part,
    // without the JSON-schema encapsulation
    // that is automatically added by the short
    // form of params.
    schema = joi.object(schema.properties)

    return validateHttpData

    function validateHttpData (data) {
      return schema.validate(data)
    }
  }

  fastify.setValidatorCompiler(validatorCompiler)

  fastify.route({
    path: '/foo/:an_id',
    method: 'GET',
    schema: {
      params: { an_id: joi.number() }
    },
    handler (req, res) {
      t.assert.deepEqual(req.params, { an_id: 42 })
      res.send({ hello: 'world' })
    }
  })

  fastify.inject({
    method: 'GET',
    url: '/foo/42'
  }, (err, res) => {
    t.assert.ifError(err)
    t.assert.strictEqual(res.statusCode, 200)
    t.assert.deepStrictEqual(res.json(), { hello: 'world' })
    done()
  })
})

test('multiple routes with one schema', (t, done) => {
  t.plan(2)

  const fastify = Fastify()

  const schema = {
    query: {
      type: 'object',
      properties: {
        id: { type: 'number' }
      }