strong-tie/inbound-calls
0
1'use strict'2 3const t = require('tap')4const test = t.test5const sget = require('simple-get').concat6const Fastify = require('../fastify')7const jsonParser = require('fast-json-body')8const { getServerUrl } = require('./helper')9 10process.removeAllListeners('warning')11 12test('Should have typeof body object with no custom parser defined, null body and content type = \'text/plain\'', t => {13 t.plan(4)14 const fastify = Fastify()15 16 fastify.post('/', (req, reply) => {17 reply.send(req.body)18 })19 20 fastify.listen({ port: 0 }, err => {21 t.error(err)22 23 sget({24 method: 'POST',25 url: getServerUrl(fastify),26 body: null,27 headers: {28 'Content-Type': 'text/plain'29 }30 }, (err, response, body) => {31 t.error(err)32 t.equal(response.statusCode, 200)33 t.equal(typeof body, 'object')34 fastify.close()35 })36 })37})38 39test('Should have typeof body object with no custom parser defined, undefined body and content type = \'text/plain\'', t => {40 t.plan(4)41 const fastify = Fastify()42 43 fastify.post('/', (req, reply) => {44 reply.send(req.body)45 })46 47 fastify.listen({ port: 0 }, err => {48 t.error(err)49 50 sget({51 method: 'POST',52 url: getServerUrl(fastify),53 body: undefined,54 headers: {55 'Content-Type': 'text/plain'56 }57 }, (err, response, body) => {58 t.error(err)59 t.equal(response.statusCode, 200)60 t.equal(typeof body, 'object')61 fastify.close()62 })63 })64})65 66test('Should get the body as string /1', t => {67 t.plan(6)68 const fastify = Fastify()69 70 fastify.post('/', (req, reply) => {71 reply.send(req.body)72 })73 74 fastify.addContentTypeParser('text/plain', { parseAs: 'string' }, function (req, body, done) {75 t.ok('called')76 t.ok(typeof body === 'string')77 try {78 const plainText = body79 done(null, plainText)80 } catch (err) {81 err.statusCode = 40082 done(err, undefined)83 }84 })85 86 fastify.listen({ port: 0 }, err => {87 t.error(err)88 89 sget({90 method: 'POST',91 url: getServerUrl(fastify),92 body: 'hello world',93 headers: {94 'Content-Type': 'text/plain'95 }96 }, (err, response, body) => {97 t.error(err)98 t.equal(response.statusCode, 200)99 t.equal(body.toString(), 'hello world')100 fastify.close()101 })102 })103})104 105test('Should get the body as string /2', t => {106 t.plan(6)107 const fastify = Fastify()108 109 fastify.post('/', (req, reply) => {110 reply.send(req.body)111 })112 113 fastify.addContentTypeParser('text/plain/test', { parseAs: 'string' }, function (req, body, done) {114 t.ok('called')115 t.ok(typeof body === 'string')116 try {117 const plainText = body118 done(null, plainText)119 } catch (err) {120 err.statusCode = 400121 done(err, undefined)122 }123 })124 125 fastify.listen({ port: 0 }, err => {126 t.error(err)127 128 sget({129 method: 'POST',130 url: getServerUrl(fastify),131 body: 'hello world',132 headers: {133 'Content-Type': ' text/plain/test '134 }135 }, (err, response, body) => {136 t.error(err)137 t.equal(response.statusCode, 200)138 t.equal(body.toString(), 'hello world')139 fastify.close()140 })141 })142})143 144test('Should get the body as buffer', t => {145 t.plan(6)146 const fastify = Fastify()147 148 fastify.post('/', (req, reply) => {149 reply.send(req.body)150 })151 152 fastify.addContentTypeParser('application/json', { parseAs: 'buffer' }, function (req, body, done) {153 t.ok('called')154 t.ok(body instanceof Buffer)155 try {156 const json = JSON.parse(body)157 done(null, json)158 } catch (err) {159 err.statusCode = 400160 done(err, undefined)161 }162 })163 164 fastify.listen({ port: 0 }, err => {165 t.error(err)166 167 sget({168 method: 'POST',169 url: getServerUrl(fastify),170 body: '{"hello":"world"}',171 headers: {172 'Content-Type': 'application/json'173 }174 }, (err, response, body) => {175 t.error(err)176 t.equal(response.statusCode, 200)177 t.equal(body.toString(), '{"hello":"world"}')178 fastify.close()179 })180 })181})182 183test('Should get the body as buffer', t => {184 t.plan(6)185 const fastify = Fastify()186 187 fastify.post('/', (req, reply) => {188 reply.send(req.body)189 })190 191 fastify.addContentTypeParser('text/plain', { parseAs: 'buffer' }, function (req, body, done) {192 t.ok('called')193 t.ok(body instanceof Buffer)194 try {195 const plainText = body196 done(null, plainText)197 } catch (err) {198 err.statusCode = 400199 done(err, undefined)200 }201 })202 203 fastify.listen({ port: 0 }, err => {204 t.error(err)205 206 sget({207 method: 'POST',208 url: getServerUrl(fastify),209 body: 'hello world',210 headers: {211 'Content-Type': 'text/plain'212 }213 }, (err, response, body) => {214 t.error(err)215 t.equal(response.statusCode, 200)216 t.equal(body.toString(), 'hello world')217 fastify.close()218 })219 })220})221 222test('Should parse empty bodies as a string', t => {223 t.plan(9)224 const fastify = Fastify()225 226 fastify.addContentTypeParser('text/plain', { parseAs: 'string' }, (req, body, done) => {227 t.equal(body, '')228 done(null, body)229 })230 231 fastify.route({232 method: ['POST', 'DELETE'],233 url: '/',234 handler (request, reply) {235 reply.send(request.body)236 }237 })238 239 fastify.listen({ port: 0 }, err => {240 t.error(err)241 t.teardown(() => { fastify.close() })242 243 sget({244 method: 'POST',245 url: getServerUrl(fastify),246 body: '',247 headers: {248 'Content-Type': 'text/plain'249 }250 }, (err, response, body) => {251 t.error(err)252 t.equal(response.statusCode, 200)253 t.equal(body.toString(), '')254 })255 256 sget({257 method: 'DELETE',258 url: getServerUrl(fastify),259 body: '',260 headers: {261 'Content-Type': 'text/plain',262 'Content-Length': '0'263 }264 }, (err, response, body) => {265 t.error(err)266 t.equal(response.statusCode, 200)267 t.equal(body.toString(), '')268 })269 })270})271 272test('Should parse empty bodies as a buffer', t => {273 t.plan(6)274 const fastify = Fastify()275 276 fastify.post('/', (req, reply) => {277 reply.send(req.body)278 })279 280 fastify.addContentTypeParser('text/plain', { parseAs: 'buffer' }, function (req, body, done) {281 t.ok(body instanceof Buffer)282 t.equal(body.length, 0)283 done(null, body)284 })285 286 fastify.listen({ port: 0 }, err => {287 t.error(err)288 289 sget({290 method: 'POST',291 url: getServerUrl(fastify),292 body: '',293 headers: {294 'Content-Type': 'text/plain'295 }296 }, (err, response, body) => {297 t.error(err)298 t.equal(response.statusCode, 200)299 t.equal(body.length, 0)300 fastify.close()301 })302 })303})304 305test('The charset should not interfere with the content type handling', t => {306 t.plan(5)307 const fastify = Fastify()308 309 fastify.post('/', (req, reply) => {310 reply.send(req.body)311 })312 313 fastify.addContentTypeParser('application/json', function (req, payload, done) {314 t.ok('called')315 jsonParser(payload, function (err, body) {316 done(err, body)317 })318 })319 320 fastify.listen({ port: 0 }, err => {321 t.error(err)322 323 sget({324 method: 'POST',325 url: getServerUrl(fastify),326 body: '{"hello":"world"}',327 headers: {328 'Content-Type': 'application/json; charset=utf-8'329 }330 }, (err, response, body) => {331 t.error(err)332 t.equal(response.statusCode, 200)333 t.equal(body.toString(), '{"hello":"world"}')334 fastify.close()335 })336 })337})338 