-
-
Notifications
You must be signed in to change notification settings - Fork 103
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
3 changed files
with
246 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,221 @@ | ||
'use strict' | ||
|
||
const test = require('tap').test | ||
const FormData = require('form-data') | ||
const Fastify = require('fastify') | ||
const multipart = require('..') | ||
const http = require('node:http') | ||
const EventEmitter = require('node:events') | ||
const { once } = EventEmitter | ||
|
||
test('Should throw RequestFileTooLargeError when throwFileSizeLimit: true for file())', async function (t) { | ||
t.plan(3) | ||
|
||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(multipart) | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
try { | ||
const file = await req.file({ limits: { fileSize: 1 }, throwFileSizeLimit: true }) | ||
await file.toBuffer() | ||
t.fail('should throw') | ||
reply.code(200).send() | ||
} catch (error) { | ||
t.ok(error instanceof fastify.multipartErrors.RequestFileTooLargeError) | ||
reply.code(500).send() | ||
} | ||
}) | ||
|
||
await fastify.listen({ port: 0 }) | ||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: '127.0.0.1', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
|
||
const randomFileBuffer = Buffer.alloc(1 * 1024 * 1024) | ||
|
||
const req = http.request(opts) | ||
|
||
form.append('upload', randomFileBuffer) | ||
|
||
form.pipe(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 RequestFileTooLargeError when throwFileSizeLimit: false for file())', async function (t) { | ||
t.plan(3) | ||
|
||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(multipart) | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
try { | ||
const file = await req.file({ limits: { fileSize: 1 }, throwFileSizeLimit: false }) | ||
await file.toBuffer() | ||
t.pass('OK') | ||
reply.code(200).send() | ||
} catch (error) { | ||
t.fail('Should not throw') | ||
reply.code(500).send() | ||
} | ||
}) | ||
|
||
await fastify.listen({ port: 0 }) | ||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: '127.0.0.1', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
|
||
const randomFileBuffer = Buffer.alloc(1 * 1024 * 1024) | ||
|
||
const req = http.request(opts) | ||
|
||
form.append('upload', randomFileBuffer) | ||
|
||
form.pipe(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 RequestFileTooLargeError when throwFileSizeLimit: true for files())', async function (t) { | ||
t.plan(3) | ||
|
||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(multipart) | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
try { | ||
const files = req.files({ limits: { fileSize: 1 }, throwFileSizeLimit: true }) | ||
for await (const file of files) { | ||
await file.toBuffer() | ||
} | ||
t.fail('Should throw') | ||
reply.code(200).send() | ||
} catch (error) { | ||
t.ok(error instanceof fastify.multipartErrors.RequestFileTooLargeError) | ||
reply.code(500).send() | ||
} | ||
}) | ||
|
||
await fastify.listen({ port: 0 }) | ||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: '127.0.0.1', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
|
||
const randomFileBuffer = Buffer.alloc(1 * 1024 * 1024) | ||
|
||
const req = http.request(opts) | ||
|
||
form.append('upload', randomFileBuffer) | ||
|
||
form.pipe(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 RequestFileTooLargeError when throwFileSizeLimit: false for files())', async function (t) { | ||
t.plan(3) | ||
|
||
const fastify = Fastify() | ||
t.teardown(fastify.close.bind(fastify)) | ||
|
||
fastify.register(multipart) | ||
|
||
fastify.post('/', async function (req, reply) { | ||
t.ok(req.isMultipart()) | ||
|
||
try { | ||
const files = req.files({ limits: { fileSize: 1 }, throwFileSizeLimit: false }) | ||
for await (const file of files) { | ||
await file.toBuffer() | ||
} | ||
t.pass('OK') | ||
reply.code(200).send() | ||
} catch (error) { | ||
t.fail('Should not throw') | ||
reply.code(500).send() | ||
} | ||
}) | ||
|
||
await fastify.listen({ port: 0 }) | ||
// request | ||
const form = new FormData() | ||
const opts = { | ||
protocol: 'http:', | ||
hostname: '127.0.0.1', | ||
port: fastify.server.address().port, | ||
path: '/', | ||
headers: form.getHeaders(), | ||
method: 'POST' | ||
} | ||
|
||
const randomFileBuffer = Buffer.alloc(1 * 1024 * 1024) | ||
|
||
const req = http.request(opts) | ||
|
||
form.append('upload', randomFileBuffer) | ||
|
||
form.pipe(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') | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters