Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky test on windows #415

Merged
merged 6 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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