diff --git a/lib/index.js b/lib/index.js index 0749432..0596daa 100755 --- a/lib/index.js +++ b/lib/index.js @@ -117,10 +117,16 @@ internals.decoder = function (source, options) { const contentEncoding = source.headers['content-encoding']; const decoders = options.decoders ?? internals.decoders; - if (!decoders.hasOwnProperty(contentEncoding)) { + if (contentEncoding === undefined) { return source; } + if (!decoders.hasOwnProperty(contentEncoding)) { + const error = Boom.unsupportedMediaType(); + error.output.headers = { 'accept-encoding': [...Reflect.ownKeys(decoders), 'identity'].join(', ') }; + throw error; + } + const decoderOptions = options.compression?.[contentEncoding] ?? null; const stream = decoders[contentEncoding](decoderOptions); diff --git a/test/index.js b/test/index.js index 08f11e9..5ca4a61 100755 --- a/test/index.js +++ b/test/index.js @@ -622,7 +622,7 @@ describe('parse()', () => { expect(payload.toString()).to.equal(body); }); - it('leaves payload raw when encoding unknown', async () => { + it('errors when encoding unknown', async () => { const body = '{"x":"1","y":"2","z":"3"}'; const compressed = await internals.compress('gzip', body); @@ -632,8 +632,11 @@ describe('parse()', () => { 'content-type': 'application/json' }; - const { payload } = await Subtext.parse(request, null, { parse: 'gunzip', output: 'data' }); - expect(payload.toString()).to.equal(compressed.toString()); + const err = await expect(Subtext.parse(request, null, { parse: 'gunzip', output: 'data' })).to.reject('Unsupported Media Type'); + expect(err.output.statusCode).to.equal(415); + expect(err.output.headers).to.include({ + 'accept-encoding': 'gzip, deflate, identity' + }); }); it('unzips payload without parsing (external decoders)', async () => {