Skip to content

Commit eb55fca

Browse files
Randomly drops connection when http status between 500 and 599
1 parent eed2a57 commit eb55fca

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

index.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ var randomProperty = function (obj) {
2727
return parseInt(newKey);
2828
};
2929

30+
// Randomly drops 5xx connections
31+
var dropConnection = function (statusId, res, jsonReturn) {
32+
if(statusId > 499 && statusId < 600){
33+
if((parseInt(Math.random() * 100) % 2) > 0){
34+
res.status(statusId).end()
35+
} else {
36+
res.status(statusId).json(jsonReturn)
37+
}
38+
} else {
39+
// Bypass if statusId < 500
40+
res.status(statusId).json(jsonReturn)
41+
}
42+
}
43+
3044
/**
3145
* Default API request.
3246
* Returns random HTTP Status code from HTTPStatusCode list
@@ -49,7 +63,7 @@ app.get('/api', (req, res, next) => {
4963
} )
5064
.catch(next);
5165
} else {
52-
res.status(statusId).json(jsonReturn)
66+
dropConnection(statusId, res, jsonReturn)
5367
}
5468
})
5569

@@ -83,7 +97,7 @@ app.get('/api/:id', function (req, res, next) {
8397
} )
8498
.catch(next);
8599
} else {
86-
res.status(statusId).json(jsonReturn)
100+
dropConnection(statusId, res, jsonReturn)
87101
}
88102
})
89103

0 commit comments

Comments
 (0)