Skip to content

Commit c486ecf

Browse files
authored
Always include form value's content-type (#391)
* always include form value's content-type * keep existing behavior for application/json
1 parent b445523 commit c486ecf

File tree

3 files changed

+48
-6
lines changed

3 files changed

+48
-6
lines changed

index.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export interface SavedMultipartFile extends MultipartFile {
4545
export interface MultipartValue<T = unknown> {
4646
value: T;
4747
fieldname: string;
48-
mimetype?: string;
48+
mimetype: string;
4949
encoding: string;
5050
fieldnameTruncated: boolean;
5151
valueTruncated: boolean;

index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,6 @@ function fastifyMultipart (fastify, options, done) {
372372
request.pipe(bb)
373373

374374
function onField (name, fieldValue, fieldnameTruncated, valueTruncated, encoding, contentType) {
375-
let mimetype
376-
377375
// don't overwrite prototypes
378376
if (getDescriptor(Object.prototype, name)) {
379377
onError(new PrototypeViolationError())
@@ -390,7 +388,7 @@ function fastifyMultipart (fastify, options, done) {
390388

391389
try {
392390
fieldValue = secureJSON.parse(fieldValue)
393-
mimetype = 'application/json'
391+
contentType = 'application/json'
394392
} catch (e) {
395393
onError(new InvalidJSONFieldError())
396394
return
@@ -399,7 +397,7 @@ function fastifyMultipart (fastify, options, done) {
399397

400398
const value = {
401399
fieldname: name,
402-
mimetype,
400+
mimetype: contentType,
403401
encoding,
404402
value: fieldValue,
405403
fieldnameTruncated,

test/multipart-json.test.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ test('should not parse JSON fields forms if no content-type is set', function (t
6060
fastify.post('/', async function (req, reply) {
6161
for await (const part of req.parts()) {
6262
t.notOk(part.filename)
63-
t.notOk(part.mimetype)
63+
t.equal(part.mimetype, 'text/plain')
6464
t.type(part.value, 'string')
6565
}
6666

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

96+
test('should not parse JSON fields forms if non-json content-type is set', function (t) {
97+
t.plan(5)
98+
99+
const fastify = Fastify()
100+
t.teardown(fastify.close.bind(fastify))
101+
102+
fastify.register(multipart)
103+
104+
fastify.post('/', async function (req, reply) {
105+
for await (const part of req.parts()) {
106+
t.notOk(part.filename)
107+
t.equal(part.mimetype, 'text/css')
108+
t.type(part.value, 'string')
109+
}
110+
111+
reply.code(200).send()
112+
})
113+
114+
fastify.listen({ port: 0 }, async function () {
115+
// request
116+
const form = new FormData()
117+
const opts = {
118+
protocol: 'http:',
119+
hostname: 'localhost',
120+
port: fastify.server.address().port,
121+
path: '/',
122+
headers: form.getHeaders(),
123+
method: 'POST'
124+
}
125+
126+
const req = http.request(opts, res => {
127+
t.equal(res.statusCode, 200)
128+
res.resume()
129+
res.on('end', () => {
130+
t.pass('res ended successfully')
131+
})
132+
})
133+
134+
form.append('css', 'body { width: 100% }', { contentType: 'text/css' })
135+
136+
form.pipe(req)
137+
})
138+
})
139+
96140
test('should throw error when parsing JSON fields failed', function (t) {
97141
t.plan(2)
98142

0 commit comments

Comments
 (0)