Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if route exist before checking Content-Type of body #4286

Merged
merged 5 commits into from Sep 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/contentTypeParser.js
Expand Up @@ -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,
Expand Down
20 changes: 19 additions & 1 deletion test/404s.test.js
Expand Up @@ -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) {
Expand All @@ -18,7 +19,7 @@ function getUrl (app) {
}

test('default 404', t => {
t.plan(4)
t.plan(5)

const test = t.test
const fastify = Fastify()
Expand Down Expand Up @@ -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')
})
})
})
})

Expand Down