From 8e8fe31982e1a9a1b6f7907ad09f06cd8ac4c60e Mon Sep 17 00:00:00 2001 From: Fabio Beneditto Date: Wed, 2 Mar 2022 08:57:25 -0300 Subject: [PATCH] Fix: redirect invalid HTTP codes to 500 --- index.js | 7 ++++++- test/index.js | 17 ++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 5e6ddda..3d219ca 100644 --- a/index.js +++ b/index.js @@ -75,7 +75,12 @@ app.get('/api', (req, res, next) => { // Read parameter and set to statusId app.param('id', function (req, res, next, id) { - statusId = parseInt(id) + // Prevent invalid codes + if(typeof allStatus[id] === 'undefined') { + statusId = 500 + } else { + statusId = parseInt(id) + } next() }) diff --git a/test/index.js b/test/index.js index 2539420..b23673f 100644 --- a/test/index.js +++ b/test/index.js @@ -49,7 +49,7 @@ describe('main App', function(){ }) }) - describe('get /api/400', function(){ + describe('get /api/400', function(){ it('Should NOT return image uri', function(done){ request(app) .get('/api/400') @@ -64,4 +64,19 @@ describe('main App', function(){ }) }) + describe('get /api/Invalid', function(){ + it('Should Return 500', function(done){ + request(app) + .get('/api/Invalid') + .set('Accept','application/json') + .expect('Content-Type', /json/) + .expect(500) + .then(res => { + expect(res.body.code, 500) + done() + }) + .catch(err => done(err)) + }) + }) + }) \ No newline at end of file