UNPKG

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