strong-tie/inbound-calls
0
1'use strict'2 3const assert = require('node:assert')4const { test } = require('node:test')5const sget = require('simple-get').concat6const fastify = require('..')()7 8const schema = {9 schema: {10 response: {11 '2xx': {12 type: 'object',13 properties: {14 hello: {15 type: 'string'16 }17 }18 }19 }20 }21}22 23const querySchema = {24 schema: {25 querystring: {26 type: 'object',27 properties: {28 hello: {29 type: 'integer'30 }31 }32 }33 }34}35 36const paramsSchema = {37 schema: {38 params: {39 type: 'object',40 properties: {41 foo: {42 type: 'string'43 },44 test: {45 type: 'integer'46 }47 }48 }49 }50}51 52const headersSchema = {53 schema: {54 headers: {55 type: 'object',56 properties: {57 'x-test': {58 type: 'number'59 }60 }61 }62 }63}64 65const bodySchema = {66 schema: {67 body: {68 type: 'object',69 properties: {70 hello: {71 type: 'string'72 }73 }74 },75 response: {76 '2xx': {77 type: 'object',78 properties: {79 hello: {80 type: 'string'81 }82 }83 }84 }85 }86}87 88test('shorthand - delete', (t, done) => {89 t.plan(1)90 try {91 fastify.delete('/', schema, function (req, reply) {92 reply.code(200).send({ hello: 'world' })93 })94 t.assert.ok(true)95 } catch (e) {96 t.assert.fail()97 } finally {98 done()99 }100})101 102test('shorthand - delete params', t => {103 t.plan(1)104 try {105 fastify.delete('/params/:foo/:test', paramsSchema, function (req, reply) {106 reply.code(200).send(req.params)107 })108 t.assert.ok(true)109 } catch (e) {110 t.assert.fail()111 }112})113 114test('shorthand - delete, querystring schema', t => {115 t.plan(1)116 try {117 fastify.delete('/query', querySchema, function (req, reply) {118 reply.send(req.query)119 })120 t.assert.ok(true)121 } catch (e) {122 t.assert.fail()123 }124})125 126test('shorthand - get, headers schema', t => {127 t.plan(1)128 try {129 fastify.delete('/headers', headersSchema, function (req, reply) {130 reply.code(200).send(req.headers)131 })132 t.assert.ok(true)133 } catch (e) {134 t.assert.fail()135 }136})137 138test('missing schema - delete', t => {139 t.plan(1)140 try {141 fastify.delete('/missing', function (req, reply) {142 reply.code(200).send({ hello: 'world' })143 })144 t.assert.ok(true)145 } catch (e) {146 t.assert.fail()147 }148})149 150test('body - delete', t => {151 t.plan(1)152 try {153 fastify.delete('/body', bodySchema, function (req, reply) {154 reply.send(req.body)155 })156 t.assert.ok(true)157 } catch (e) {158 t.assert.fail()159 }160})161 162fastify.listen({ port: 0 }, err => {163 assert.ifError(err)164 test.after(() => { fastify.close() })165 166 test('shorthand - request delete', (t, done) => {167 t.plan(4)168 sget({169 method: 'DELETE',170 url: 'http://localhost:' + fastify.server.address().port171 }, (err, response, body) => {172 t.assert.ifError(err)173 t.assert.strictEqual(response.statusCode, 200)174 t.assert.strictEqual(response.headers['content-length'], '' + body.length)175 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })176 done()177 })178 })179 180 test('shorthand - request delete params schema', (t, done) => {181 t.plan(4)182 sget({183 method: 'DELETE',184 url: 'http://localhost:' + fastify.server.address().port + '/params/world/123'185 }, (err, response, body) => {186 t.assert.ifError(err)187 t.assert.strictEqual(response.statusCode, 200)188 t.assert.strictEqual(response.headers['content-length'], '' + body.length)189 t.assert.deepStrictEqual(JSON.parse(body), { foo: 'world', test: 123 })190 done()191 })192 })193 194 test('shorthand - request delete params schema error', (t, done) => {195 t.plan(3)196 sget({197 method: 'DELETE',198 url: 'http://localhost:' + fastify.server.address().port + '/params/world/string'199 }, (err, response, body) => {200 t.assert.ifError(err)201 t.assert.strictEqual(response.statusCode, 400)202 t.assert.deepStrictEqual(JSON.parse(body), {203 error: 'Bad Request',204 code: 'FST_ERR_VALIDATION',205 message: 'params/test must be integer',206 statusCode: 400207 })208 done()209 })210 })211 212 test('shorthand - request delete headers schema', (t, done) => {213 t.plan(4)214 sget({215 method: 'DELETE',216 headers: {217 'x-test': 1218 },219 url: 'http://localhost:' + fastify.server.address().port + '/headers'220 }, (err, response, body) => {221 t.assert.ifError(err)222 t.assert.strictEqual(response.statusCode, 200)223 t.assert.strictEqual(response.headers['content-length'], '' + body.length)224 t.assert.strictEqual(JSON.parse(body)['x-test'], 1)225 done()226 })227 })228 229 test('shorthand - request delete headers schema error', (t, done) => {230 t.plan(3)231 sget({232 method: 'DELETE',233 headers: {234 'x-test': 'abc'235 },236 url: 'http://localhost:' + fastify.server.address().port + '/headers'237 }, (err, response, body) => {238 t.assert.ifError(err)239 t.assert.strictEqual(response.statusCode, 400)240 t.assert.deepStrictEqual(JSON.parse(body), {241 error: 'Bad Request',242 code: 'FST_ERR_VALIDATION',243 message: 'headers/x-test must be number',244 statusCode: 400245 })246 done()247 })248 })249 250 test('shorthand - request delete querystring schema', (t, done) => {251 t.plan(4)252 sget({253 method: 'DELETE',254 url: 'http://localhost:' + fastify.server.address().port + '/query?hello=123'255 }, (err, response, body) => {256 t.assert.ifError(err)257 t.assert.strictEqual(response.statusCode, 200)258 t.assert.strictEqual(response.headers['content-length'], '' + body.length)259 t.assert.deepStrictEqual(JSON.parse(body), { hello: 123 })260 done()261 })262 })263 264 test('shorthand - request delete querystring schema error', (t, done) => {265 t.plan(3)266 sget({267 method: 'DELETE',268 url: 'http://localhost:' + fastify.server.address().port + '/query?hello=world'269 }, (err, response, body) => {270 t.assert.ifError(err)271 t.assert.strictEqual(response.statusCode, 400)272 t.assert.deepStrictEqual(JSON.parse(body), {273 error: 'Bad Request',274 code: 'FST_ERR_VALIDATION',275 message: 'querystring/hello must be integer',276 statusCode: 400277 })278 done()279 })280 })281 282 test('shorthand - request delete missing schema', (t, done) => {283 t.plan(4)284 sget({285 method: 'DELETE',286 url: 'http://localhost:' + fastify.server.address().port + '/missing'287 }, (err, response, body) => {288 t.assert.ifError(err)289 t.assert.strictEqual(response.statusCode, 200)290 t.assert.strictEqual(response.headers['content-length'], '' + body.length)291 t.assert.deepStrictEqual(JSON.parse(body), { hello: 'world' })292 done()293 })294 })295 296 test('shorthand - delete with body', (t, done) => {297 t.plan(3)298 sget({299 method: 'DELETE',300 url: 'http://localhost:' + fastify.server.address().port + '/body',301 body: {302 hello: 'world'303 },304 json: true305 }, (err, response, body) => {306 t.assert.ifError(err)307 t.assert.strictEqual(response.statusCode, 200)308 t.assert.deepStrictEqual(body, { hello: 'world' })309 done()310 })311 })312})313 314test('shorthand - delete with application/json Content-Type header and null body', (t, done) => {315 t.plan(4)316 const fastify = require('..')()317 fastify.delete('/', {}, (req, reply) => {318 t.assert.strictEqual(req.body, null)319 reply.send(req.body)320 })321 fastify.inject({322 method: 'DELETE',323 url: '/',324 headers: { 'Content-Type': 'application/json' },325 body: 'null'326 }, (err, response) => {327 t.assert.ifError(err)328 t.assert.strictEqual(response.statusCode, 200)329 t.assert.strictEqual(response.payload.toString(), 'null')330 done()331 })332})333 334// https://github.com/fastify/fastify/issues/936335// Skip this test because this is an invalid request336test('shorthand - delete with application/json Content-Type header and without body', { skip: 'https://github.com/fastify/fastify/pull/5419' }, t => {337 t.plan(4)338 const fastify = require('..')()339 fastify.delete('/', {}, (req, reply) => {340 t.assert.strictEqual(req.body, undefined)341 reply.send(req.body)342 })343 fastify.inject({344 method: 'DELETE',345 url: '/',346 headers: { 'Content-Type': 'application/json' },347 body: null348 }, (err, response) => {349 t.assert.ifError(err)350 t.assert.strictEqual(response.statusCode, 200)351 t.assert.strictEqual(response.payload.toString(), '')352 })353})354 