Skip to content

Commit 2179f83

Browse files
authored
ref(ctp): Return boolean indicating content type parser removal result (fastify#4550)
1 parent 19d05b5 commit 2179f83

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

lib/contentTypeParser.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ ContentTypeParser.prototype.removeAll = function () {
141141
ContentTypeParser.prototype.remove = function (contentType) {
142142
if (!(typeof contentType === 'string' || contentType instanceof RegExp)) throw new FST_ERR_CTP_INVALID_TYPE()
143143

144-
this.customParsers.delete(contentType.toString())
144+
const removed = this.customParsers.delete(contentType.toString())
145145

146146
const parsers = typeof contentType === 'string' ? this.parserList : this.parserRegExpList
147147

@@ -150,6 +150,8 @@ ContentTypeParser.prototype.remove = function (contentType) {
150150
if (idx > -1) {
151151
parsers.splice(idx, 1)
152152
}
153+
154+
return removed || idx > -1
153155
}
154156

155157
ContentTypeParser.prototype.run = function (contentType, handler, request, reply) {

test/content-parser.test.js

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -256,27 +256,25 @@ test('Error thrown 415 from content type is null and make post request to server
256256

257257
test('remove', t => {
258258
test('should remove default parser', t => {
259-
t.plan(2)
259+
t.plan(3)
260260

261261
const fastify = Fastify()
262262
const contentTypeParser = fastify[keys.kContentTypeParser]
263263

264-
contentTypeParser.remove('application/json')
265-
264+
t.ok(contentTypeParser.remove('application/json'))
266265
t.notOk(contentTypeParser.customParsers['application/json'])
267266
t.notOk(contentTypeParser.parserList.find(parser => parser === 'application/json'))
268267
})
269268

270269
test('should remove RegExp parser', t => {
271-
t.plan(2)
270+
t.plan(3)
272271

273272
const fastify = Fastify()
274273
fastify.addContentTypeParser(/^text\/*/, first)
275274

276275
const contentTypeParser = fastify[keys.kContentTypeParser]
277276

278-
contentTypeParser.remove(/^text\/*/)
279-
277+
t.ok(contentTypeParser.remove(/^text\/*/))
280278
t.notOk(contentTypeParser.customParsers[/^text\/*/])
281279
t.notOk(contentTypeParser.parserRegExpList.find(parser => parser.toString() === /^text\/*/.toString()))
282280
})
@@ -289,23 +287,22 @@ test('remove', t => {
289287
t.throws(() => fastify[keys.kContentTypeParser].remove(12), FST_ERR_CTP_INVALID_TYPE)
290288
})
291289

292-
test('should not throw error if content type does not exist', t => {
290+
test('should return false if content type does not exist', t => {
293291
t.plan(1)
294292

295293
const fastify = Fastify()
296294

297-
t.doesNotThrow(() => fastify[keys.kContentTypeParser].remove('image/png'))
295+
t.notOk(fastify[keys.kContentTypeParser].remove('image/png'))
298296
})
299297

300298
test('should not remove any content type parser if content type does not exist', t => {
301-
t.plan(1)
299+
t.plan(2)
302300

303301
const fastify = Fastify()
304302

305303
const contentTypeParser = fastify[keys.kContentTypeParser]
306304

307-
contentTypeParser.remove('image/png')
308-
305+
t.notOk(contentTypeParser.remove('image/png'))
309306
t.same(contentTypeParser.customParsers.size, 2)
310307
})
311308

0 commit comments

Comments
 (0)