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

fix: add content type parsers after removeAllContentTypeParsers #3292

Merged
merged 1 commit into from Sep 5, 2021
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
8 changes: 3 additions & 5 deletions lib/contentTypeParser.js
Expand Up @@ -67,9 +67,7 @@ ContentTypeParser.prototype.add = function (contentType, opts, parserFn) {
this.customParsers[''] = parser
} else {
if (contentTypeIsString) {
if (contentType !== 'application/json' && contentType !== 'text/plain') {
this.parserList.unshift(contentType)
}
this.parserList.unshift(contentType)
} else {
this.parserRegExpList.unshift(contentType)
}
Expand All @@ -83,10 +81,10 @@ ContentTypeParser.prototype.hasParser = function (contentType) {

ContentTypeParser.prototype.existingParser = function (contentType) {
if (contentType === 'application/json') {
return this.customParsers['application/json'].fn !== this[kDefaultJsonParse]
return this.customParsers['application/json'] && this.customParsers['application/json'].fn !== this[kDefaultJsonParse]
}
if (contentType === 'text/plain') {
return this.customParsers['text/plain'].fn !== defaultPlainTextParser
return this.customParsers['text/plain'] && this.customParsers['text/plain'].fn !== defaultPlainTextParser
}

return contentType in this.customParsers
Expand Down
111 changes: 111 additions & 0 deletions test/custom-parser.test.js
Expand Up @@ -1707,3 +1707,114 @@ test('cannot remove content type parsers after binding', t => {
t.throws(() => fastify.removeContentTypeParser('application/json'))
})
})

test('should be able to override the default json parser after removeAllContentTypeParsers', t => {
t.plan(5)

const fastify = Fastify()

fastify.post('/', (req, reply) => {
reply.send(req.body)
})

fastify.removeAllContentTypeParsers()

fastify.addContentTypeParser('application/json', function (req, payload, done) {
t.ok('called')
jsonParser(payload, function (err, body) {
done(err, body)
})
})

fastify.listen(0, err => {
t.error(err)

sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port,
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/json'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
fastify.close()
})
})
})

test('should be able to override the default plain text parser after removeAllContentTypeParsers', t => {
t.plan(5)

const fastify = Fastify()

fastify.post('/', (req, reply) => {
reply.send(req.body)
})

fastify.removeAllContentTypeParsers()

fastify.addContentTypeParser('text/plain', function (req, payload, done) {
t.ok('called')
plainTextParser(payload, function (err, body) {
done(err, body)
})
})

fastify.listen(0, err => {
t.error(err)

sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port,
body: 'hello world',
headers: {
'Content-Type': 'text/plain'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.equal(body.toString(), 'hello world')
fastify.close()
})
})
})

test('should be able to add a custom content type parser after removeAllContentTypeParsers', t => {
t.plan(5)

const fastify = Fastify()

fastify.post('/', (req, reply) => {
reply.send(req.body)
})

fastify.removeAllContentTypeParsers()

fastify.addContentTypeParser('application/jsoff', function (req, payload, done) {
t.ok('called')
jsonParser(payload, function (err, body) {
done(err, body)
})
})

fastify.listen(0, err => {
t.error(err)

sget({
method: 'POST',
url: 'http://localhost:' + fastify.server.address().port,
body: '{"hello":"world"}',
headers: {
'Content-Type': 'application/jsoff'
}
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
t.same(body.toString(), JSON.stringify({ hello: 'world' }))
fastify.close()
})
})
})