Skip to content

Commit f1bc0c3

Browse files
authored
chore: removed simple-get from body-limit tests (#6209)
1 parent 1b46ddf commit f1bc0c3

File tree

1 file changed

+41
-65
lines changed

1 file changed

+41
-65
lines changed

test/body-limit.test.js

Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
'use strict'
22

33
const Fastify = require('../fastify')
4-
const sget = require('simple-get').concat
54
const zlib = require('node:zlib')
65
const { test } = require('node:test')
76

8-
test('bodyLimit', (t, done) => {
9-
t.plan(5)
7+
test('bodyLimit', async t => {
8+
t.plan(4)
109

1110
try {
1211
Fastify({ bodyLimit: 1.3 })
@@ -28,22 +27,17 @@ test('bodyLimit', (t, done) => {
2827
reply.send({ error: 'handler should not be called' })
2928
})
3029

31-
fastify.listen({ port: 0 }, function (err) {
32-
t.assert.ifError(err)
33-
t.after(() => { fastify.close() })
30+
const fastifyServer = await fastify.listen({ port: 0 })
31+
t.after(() => { fastify.close() })
3432

35-
sget({
36-
method: 'POST',
37-
url: 'http://localhost:' + fastify.server.address().port,
38-
headers: { 'Content-Type': 'application/json' },
39-
body: [],
40-
json: true
41-
}, (err, response, body) => {
42-
t.assert.ifError(err)
43-
t.assert.strictEqual(response.statusCode, 413)
44-
done()
45-
})
33+
const result = await fetch(fastifyServer, {
34+
method: 'POST',
35+
headers: { 'Content-Type': 'application/json' },
36+
body: JSON.stringify([])
4637
})
38+
39+
t.assert.ok(!result.ok)
40+
t.assert.strictEqual(result.status, 413)
4741
})
4842

4943
test('bodyLimit is applied to decoded content', async (t) => {
@@ -114,35 +108,29 @@ test('bodyLimit is applied to decoded content', async (t) => {
114108
})
115109
})
116110

117-
test('default request.routeOptions.bodyLimit should be 1048576', (t, done) => {
118-
t.plan(4)
111+
test('default request.routeOptions.bodyLimit should be 1048576', async t => {
112+
t.plan(3)
119113
const fastify = Fastify()
120114
fastify.post('/default-bodylimit', {
121115
handler (request, reply) {
122116
t.assert.strictEqual(1048576, request.routeOptions.bodyLimit)
123117
reply.send({ })
124118
}
125119
})
126-
fastify.listen({ port: 0 }, function (err) {
127-
t.assert.ifError(err)
128-
t.after(() => { fastify.close() })
120+
const fastifyServer = await fastify.listen({ port: 0 })
121+
t.after(() => { fastify.close() })
129122

130-
sget({
131-
method: 'POST',
132-
url: 'http://localhost:' + fastify.server.address().port + '/default-bodylimit',
133-
headers: { 'Content-Type': 'application/json' },
134-
body: [],
135-
json: true
136-
}, (err, response, body) => {
137-
t.assert.ifError(err)
138-
t.assert.strictEqual(response.statusCode, 200)
139-
done()
140-
})
123+
const result = await fetch(fastifyServer + '/default-bodylimit', {
124+
method: 'POST',
125+
headers: { 'Content-Type': 'application/json' },
126+
body: JSON.stringify([])
141127
})
128+
t.assert.ok(result.ok)
129+
t.assert.strictEqual(result.status, 200)
142130
})
143131

144-
test('request.routeOptions.bodyLimit should be equal to route limit', (t, done) => {
145-
t.plan(4)
132+
test('request.routeOptions.bodyLimit should be equal to route limit', async t => {
133+
t.plan(3)
146134
const fastify = Fastify({ bodyLimit: 1 })
147135
fastify.post('/route-limit', {
148136
bodyLimit: 1000,
@@ -151,47 +139,35 @@ test('request.routeOptions.bodyLimit should be equal to route limit', (t, done)
151139
reply.send({})
152140
}
153141
})
154-
fastify.listen({ port: 0 }, function (err) {
155-
t.assert.ifError(err)
156-
t.after(() => { fastify.close() })
142+
const fastifyServer = await fastify.listen({ port: 0 })
143+
t.after(() => { fastify.close() })
157144

158-
sget({
159-
method: 'POST',
160-
url: 'http://localhost:' + fastify.server.address().port + '/route-limit',
161-
headers: { 'Content-Type': 'application/json' },
162-
body: [],
163-
json: true
164-
}, (err, response, body) => {
165-
t.assert.ifError(err)
166-
t.assert.strictEqual(response.statusCode, 200)
167-
done()
168-
})
145+
const result = await fetch(fastifyServer + '/route-limit', {
146+
method: 'POST',
147+
headers: { 'Content-Type': 'application/json' },
148+
body: JSON.stringify([])
169149
})
150+
t.assert.ok(result.ok)
151+
t.assert.strictEqual(result.status, 200)
170152
})
171153

172-
test('request.routeOptions.bodyLimit should be equal to server limit', (t, done) => {
173-
t.plan(4)
154+
test('request.routeOptions.bodyLimit should be equal to server limit', async t => {
155+
t.plan(3)
174156
const fastify = Fastify({ bodyLimit: 100 })
175157
fastify.post('/server-limit', {
176158
handler (request, reply) {
177159
t.assert.strictEqual(100, request.routeOptions.bodyLimit)
178160
reply.send({})
179161
}
180162
})
181-
fastify.listen({ port: 0 }, function (err) {
182-
t.assert.ifError(err)
183-
t.after(() => { fastify.close() })
163+
const fastifyServer = await fastify.listen({ port: 0 })
164+
t.after(() => { fastify.close() })
184165

185-
sget({
186-
method: 'POST',
187-
url: 'http://localhost:' + fastify.server.address().port + '/server-limit',
188-
headers: { 'Content-Type': 'application/json' },
189-
body: [],
190-
json: true
191-
}, (err, response, body) => {
192-
t.assert.ifError(err)
193-
t.assert.strictEqual(response.statusCode, 200)
194-
done()
195-
})
166+
const result = await fetch(fastifyServer + '/server-limit', {
167+
method: 'POST',
168+
headers: { 'Content-Type': 'application/json' },
169+
body: JSON.stringify([])
196170
})
171+
t.assert.ok(result.ok)
172+
t.assert.strictEqual(result.status, 200)
197173
})

0 commit comments

Comments
 (0)