Skip to content

Commit

Permalink
Always include form value's content-type (#391)
Browse files Browse the repository at this point in the history
* always include form value's content-type

* keep existing behavior for application/json
  • Loading branch information
dwickern committed Sep 16, 2022
1 parent b445523 commit c486ecf
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 6 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Expand Up @@ -45,7 +45,7 @@ export interface SavedMultipartFile extends MultipartFile {
export interface MultipartValue<T = unknown> {
value: T;
fieldname: string;
mimetype?: string;
mimetype: string;
encoding: string;
fieldnameTruncated: boolean;
valueTruncated: boolean;
Expand Down
6 changes: 2 additions & 4 deletions index.js
Expand Up @@ -372,8 +372,6 @@ function fastifyMultipart (fastify, options, done) {
request.pipe(bb)

function onField (name, fieldValue, fieldnameTruncated, valueTruncated, encoding, contentType) {
let mimetype

// don't overwrite prototypes
if (getDescriptor(Object.prototype, name)) {
onError(new PrototypeViolationError())
Expand All @@ -390,7 +388,7 @@ function fastifyMultipart (fastify, options, done) {

try {
fieldValue = secureJSON.parse(fieldValue)
mimetype = 'application/json'
contentType = 'application/json'
} catch (e) {
onError(new InvalidJSONFieldError())
return
Expand All @@ -399,7 +397,7 @@ function fastifyMultipart (fastify, options, done) {

const value = {
fieldname: name,
mimetype,
mimetype: contentType,
encoding,
value: fieldValue,
fieldnameTruncated,
Expand Down
46 changes: 45 additions & 1 deletion test/multipart-json.test.js
Expand Up @@ -60,7 +60,7 @@ test('should not parse JSON fields forms if no content-type is set', function (t
fastify.post('/', async function (req, reply) {
for await (const part of req.parts()) {
t.notOk(part.filename)
t.notOk(part.mimetype)
t.equal(part.mimetype, 'text/plain')
t.type(part.value, 'string')
}

Expand Down Expand Up @@ -93,6 +93,50 @@ test('should not parse JSON fields forms if no content-type is set', function (t
})
})

test('should not parse JSON fields forms if non-json content-type is set', function (t) {
t.plan(5)

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

fastify.register(multipart)

fastify.post('/', async function (req, reply) {
for await (const part of req.parts()) {
t.notOk(part.filename)
t.equal(part.mimetype, 'text/css')
t.type(part.value, 'string')
}

reply.code(200).send()
})

fastify.listen({ port: 0 }, async function () {
// 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, res => {
t.equal(res.statusCode, 200)
res.resume()
res.on('end', () => {
t.pass('res ended successfully')
})
})

form.append('css', 'body { width: 100% }', { contentType: 'text/css' })

form.pipe(req)
})
})

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

Expand Down

0 comments on commit c486ecf

Please sign in to comment.