Skip to content
Open
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
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,9 @@ function fastifyMultipart (fastify, options, done) {
InvalidMultipartContentTypeError,
RequestFileTooLargeError,
FileBufferNotFoundError,
PrematureCloseError
PrematureCloseError,
InvalidJSONFieldError,
NoFormData
})

fastify.addContentTypeParser('multipart/form-data', setMultipart)
Expand Down
70 changes: 68 additions & 2 deletions test/multipart-json.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ test('should not parse JSON fields forms if non-json content-type is set', funct
})

test('should throw error when parsing JSON fields failed', function (t, done) {
t.plan(2)
t.plan(3)

const fastify = Fastify()
t.after(() => fastify.close())
Expand Down Expand Up @@ -188,7 +188,7 @@ test('should throw error when parsing JSON fields failed', function (t, done) {
})

test('should always reject JSON parsing if the value was truncated', function (t, done) {
t.plan(2)
t.plan(3)

const fastify = Fastify()
t.after(() => fastify.close())
Expand Down Expand Up @@ -409,3 +409,69 @@ test('should return 400 when the field validation fails', function (t, done) {
form.pipe(req)
})
})

test('multipartErrors should expose InvalidJSONFieldError', async function (t) {
t.plan(2)

const fastify = Fastify()
t.after(() => fastify.close())

fastify.register(multipart)
await fastify.ready()

t.assert.ok(fastify.multipartErrors.InvalidJSONFieldError, 'InvalidJSONFieldError is exposed on multipartErrors')
t.assert.strictEqual(typeof fastify.multipartErrors.InvalidJSONFieldError, 'function')
})

test('thrown InvalidJSONFieldError should be an instance of multipartErrors.InvalidJSONFieldError', function (t, done) {
t.plan(3)

const fastify = Fastify()
t.after(() => fastify.close())

fastify.register(multipart)

fastify.post('/', async function (req, reply) {
try {
for await (const part of req.parts()) {
t.assert.strictEqual(typeof part.value, 'string')
}
reply.code(200).send()
} catch (error) {
t.assert.ok(error instanceof fastify.multipartErrors.InvalidJSONFieldError)
t.assert.strictEqual(error.code, 'FST_INVALID_JSON_FIELD_ERROR')
reply.code(error.statusCode).send()
}
})

fastify.listen({ port: 0 }, function () {
const boundary = 'testboundary'
const body = [
'--' + boundary,
'Content-Disposition: form-data; name="jsonfield"',
'Content-Type: application/json',
'',
'not valid json',
'--' + boundary + '--',
''
].join('\r\n')

const opts = {
hostname: '127.0.0.1',
port: fastify.server.address().port,
path: '/',
method: 'POST',
headers: {
'content-type': 'multipart/form-data; boundary=' + boundary,
'content-length': Buffer.byteLength(body)
}
}

const req = http.request(opts, res => {
t.assert.strictEqual(res.statusCode, 406)
res.resume()
res.on('end', done)
})
req.end(body)
})
})
3 changes: 3 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ interface MultipartErrors {
PrototypeViolationError: FastifyErrorConstructor;
InvalidMultipartContentTypeError: FastifyErrorConstructor;
RequestFileTooLargeError: FastifyErrorConstructor;
FileBufferNotFoundError: FastifyErrorConstructor;
PrematureCloseError: FastifyErrorConstructor;
InvalidJSONFieldError: FastifyErrorConstructor;
NoFormData: FastifyErrorConstructor;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add these to the type tests?

}

declare namespace fastifyMultipart {
Expand Down
2 changes: 2 additions & 0 deletions types/index.tst.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,9 @@ const runServer = async () => {

expect(app.multipartErrors.FieldsLimitError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.FilesLimitError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.InvalidJSONFieldError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.InvalidMultipartContentTypeError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.NoFormData).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.PartsLimitError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.PrototypeViolationError).type.toBe<FastifyErrorConstructor>()
expect(app.multipartErrors.RequestFileTooLargeError).type.toBe<FastifyErrorConstructor>()
Expand Down
Loading