Skip to content

Commit

Permalink
Feature: parameter for NOT DROP connection on 5xx status
Browse files Browse the repository at this point in the history
  • Loading branch information
FabioBeneditto committed Mar 2, 2022
1 parent d30f4a9 commit b5e0e72
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Useful in code tests:
- for tests, `/api/418` returns 418 http status code
- if http status code is between `100` and `399` returns image uri
- if http status code is `5xx` randomly drops connection
- with additional parameter `/api/5xx/1` connection was not dropped (useful for tests)

## Requirements
- `node` and `npm`
Expand Down
35 changes: 32 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const HTTPStatusCode = require('http-status-code')
const fortune = require('random-fortune')
var phrase = fortune.fortune()
var statusId = 200
var ignoreDrop = false
var allStatus = HTTPStatusCode.getProtocolDefinitions()

// Favicon - because I don't like unnecessary errors on log
Expand All @@ -33,7 +34,7 @@ var dropConnection = function (statusId, res, jsonReturn) {
if((parseInt(Math.random() * 100) % 2) > 0){
res.status(statusId).end()
} else {
res.status(statusId).json(jsonReturn)
res.status(statusId).json(jsonReturn)
}
} else {
// Bypass if statusId < 500
Expand Down Expand Up @@ -68,8 +69,7 @@ app.get('/api', (req, res, next) => {
})

/**
* Request with parameters, ex.:
* /api/418
* Request with parameters, ex.: /api/418
* Returns this HTTP status code
*/

Expand All @@ -84,6 +84,14 @@ app.param('id', function (req, res, next, id) {
next()
})

// Read OPTIONAL parameter to drop 500 connection
app.param('drop', function (req, res, next, drop) {
if(drop) {
ignoreDrop = true
}
next()
})

// Run Request
app.get('/api/:id', function (req, res, next) {
phrase = fortune.fortune()
Expand All @@ -106,6 +114,27 @@ app.get('/api/:id', function (req, res, next) {
}
})

app.get('/api/:id/:drop', function (req, res, next) {
phrase = fortune.fortune()
var jsonReturn = {
'code': statusId,
'status': HTTPStatusCode.getMessage(statusId),
'message': phrase
}

if(statusId >= 100 && statusId < 400) {
// Async to obtain image uri
photoCli.photos.random()
.then (photo => {
jsonReturn.image = photo.src.medium
res.status(statusId).json(jsonReturn)
} )
.catch(next);
} else {
res.status(statusId).json(jsonReturn)
}
})

// Indicate running app
app.listen(port, () => {
console.log('Our app is running on port ' + port + ', with current status: \n\n' + phrase)
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ describe('main App', function(){
})
})

describe('get /api/Invalid', function(){
it('Should Return 500', function(done){
describe('get /api/Invalid/1', function(){
it('Should Return 500 error', function(done){
request(app)
.get('/api/Invalid')
.get('/api/Invalid/1')
.set('Accept','application/json')
.expect('Content-Type', /json/)
.expect(500)
Expand Down

0 comments on commit b5e0e72

Please sign in to comment.