Skip to content

Commit 5613125

Browse files
committed
Sleep based on x-set-response-delay-ms header
1 parent f62fb56 commit 5613125

2 files changed

Lines changed: 28 additions & 10 deletions

File tree

index.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ var app = express()
66
const os = require('os');
77
const jwt = require('jsonwebtoken');
88
var concat = require('concat-stream');
9+
const { promisify } = require('util');
10+
const sleep = promisify(setTimeout);
911

1012
app.set('json spaces', 2);
1113
app.set('trust proxy', ['loopback', 'linklocal', 'uniquelocal']);
@@ -61,17 +63,24 @@ app.all('*', (req, res) => {
6163
res.status(setResponseStatusCode)
6264
}
6365

64-
res.json(echo);
65-
if (process.env.LOG_IGNORE_PATH != req.path) {
66-
console.log('-----------------')
66+
const sleepTime = parseInt(req.headers["x-set-response-delay-ms"], 0)
67+
sleep(sleepTime).then(() => {
68+
69+
res.json(echo);
6770

68-
let spacer = 4;
69-
if(process.env.LOG_WITHOUT_NEWLINE){
70-
spacer = null;
71+
if (process.env.LOG_IGNORE_PATH != req.path) {
72+
console.log('-----------------')
73+
74+
let spacer = 4;
75+
if(process.env.LOG_WITHOUT_NEWLINE){
76+
spacer = null;
77+
}
78+
79+
console.log(JSON.stringify(echo, null, spacer));
7180
}
81+
});
7282

73-
console.log(JSON.stringify(echo, null, spacer));
74-
}
83+
7584
});
7685

7786
const sslOpts = {

tests.sh

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,14 +64,23 @@ else
6464
exit 1
6565
fi
6666

67+
REQUEST_WITH_SLEEP_MS=$(curl -o /dev/null -Ss -H "x-set-response-delay-ms: 6000" -k https://localhost:8443/ -w '%{time_total}')
68+
if [[ $(echo "$REQUEST_WITH_SLEEP_MS>5" | bc -l) == 1 ]]; then
69+
passed "Request with response delay passed"
70+
else
71+
failed "Request with response delay failed"
72+
echo $REQUEST_WITH_SLEEP_MS
73+
exit 1
74+
fi
75+
6776
REQUEST=$(curl -s -X PUT -H "Arbitrary:Header" -d aaa=bbb http://localhost:8080/hello-world)
6877
if [ $(echo $REQUEST | jq -r '.path') == '/hello-world' ] && \
6978
[ $(echo $REQUEST | jq -r '.method') == 'PUT' ] && \
7079
[ $(echo $REQUEST | jq -r '.headers.arbitrary') == 'Header' ]
7180
then
72-
passed "HTTP request passed."
81+
passed "HTTP request with arbitrary header passed."
7382
else
74-
failed "HTTP request failed."
83+
failed "HTTP request with arbitrary header failed."
7584
echo $REQUEST | jq
7685
exit 1
7786
fi

0 commit comments

Comments
 (0)