diff --git a/lib/contentTypeParser.js b/lib/contentTypeParser.js index 48e293390b..286aa84b41 100644 --- a/lib/contentTypeParser.js +++ b/lib/contentTypeParser.js @@ -149,7 +149,11 @@ ContentTypeParser.prototype.run = function (contentType, handler, request, reply const resource = new AsyncResource('content-type-parser:run', request) if (parser === undefined) { - reply.send(new FST_ERR_CTP_INVALID_MEDIA_TYPE(contentType || undefined)) + if (request.is404) { + handler(request, reply) + } else { + reply.send(new FST_ERR_CTP_INVALID_MEDIA_TYPE(contentType || undefined)) + } } else if (parser.asString === true || parser.asBuffer === true) { rawBody( request, diff --git a/test/404s.test.js b/test/404s.test.js index 0c945c6806..94709f6bec 100644 --- a/test/404s.test.js +++ b/test/404s.test.js @@ -6,6 +6,7 @@ const fp = require('fastify-plugin') const sget = require('simple-get').concat const errors = require('http-errors') const split = require('split2') +const FormData = require('form-data') const Fastify = require('..') function getUrl (app) { @@ -18,7 +19,7 @@ function getUrl (app) { } test('default 404', t => { - t.plan(4) + t.plan(5) const test = t.test const fastify = Fastify() @@ -74,6 +75,23 @@ test('default 404', t => { t.equal(response.headers['content-type'], 'application/json; charset=utf-8') }) }) + + test('using post method and multipart/formdata', t => { + t.plan(3) + const form = FormData() + form.append('test-field', 'just some field') + + sget({ + method: 'POST', + url: getUrl(fastify) + '/notSupported', + body: form, + json: false + }, (err, response, body) => { + t.error(err) + t.equal(response.statusCode, 404) + t.equal(response.headers['content-type'], 'application/json; charset=utf-8') + }) + }) }) })