-
-
Notifications
You must be signed in to change notification settings - Fork 105
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
Feature/increase test coverage #204
Changes from all commits
6d01024
f448f67
3b1e8e8
634428a
d2a1c39
d1d2a1e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ const { Readable } = require('readable-stream') | |
const path = require('path') | ||
const fs = require('fs') | ||
const { access } = require('fs').promises | ||
const rimraf = require('rimraf') | ||
const stream = require('stream') | ||
const EventEmitter = require('events') | ||
const { once } = EventEmitter | ||
|
@@ -184,6 +185,104 @@ test('should throw on file limit error', async function (t) { | |
} | ||
}) | ||
|
||
test('should throw on file save error', async function (t) { | ||
t.plan(2) | ||
|
||
const fastify = Fastify() | ||
t.tearDown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(require('..')) | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
try { | ||
await req.saveRequestFiles({ tmpdir: 'something' }) | ||
reply.code(200).send() | ||
} catch (error) { | ||
reply.code(500).send() | ||
} | ||
}) | ||
|
||
await fastify.listen(0) | ||
|
||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
const req = http.request(opts) | ||
const readStream = fs.createReadStream(filePath) | ||
form.append('upload', readStream) | ||
|
||
pump(form, req) | ||
|
||
try { | ||
const [res] = await once(req, 'response') | ||
t.equal(res.statusCode, 500) | ||
res.resume() | ||
await once(res, 'end') | ||
} catch (error) { | ||
t.error(error, 'request') | ||
} | ||
}) | ||
|
||
test('should not throw on request files cleanup error', { skip: process.platform === 'win32' }, async function (t) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same question for this test. it doesn't seem to be doing anything that wouldn't work on windows There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The reason for having this test skip the Windows platform is different. I think the test is flaky on Windows. It failed on our windows CI with node 12.x but it ran successfully on a Windows 10 laptop with node 12.x. (I can't prove that, you'll have to trust me :) ) In the test we are using rimraf which has a known issue that could be causing this: isaacs/rimraf#72 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Makes sense, thanks for clarifying. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can remove rimraf. recursive folder removal is now part of Node.js. See https://nodejs.org/api/fs.html#fs_fs_rmdirsync_path_options and others. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This module still supports Node 10 though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, recursive is not supported in Node 10. |
||
t.plan(2) | ||
|
||
const fastify = Fastify() | ||
t.tearDown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(require('..')) | ||
|
||
const tmpdir = t.testdir() | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
try { | ||
await req.saveRequestFiles({ tmpdir }) | ||
// temp file saved, remove before the onResponse hook | ||
rimraf.sync(tmpdir) | ||
reply.code(200).send() | ||
} catch (error) { | ||
reply.code(500).send() | ||
} | ||
}) | ||
|
||
await fastify.listen(0) | ||
|
||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: 'localhost', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
const req = http.request(opts) | ||
const readStream = fs.createReadStream(filePath) | ||
form.append('upload', readStream) | ||
|
||
pump(form, req) | ||
|
||
try { | ||
const [res] = await once(req, 'response') | ||
t.equal(res.statusCode, 200) | ||
res.resume() | ||
await once(res, 'end') | ||
} catch (error) { | ||
t.error(error, 'request') | ||
} | ||
}) | ||
|
||
test('should throw on file limit error, after highWaterMark', async function (t) { | ||
t.plan(5) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know why these two tests wouldn't work on windows? Also, is there a reason why you're adding tests to this legacy folder? The name suggests these are old tests, I'm assuming new tests should be written outside of this folder perhaps.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assume it is connected to already existing issue where tests are failing on windows for the legacy api #110.
As per readme the legacy api is not compatible with windows.
The legacy use cases also take part in the test coverage computation. I assume the folder is named legacy to group the tests for the legacy callback api. In this test we are covering the error that happens in the function
handleLegacyMultipartApi
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Understood, I didn't know there was a legacy API, this explains it.