Skip to content

Commit

Permalink
add possibility do delay and set response status code by providing sp…
Browse files Browse the repository at this point in the history
…ecific headers (#53)


---------

Signed-off-by: Marcin Skalski <[email protected]>
  • Loading branch information
Automaat authored Oct 1, 2024
1 parent 7ba5ab1 commit 0cbab4f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,21 @@ The `APP_VERSION` and `APP_COLOR` environment variables are handy when we want t

[kuma-url]: https://kuma.io/
[kuma-logo]: https://kuma-public-assets.s3.amazonaws.com/kuma-logo-v2.png

## Modifying responses

### Adding delay to response

To add delay to response you need to set header `x-set-response-delay-ms`. Example:

```shell
curl localhost:5001/increment -XPOST -H "x-set-response-delay-ms: 5000"
```

### Enforcing response status code

To enforce response status code you need to set header `x-set-response-status-code`. Example:

```shell
curl localhost:5001/increment -XPOST -H "x-set-response-status-code: 503"
```
32 changes: 24 additions & 8 deletions app/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ const PORT = 5000;

const app = express();

var version = process.env.APP_VERSION || "1.0";
var color = process.env.APP_COLOR || "#efefef";
const version = process.env.APP_VERSION || "1.0";
const color = process.env.APP_COLOR || "#efefef";

function getClient() {
var host = process.env.REDIS_HOST || "127.0.0.1";
var port = parseInt(process.env.REDIS_PORT) || 6379;
const host = process.env.REDIS_HOST || "127.0.0.1";
const port = parseInt(process.env.REDIS_PORT) || 6379;
console.log("Connecting to Redis at %s:%d", host, port);
var client = new Redis({
const client = new Redis({
port: port,
host: host,
family: 4,
Expand All @@ -37,10 +37,26 @@ function getClient() {
return client;
}

const sleep = util.promisify(setTimeout);
const delayMiddleware = function (req, res, next) {
const delay = parseInt(req.header("x-set-response-delay-ms") || 0, 10)
sleep(delay).then(() => {
next()
})
}

const statusCodeMiddleware = function (req, res, next) {
const statusCode = parseInt(req.header("x-set-response-status-code") || 200, 10)
res.status(statusCode)
next()
}

app.use(delayMiddleware)
app.use(statusCodeMiddleware)
app.use('/', express.static('public'));

app.post('/increment', function(req, res){
var client = getClient();
const client = getClient();
client.incr(COUNTER_KEY, function (err, counter_result) {
if (err) {
console.log(err);
Expand All @@ -63,7 +79,7 @@ app.post('/increment', function(req, res){
});

app.delete('/counter', function(req, res){
var client = getClient();
const client = getClient();
client.del(COUNTER_KEY, function(err) {
if (err) {
console.log(err);
Expand All @@ -83,7 +99,7 @@ app.delete('/counter', function(req, res){
});

app.get('/counter', function(req, res){
var client = getClient();
const client = getClient();
client.get(COUNTER_KEY, function(err, counter_result) {
if (err) {
console.log(err);
Expand Down

0 comments on commit 0cbab4f

Please sign in to comment.