From b5e0e72def421326746ff9d21e1153c0d399e253 Mon Sep 17 00:00:00 2001 From: Fabio Beneditto Date: Wed, 2 Mar 2022 10:14:33 -0300 Subject: [PATCH] Feature: parameter for NOT DROP connection on 5xx status --- README.md | 1 + index.js | 35 ++++++++++++++++++++++++++++++++--- test/index.js | 6 +++--- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index bfe0e4e..e49ba8c 100644 --- a/README.md +++ b/README.md @@ -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` diff --git a/index.js b/index.js index 3d219ca..d2572d0 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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 @@ -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 */ @@ -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() @@ -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) diff --git a/test/index.js b/test/index.js index b23673f..2091bb4 100644 --- a/test/index.js +++ b/test/index.js @@ -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)