CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
context-config.test.js165 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4 5const { kRouteContext } = require('../lib/symbols')6const Fastify = require('..')7 8const schema = {9  schema: { },10  config: {11    value1: 'foo',12    value2: true13  }14}15 16function handler (req, reply) {17  reply.send(reply[kRouteContext].config)18}19 20test('config', async t => {21  t.plan(6)22  const fastify = Fastify()23 24  fastify.get('/get', {25    schema: schema.schema,26    config: Object.assign({}, schema.config)27  }, handler)28 29  fastify.route({30    method: 'GET',31    url: '/route',32    schema: schema.schema,33    handler,34    config: Object.assign({}, schema.config)35  })36 37  fastify.route({38    method: 'GET',39    url: '/no-config',40    schema: schema.schema,41    handler42  })43 44  let response = await fastify.inject({45    method: 'GET',46    url: '/route'47  })48 49  t.assert.strictEqual(response.statusCode, 200)50  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))51 52  response = await fastify.inject({53    method: 'GET',54    url: '/route'55  })56 57  t.assert.strictEqual(response.statusCode, 200)58  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))59 60  response = await fastify.inject({61    method: 'GET',62    url: '/no-config'63  })64 65  t.assert.strictEqual(response.statusCode, 200)66  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })67})68 69test('config with exposeHeadRoutes', async t => {70  t.plan(6)71  const fastify = Fastify({ exposeHeadRoutes: true })72 73  fastify.get('/get', {74    schema: schema.schema,75    config: Object.assign({}, schema.config)76  }, handler)77 78  fastify.route({79    method: 'GET',80    url: '/route',81    schema: schema.schema,82    handler,83    config: Object.assign({}, schema.config)84  })85 86  fastify.route({87    method: 'GET',88    url: '/no-config',89    schema: schema.schema,90    handler91  })92 93  let response = await fastify.inject({94    method: 'GET',95    url: '/get'96  })97 98  t.assert.strictEqual(response.statusCode, 200)99  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))100 101  response = await fastify.inject({102    method: 'GET',103    url: '/route'104  })105 106  t.assert.strictEqual(response.statusCode, 200)107  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))108 109  response = await fastify.inject({110    method: 'GET',111    url: '/no-config'112  })113 114  t.assert.strictEqual(response.statusCode, 200)115  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })116})117 118test('config without exposeHeadRoutes', async t => {119  t.plan(6)120  const fastify = Fastify({ exposeHeadRoutes: false })121 122  fastify.get('/get', {123    schema: schema.schema,124    config: Object.assign({}, schema.config)125  }, handler)126 127  fastify.route({128    method: 'GET',129    url: '/route',130    schema: schema.schema,131    handler,132    config: Object.assign({}, schema.config)133  })134 135  fastify.route({136    method: 'GET',137    url: '/no-config',138    schema: schema.schema,139    handler140  })141 142  let response = await fastify.inject({143    method: 'GET',144    url: '/get'145  })146 147  t.assert.strictEqual(response.statusCode, 200)148  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/get', method: 'GET' }, schema.config))149 150  response = await fastify.inject({151    method: 'GET',152    url: '/route'153  })154  t.assert.strictEqual(response.statusCode, 200)155  t.assert.deepStrictEqual(response.json(), Object.assign({ url: '/route', method: 'GET' }, schema.config))156 157  response = await fastify.inject({158    method: 'GET',159    url: '/no-config'160  })161 162  t.assert.strictEqual(response.statusCode, 200)163  t.assert.deepStrictEqual(response.json(), { url: '/no-config', method: 'GET' })164})165