Skip to content

Commit

Permalink
Merge pull request #4 from FabioBeneditto/not-valid-http-code
Browse files Browse the repository at this point in the history
Fix: redirect invalid HTTP codes to 500
  • Loading branch information
FabioBeneditto authored Mar 2, 2022
2 parents 072760f + 8e8fe31 commit d30f4a9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})

Expand Down
17 changes: 16 additions & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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))
})
})

})

0 comments on commit d30f4a9

Please sign in to comment.