CoolFace
Apppublic

strong-tie/inbound-calls

sourceHugging Faceupdated 2y agoView on Hugging Face
0likes
nullable-validation.test.js201 linesDownload Raw Back to test
1'use strict'2 3const { test } = require('node:test')4const sget = require('simple-get').concat5const Fastify = require('..')6 7test('nullable string', (t, done) => {8  t.plan(3)9  const fastify = Fastify()10  fastify.route({11    method: 'POST',12    url: '/',13    handler: (req, reply) => {14      t.assert.strictEqual(req.body.hello, null)15      reply.code(200).send(req.body)16    },17    schema: {18      body: {19        type: 'object',20        properties: {21          hello: {22            type: 'string',23            format: 'email',24            nullable: true25          }26        }27      },28      response: {29        200: {30          type: 'object',31          properties: {32            hello: {33              type: 'string',34              format: 'email',35              nullable: true36            }37          }38        }39      }40    }41  })42  fastify.inject({43    method: 'POST',44    url: '/',45    body: {46      hello: null47    }48  }, (err, res) => {49    t.assert.ifError(err)50    t.assert.strictEqual(res.json().hello, null)51    done()52  })53})54 55test('object or null body', (t, done) => {56  t.plan(5)57 58  const fastify = Fastify()59 60  fastify.route({61    method: 'POST',62    url: '/',63    handler: (req, reply) => {64      t.assert.strictEqual(req.body, undefined)65      reply.code(200).send({ isUndefinedBody: req.body === undefined })66    },67    schema: {68      body: {69        type: ['object', 'null'],70        properties: {71          hello: {72            type: 'string',73            format: 'email'74          }75        }76      },77      response: {78        200: {79          type: 'object',80          nullable: true,81          properties: {82            isUndefinedBody: {83              type: 'boolean'84            }85          }86        }87      }88    }89  })90 91  fastify.listen({ port: 0 }, (err) => {92    t.assert.ifError(err)93    t.after(() => { fastify.close() })94 95    sget({96      method: 'POST',97      url: 'http://localhost:' + fastify.server.address().port98    }, (err, response, body) => {99      t.assert.ifError(err)100      t.assert.strictEqual(response.statusCode, 200)101      t.assert.deepStrictEqual(JSON.parse(body), { isUndefinedBody: true })102      done()103    })104  })105})106 107test('nullable body', (t, done) => {108  t.plan(5)109 110  const fastify = Fastify()111 112  fastify.route({113    method: 'POST',114    url: '/',115    handler: (req, reply) => {116      t.assert.strictEqual(req.body, undefined)117      reply.code(200).send({ isUndefinedBody: req.body === undefined })118    },119    schema: {120      body: {121        type: 'object',122        nullable: true,123        properties: {124          hello: {125            type: 'string',126            format: 'email'127          }128        }129      },130      response: {131        200: {132          type: 'object',133          nullable: true,134          properties: {135            isUndefinedBody: {136              type: 'boolean'137            }138          }139        }140      }141    }142  })143 144  fastify.listen({ port: 0 }, (err) => {145    t.assert.ifError(err)146    t.after(() => fastify.close())147 148    sget({149      method: 'POST',150      url: 'http://localhost:' + fastify.server.address().port151    }, (err, response, body) => {152      t.assert.ifError(err)153      t.assert.strictEqual(response.statusCode, 200)154      t.assert.deepStrictEqual(JSON.parse(body), { isUndefinedBody: true })155      done()156    })157  })158})159 160test('Nullable body with 204', (t, done) => {161  t.plan(5)162 163  const fastify = Fastify()164 165  fastify.route({166    method: 'POST',167    url: '/',168    handler: (req, reply) => {169      t.assert.strictEqual(req.body, undefined)170      reply.code(204).send()171    },172    schema: {173      body: {174        type: 'object',175        nullable: true,176        properties: {177          hello: {178            type: 'string',179            format: 'email'180          }181        }182      }183    }184  })185 186  fastify.listen({ port: 0 }, (err) => {187    t.assert.ifError(err)188    t.after(() => fastify.close())189 190    sget({191      method: 'POST',192      url: 'http://localhost:' + fastify.server.address().port193    }, (err, response, body) => {194      t.assert.ifError(err)195      t.assert.strictEqual(response.statusCode, 204)196      t.assert.strictEqual(body.length, 0)197      done()198    })199  })200})201