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