strong-tie/inbound-calls
0
1'use strict'2 3const t = require('tap')4const test = t.test5const querystring = require('node:querystring')6const sget = require('simple-get').concat7const Fastify = require('..')8 9test('Custom querystring parser', t => {10 t.plan(9)11 12 const fastify = Fastify({13 querystringParser: function (str) {14 t.equal(str, 'foo=bar&baz=faz')15 return querystring.parse(str)16 }17 })18 19 fastify.get('/', (req, reply) => {20 t.same(req.query, {21 foo: 'bar',22 baz: 'faz'23 })24 reply.send({ hello: 'world' })25 })26 27 fastify.listen({ port: 0 }, (err, address) => {28 t.error(err)29 t.teardown(() => fastify.close())30 31 sget({32 method: 'GET',33 url: `${address}?foo=bar&baz=faz`34 }, (err, response, body) => {35 t.error(err)36 t.equal(response.statusCode, 200)37 })38 39 fastify.inject({40 method: 'GET',41 url: `${address}?foo=bar&baz=faz`42 }, (err, response, body) => {43 t.error(err)44 t.equal(response.statusCode, 200)45 })46 })47})48 49test('Custom querystring parser should be called also if there is nothing to parse', t => {50 t.plan(9)51 52 const fastify = Fastify({53 querystringParser: function (str) {54 t.equal(str, '')55 return querystring.parse(str)56 }57 })58 59 fastify.get('/', (req, reply) => {60 t.same(req.query, {})61 reply.send({ hello: 'world' })62 })63 64 fastify.listen({ port: 0 }, (err, address) => {65 t.error(err)66 t.teardown(() => fastify.close())67 68 sget({69 method: 'GET',70 url: address71 }, (err, response, body) => {72 t.error(err)73 t.equal(response.statusCode, 200)74 })75 76 fastify.inject({77 method: 'GET',78 url: address79 }, (err, response, body) => {80 t.error(err)81 t.equal(response.statusCode, 200)82 })83 })84})85 86test('Querystring without value', t => {87 t.plan(9)88 89 const fastify = Fastify({90 querystringParser: function (str) {91 t.equal(str, 'foo')92 return querystring.parse(str)93 }94 })95 96 fastify.get('/', (req, reply) => {97 t.same(req.query, { foo: '' })98 reply.send({ hello: 'world' })99 })100 101 fastify.listen({ port: 0 }, (err, address) => {102 t.error(err)103 t.teardown(() => fastify.close())104 105 sget({106 method: 'GET',107 url: `${address}?foo`108 }, (err, response, body) => {109 t.error(err)110 t.equal(response.statusCode, 200)111 })112 113 fastify.inject({114 method: 'GET',115 url: `${address}?foo`116 }, (err, response, body) => {117 t.error(err)118 t.equal(response.statusCode, 200)119 })120 })121})122 123test('Custom querystring parser should be a function', t => {124 t.plan(1)125 126 try {127 Fastify({128 querystringParser: 10129 })130 t.fail('Should throw')131 } catch (err) {132 t.equal(133 err.message,134 "querystringParser option should be a function, instead got 'number'"135 )136 }137})138 