From 7e349abb81aac68f9a758bda969f29086f363560 Mon Sep 17 00:00:00 2001 From: MageshBabu K Date: Sat, 17 Sep 2022 16:01:59 +0530 Subject: [PATCH 1/5] check if route exist before checking content type --- lib/contentTypeParser.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/contentTypeParser.js b/lib/contentTypeParser.js index 48e293390b..ca160e03d4 100644 --- a/lib/contentTypeParser.js +++ b/lib/contentTypeParser.js @@ -145,6 +145,9 @@ ContentTypeParser.prototype.remove = function (contentType) { } ContentTypeParser.prototype.run = function (contentType, handler, request, reply) { + if (request.is404) { + return handler(request, reply) + } const parser = this.getParser(contentType) const resource = new AsyncResource('content-type-parser:run', request) From 3151fe5b5069921a3fb84543f1d7ad7140a8b1d4 Mon Sep 17 00:00:00 2001 From: MageshBabu K Date: Sat, 17 Sep 2022 16:02:17 +0530 Subject: [PATCH 2/5] added test for route check before content type --- test/404s.test.js | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/404s.test.js b/test/404s.test.js index 0c945c6806..bae455b2a3 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) { @@ -74,6 +75,23 @@ test('default 404', t => { t.equal(response.headers['content-type'], 'application/json; charset=utf-8') }) }) + + test('not registered route with unsupported content type', t => { + t.plan(3) + const form = FormData() + form.append('test-field', 'just some field') + sget({ + method: 'POST', + url: getUrl(fastify) + '/nonSupportedRoute', + body: form, + json: false + }, (err, response, body) => { + t.error(err) + console.log('from test, status', response.statusCode) + t.equal(response.statusCode, 404) + t.equal(response.headers['content-type'], 'application/json; charset=utf-8') + }) + }) }) }) From 00e0f66f5ba0844cf6e885200902ec838eed3224 Mon Sep 17 00:00:00 2001 From: MageshBabu K Date: Sat, 17 Sep 2022 19:11:03 +0530 Subject: [PATCH 3/5] 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 --- lib/contentTypeParser.js | 9 +++++---- test/404s.test.js | 8 ++++---- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/contentTypeParser.js b/lib/contentTypeParser.js index ca160e03d4..f4bbf730ed 100644 --- a/lib/contentTypeParser.js +++ b/lib/contentTypeParser.js @@ -145,14 +145,15 @@ ContentTypeParser.prototype.remove = function (contentType) { } ContentTypeParser.prototype.run = function (contentType, handler, request, reply) { - if (request.is404) { - return handler(request, reply) - } const parser = this.getParser(contentType) 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) { + return 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 bae455b2a3..94709f6bec 100644 --- a/test/404s.test.js +++ b/test/404s.test.js @@ -19,7 +19,7 @@ function getUrl (app) { } test('default 404', t => { - t.plan(4) + t.plan(5) const test = t.test const fastify = Fastify() @@ -76,18 +76,18 @@ test('default 404', t => { }) }) - test('not registered route with unsupported content type', t => { + 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) + '/nonSupportedRoute', + url: getUrl(fastify) + '/notSupported', body: form, json: false }, (err, response, body) => { t.error(err) - console.log('from test, status', response.statusCode) t.equal(response.statusCode, 404) t.equal(response.headers['content-type'], 'application/json; charset=utf-8') }) From 7b9bd3b37011582ee22e307c683d097dbe3a58ab Mon Sep 17 00:00:00 2001 From: Magesh Babu Date: Sun, 18 Sep 2022 00:58:53 +0530 Subject: [PATCH 4/5] Update lib/contentTypeParser.js Noted, thanks for the review Co-authored-by: Manuel Spigolon --- lib/contentTypeParser.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/contentTypeParser.js b/lib/contentTypeParser.js index f4bbf730ed..89f0f2b452 100644 --- a/lib/contentTypeParser.js +++ b/lib/contentTypeParser.js @@ -150,7 +150,8 @@ ContentTypeParser.prototype.run = function (contentType, handler, request, reply if (parser === undefined) { if (request.is404) { - return handler(request, reply) + handler(request, reply) + return } else { reply.send(new FST_ERR_CTP_INVALID_MEDIA_TYPE(contentType || undefined)) } From 7ec52a1d2be7faad446cc16fe737132ac447b627 Mon Sep 17 00:00:00 2001 From: MageshBabu K Date: Sun, 18 Sep 2022 10:05:24 +0530 Subject: [PATCH 5/5] pass tests from wrong return --- lib/contentTypeParser.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/contentTypeParser.js b/lib/contentTypeParser.js index 89f0f2b452..286aa84b41 100644 --- a/lib/contentTypeParser.js +++ b/lib/contentTypeParser.js @@ -150,8 +150,7 @@ ContentTypeParser.prototype.run = function (contentType, handler, request, reply if (parser === undefined) { if (request.is404) { - handler(request, reply) - return + handler(request, reply) } else { reply.send(new FST_ERR_CTP_INVALID_MEDIA_TYPE(contentType || undefined)) }