Skip to content

Commit

Permalink
fix: check if route exist before checking Content-Type of body (#4286)
Browse files Browse the repository at this point in the history
* check if route exist before checking content type

* added test for route check before content type

* pass tests

- check if the route is 404 only if the content-parser is undefined
by this we can avoid breaking changes.
- this also passes previous tests

this resolves issue
- fastify/fastify-multipart#381

* Update lib/contentTypeParser.js

Noted, thanks for the review

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>

* pass tests from wrong return

Co-authored-by: Manuel Spigolon <behemoth89@gmail.com>
  • Loading branch information
mage1k99 and Eomm committed Sep 18, 2022
1 parent e9838a2 commit 4109b03
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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

0 comments on commit 4109b03

Please sign in to comment.