strong-tie/inbound-calls
0
1'use strict'2 3const { test } = require('node:test')4const Fastify = require('..')5 6test('default 413 with bodyLimit option', async (t) => {7 t.plan(3)8 9 const fastify = Fastify({10 bodyLimit: 1011 })12 13 fastify.post('/', function (req, reply) {14 reply.send({ hello: 'world' })15 })16 17 const response = await fastify.inject({18 method: 'POST',19 url: '/',20 body: {21 text: '12345678901234567890123456789012345678901234567890'22 }23 })24 t.assert.strictEqual(response.statusCode, 413)25 t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')26 t.assert.deepStrictEqual(JSON.parse(response.payload), {27 error: 'Payload Too Large',28 code: 'FST_ERR_CTP_BODY_TOO_LARGE',29 message: 'Request body is too large',30 statusCode: 41331 })32})33 34test('default 400 with wrong content-length', async (t) => {35 t.plan(3)36 37 const fastify = Fastify()38 39 fastify.post('/', function (req, reply) {40 reply.send({ hello: 'world' })41 })42 43 const response = await fastify.inject({44 method: 'POST',45 url: '/',46 headers: {47 'content-length': 2048 },49 body: {50 text: '12345678901234567890123456789012345678901234567890'51 }52 })53 t.assert.strictEqual(response.statusCode, 400)54 t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')55 t.assert.deepStrictEqual(JSON.parse(response.payload), {56 error: 'Bad Request',57 code: 'FST_ERR_CTP_INVALID_CONTENT_LENGTH',58 message: 'Request body size did not match Content-Length',59 statusCode: 40060 })61})62 63test('custom 413 with bodyLimit option', async (t) => {64 t.plan(3)65 66 const fastify = Fastify({67 bodyLimit: 1068 })69 70 fastify.post('/', function (req, reply) {71 reply.send({ hello: 'world' })72 })73 74 fastify.setErrorHandler(function (err, request, reply) {75 reply76 .code(err.statusCode)77 .type('application/json; charset=utf-8')78 .send(err)79 })80 81 const response = await fastify.inject({82 method: 'POST',83 url: '/',84 body: {85 text: '12345678901234567890123456789012345678901234567890'86 }87 })88 t.assert.strictEqual(response.statusCode, 413)89 t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')90 t.assert.deepStrictEqual(JSON.parse(response.payload), {91 error: 'Payload Too Large',92 code: 'FST_ERR_CTP_BODY_TOO_LARGE',93 message: 'Request body is too large',94 statusCode: 41395 })96})97 98test('custom 400 with wrong content-length', async (t) => {99 t.plan(3)100 101 const fastify = Fastify()102 103 fastify.post('/', function (req, reply) {104 reply.send({ hello: 'world' })105 })106 107 fastify.setErrorHandler(function (err, request, reply) {108 reply109 .code(err.statusCode)110 .type('application/json; charset=utf-8')111 .send(err)112 })113 114 const response = await fastify.inject({115 method: 'POST',116 url: '/',117 headers: {118 'content-length': 20119 },120 body: {121 text: '12345678901234567890123456789012345678901234567890'122 }123 })124 t.assert.strictEqual(response.statusCode, 400)125 t.assert.strictEqual(response.headers['content-type'], 'application/json; charset=utf-8')126 t.assert.deepStrictEqual(JSON.parse(response.payload), {127 error: 'Bad Request',128 code: 'FST_ERR_CTP_INVALID_CONTENT_LENGTH',129 message: 'Request body size did not match Content-Length',130 statusCode: 400131 })132})133 134test('#2214 - wrong content-length', async (t) => {135 const fastify = Fastify()136 137 fastify.get('/', async () => {138 const error = new Error('MY_ERROR_MESSAGE')139 error.headers = {140 'content-length': 2141 }142 throw error143 })144 145 const response = await fastify.inject({146 method: 'GET',147 path: '/'148 })149 t.assert.strictEqual(response.headers['content-length'], '' + response.rawPayload.length)150})151 152test('#2543 - wrong content-length with errorHandler', async (t) => {153 const fastify = Fastify()154 155 fastify.setErrorHandler((_error, _request, reply) => {156 reply.code(500).send({ message: 'longer than 2 bytes' })157 })158 159 fastify.get('/', async () => {160 const error = new Error('MY_ERROR_MESSAGE')161 error.headers = {162 'content-length': 2163 }164 throw error165 })166 167 const response = await fastify.inject({168 method: 'GET',169 path: '/'170 })171 t.assert.strictEqual(response.statusCode, 500)172 t.assert.strictEqual(response.headers['content-length'], '' + response.rawPayload.length)173 t.assert.deepStrictEqual(JSON.parse(response.payload), { message: 'longer than 2 bytes' })174})175 