Skip to content

Commit

Permalink
Fix flaky test on windows (#415)
Browse files Browse the repository at this point in the history
* Fix flaky test on windows

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

* fixup

Signed-off-by: Matteo Collina <[email protected]>

---------

Signed-off-by: Matteo Collina <[email protected]>
  • Loading branch information
mcollina authored Feb 14, 2023
1 parent 1749638 commit 1a0c683
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 16 deletions.
6 changes: 4 additions & 2 deletions test/multipart-security.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,20 @@ test('should use default for fileSize', async function (t) {
})

fastify.post('/', async function (req, reply) {
t.ok(req.isMultipart())
t.ok(req.isMultipart(), 'is multipart')

const part = await req.file()
t.pass('the file is not consumed yet')

try {
await part.toBuffer()
reply.send('not ok')
t.fail('it should throw')
} catch (error) {
t.ok(error)
reply.send(error)
}
return reply
})

await fastify.listen({ port: 0 })
Expand All @@ -151,7 +153,7 @@ test('should use default for fileSize', async function (t) {

try {
const [res] = await once(req, 'response')
t.equal(res.statusCode, 413)
t.equal(res.statusCode, 413, 'status code equal')
res.resume()
await once(res, 'end')
} catch (error) {
Expand Down
48 changes: 34 additions & 14 deletions test/multipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,6 @@ test('should error if boundary is empty', function (t) {
})

test('should throw error due to filesLimit (The max number of file fields (Default: Infinity))', function (t) {
t.plan(4)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))

Expand All @@ -269,12 +267,12 @@ test('should throw error due to filesLimit (The max number of file fields (Defau
try {
const parts = req.files({ limits: { files: 1 } })
for await (const part of parts) {
t.ok(part.file)
t.ok(part.file, 'part received')
await sendToWormhole(part.file)
}
reply.code(200).send()
} catch (error) {
t.ok(error instanceof fastify.multipartErrors.FilesLimitError)
t.ok(error instanceof fastify.multipartErrors.FilesLimitError, 'error')
reply.code(500).send()
}
})
Expand All @@ -291,22 +289,34 @@ test('should throw error due to filesLimit (The max number of file fields (Defau
method: 'POST'
}

let ended = false
const req = http.request(opts, (res) => {
t.equal(res.statusCode, 500)
t.equal(res.statusCode, 500, 'status code')
res.resume()
res.on('end', () => {
if (ended) {
return
}
ended = true
t.pass('res ended successfully')
t.end()
})
})
form.append('upload', fs.createReadStream(filePath))
form.append('upload2', fs.createReadStream(filePath))
form.pipe(req)
req.on('error', (err) => {
if (ended) {
return
}
ended = true
t.equal(err.code, 'ECONNRESET')
t.end()
})
})
})

test('should be able to configure limits globally with plugin register options', function (t) {
t.plan(4)

const fastify = Fastify()
t.teardown(fastify.close.bind(fastify))

Expand All @@ -326,7 +336,7 @@ test('should be able to configure limits globally with plugin register options',
}
})

fastify.listen({ port: 0 }, async function () {
fastify.listen({ port: 0 }, function () {
// request
const form = new FormData()
const opts = {
Expand All @@ -338,21 +348,31 @@ test('should be able to configure limits globally with plugin register options',
method: 'POST'
}

let ended = false
const req = http.request(opts, (res) => {
t.equal(res.statusCode, 500)
res.resume()
res.on('end', () => {
if (ended) {
return
}
ended = true
t.pass('res ended successfully')
t.end()
})
})
form.append('upload', fs.createReadStream(filePath))
form.append('upload2', fs.createReadStream(filePath))
req.on('error', (err) => {
if (ended) {
return
}
ended = true
t.equal(err.code, 'ECONNRESET')
t.end()
})

try {
await pump(form, req)
} catch (error) {
t.error(error, 'formData request pump: no err')
}
pump(form, req).catch(() => {})
})
})

Expand All @@ -376,7 +396,7 @@ test('should throw error due to fieldsLimit (Max number of non-file fields (Defa
}
})

fastify.listen({ port: 0 }, async function () {
fastify.listen({ port: 0 }, function () {
// request
const form = new FormData()
const opts = {
Expand Down

0 comments on commit 1a0c683

Please sign in to comment.