UNPKG

3.44 kBJavaScriptView Raw
1'use strict'
2
3const t = require('tap')
4const test = t.test
5const Fastify = require('..')
6
7test('default 413 with bodyLimit option', t => {
8 t.plan(4)
9
10 const fastify = Fastify({
11 bodyLimit: 10
12 })
13
14 fastify.post('/', function (req, reply) {
15 reply.send({ hello: 'world' })
16 })
17
18 fastify.inject({
19 method: 'POST',
20 url: '/',
21 body: {
22 text: '12345678901234567890123456789012345678901234567890'
23 }
24 }, (err, res) => {
25 t.error(err)
26 t.strictEqual(res.statusCode, 413)
27 t.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
28 t.deepEqual(JSON.parse(res.payload), {
29 error: 'Payload Too Large',
30 code: 'FST_ERR_CTP_BODY_TOO_LARGE',
31 message: 'FST_ERR_CTP_BODY_TOO_LARGE: Request body is too large',
32 statusCode: 413
33 })
34 })
35})
36
37test('default 400 with wrong content-length', t => {
38 t.plan(4)
39
40 const fastify = Fastify()
41
42 fastify.post('/', function (req, reply) {
43 reply.send({ hello: 'world' })
44 })
45
46 fastify.inject({
47 method: 'POST',
48 url: '/',
49 headers: {
50 'content-length': 20
51 },
52 body: {
53 text: '12345678901234567890123456789012345678901234567890'
54 }
55 }, (err, res) => {
56 t.error(err)
57 t.strictEqual(res.statusCode, 400)
58 t.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
59 t.deepEqual(JSON.parse(res.payload), {
60 error: 'Bad Request',
61 code: 'FST_ERR_CTP_INVALID_CONTENT_LENGTH',
62 message: 'FST_ERR_CTP_INVALID_CONTENT_LENGTH: Request body size did not match Content-Length',
63 statusCode: 400
64 })
65 })
66})
67
68test('custom 413 with bodyLimit option', t => {
69 t.plan(4)
70
71 const fastify = Fastify({
72 bodyLimit: 10
73 })
74
75 fastify.post('/', function (req, reply) {
76 reply.send({ hello: 'world' })
77 })
78
79 fastify.setErrorHandler(function (err, request, reply) {
80 reply
81 .code(err.statusCode)
82 .type('application/json; charset=utf-8')
83 .send(err)
84 })
85
86 fastify.inject({
87 method: 'POST',
88 url: '/',
89 body: {
90 text: '12345678901234567890123456789012345678901234567890'
91 }
92 }, (err, res) => {
93 t.error(err)
94 t.strictEqual(res.statusCode, 413)
95 t.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
96 t.deepEqual(JSON.parse(res.payload), {
97 error: 'Payload Too Large',
98 code: 'FST_ERR_CTP_BODY_TOO_LARGE',
99 message: 'FST_ERR_CTP_BODY_TOO_LARGE: Request body is too large',
100 statusCode: 413
101 })
102 })
103})
104
105test('custom 400 with wrong content-length', t => {
106 t.plan(4)
107
108 const fastify = Fastify()
109
110 fastify.post('/', function (req, reply) {
111 reply.send({ hello: 'world' })
112 })
113
114 fastify.setErrorHandler(function (err, request, reply) {
115 reply
116 .code(err.statusCode)
117 .type('application/json; charset=utf-8')
118 .send(err)
119 })
120
121 fastify.inject({
122 method: 'POST',
123 url: '/',
124 headers: {
125 'content-length': 20
126 },
127 body: {
128 text: '12345678901234567890123456789012345678901234567890'
129 }
130 }, (err, res) => {
131 t.error(err)
132 t.strictEqual(res.statusCode, 400)
133 t.strictEqual(res.headers['content-type'], 'application/json; charset=utf-8')
134 t.deepEqual(JSON.parse(res.payload), {
135 error: 'Bad Request',
136 code: 'FST_ERR_CTP_INVALID_CONTENT_LENGTH',
137 message: 'FST_ERR_CTP_INVALID_CONTENT_LENGTH: Request body size did not match Content-Length',
138 statusCode: 400
139 })
140 })
141})