Skip to content

Commit

Permalink
fix: content-type parserRegExpList when plugin override (#4496) (#4498)
Browse files Browse the repository at this point in the history
(cherry picked from commit 687459a)

Co-authored-by: KaKa <kaka@kakawebsitedemo.com>
  • Loading branch information
github-actions[bot] and climba03003 committed Jan 6, 2023
1 parent 6453645 commit c172cae
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/contentTypeParser.js
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,7 @@ function buildContentTypeParser (c) {
contentTypeParser[kDefaultJsonParse] = c[kDefaultJsonParse]
contentTypeParser.customParsers = new Map(c.customParsers.entries())
contentTypeParser.parserList = c.parserList.slice()
contentTypeParser.parserRegExpList = c.parserRegExpList.slice()
return contentTypeParser
}

Expand Down
43 changes: 43 additions & 0 deletions test/content-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -542,3 +542,46 @@ test('content-type fail when parameters not match - regexp', async t => {

t.same(response.statusCode, 415)
})

// Refs: https://github.com/fastify/fastify/issues/4495
test('content-type regexp list should be cloned when plugin override', async t => {
t.plan(6)

const fastify = Fastify()

fastify.addContentTypeParser(/^image\/.*/, { parseAs: 'buffer' }, (req, payload, done) => {
done(null, payload)
})

fastify.register(function plugin (fastify, options, done) {
fastify.post('/', function (request, reply) {
reply.type(request.headers['content-type']).send(request.body)
})

done()
})

{
const { payload, headers, statusCode } = await fastify.inject({
method: 'POST',
path: '/',
payload: 'jpeg',
headers: { 'content-type': 'image/jpeg' }
})
t.same(statusCode, 200)
t.same(headers['content-type'], 'image/jpeg')
t.same(payload, 'jpeg')
}

{
const { payload, headers, statusCode } = await fastify.inject({
method: 'POST',
path: '/',
payload: 'png',
headers: { 'content-type': 'image/png' }
})
t.same(statusCode, 200)
t.same(headers['content-type'], 'image/png')
t.same(payload, 'png')
}
})

0 comments on commit c172cae

Please sign in to comment.