Skip to content

Commit

Permalink
Randomize status code (#41)
Browse files Browse the repository at this point in the history
* Randomize status code response
  • Loading branch information
Ealenn authored Mar 15, 2020
1 parent fc48f3a commit e887971
Show file tree
Hide file tree
Showing 6 changed files with 227 additions and 35 deletions.
65 changes: 51 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ Available:

## Configuration

| Environment | Helm | CLI | Default |
|---------------------|--------------------------------|-----------------------|---------------|
| PORT | service.port | --port | `80` |
| LOGS__IGNORE__PING | application.logs.ignore.ping | --logs:ignore:ping | `false` |
| ENABLE__HOST | application.enable.host | --enable:host | `true` |
| ENABLE__HTTP | application.enable.http | --enable:http | `true` |
| ENABLE__REQUEST | application.enable.request | --enable:request | `true` |
| ENABLE__ENVIRONMENT | application.enable.environment | --enable:environment | `true` |
| ENABLE__FILE | application.enable.file | --enable:file | `true` |
| Environment | Helm | CLI | Default |
|------------------------------------|----------------------------------|------------------------------------|---------------|
| PORT | service.port | --port | `80` |
| LOGS__IGNORE__PING | application.logs.ignore.ping | --logs:ignore:ping | `false` |
| ENABLE__HOST | application.enable.host | --enable:host | `true` |
| ENABLE__HTTP | application.enable.http | --enable:http | `true` |
| ENABLE__REQUEST | application.enable.request | --enable:request | `true` |
| ENABLE__ENVIRONMENT | application.enable.environment | --enable:environment | `true` |
| ENABLE__FILE | application.enable.file | --enable:file | `true` |
| COMMANDS__HTTPCODE__HEADERRESPONSE | commands.httpCode.headerResponse | --commands:httpCode:headerResponse | `true` |

## Use Echo-Server

Expand All @@ -51,11 +52,12 @@ I use [jq](https://stedolan.github.io/jq) for nice `curl` results ;)

| Query | Header | Content | Conditions |
|---------------------|-----------------------|----------------------------------| ------------------------- |
| ?echo_code= | X-ECHO-CODE | HTTP code (ex `200`, `404`) | 200 <= `CODE` <= 599 |
| ?echo_code= | X-ECHO-CODE | HTTP code `200`, `404` | 200 <= `CODE` <= 599 |
| | | `404-401` or `200-500-301` | |
| ?echo_body= | X-ECHO-BODY | Body message | |
| ?echo_env_body= | X-ECHO-ENV-BODY | The key of environment variable | Enable environment `true` |
| ?echo_time= | X-ECHO-TIME | Wait time in `ms` | 0 <= `TIME` <= 30.000 |
| ?echo_file= | X-ECHO-FILE | Path of Directory or File | Enable file `true` |
| ?echo_file= | X-ECHO-FILE | Path of Directory or File | Enable file `true` |

#### Custom HTTP Status Code

Expand All @@ -66,6 +68,28 @@ I use [jq](https://stedolan.github.io/jq) for nice `curl` results ;)
HTTP/1.1 404 Not Found
```

```bash
➜ curl -I --header 'X-ECHO-CODE: 404-300' $ECHO_HOST
➜ curl $ECHO_HOST/?echo_code=404-300

HTTP/1.1 404 Not Found
HTTP/1.1 300 Multiple Choices
```

```bash
for i in {1..10}
do
➜ curl -I $ECHO_HOST/?echo_code=200-400-500
done

HTTP/1.1 500 Internal Server Error
HTTP/1.1 400 Bad Request
HTTP/1.1 200 OK
HTTP/1.1 500 Internal Server Error
HTTP/1.1 200 OK
HTTP/1.1 500 Internal Server Error
```

#### Custom Body

```bash
Expand All @@ -78,10 +102,23 @@ HTTP/1.1 404 Not Found
#### Custom Body with Environment variable value

```bash
➜ curl --header 'X-ECHO-ENV-BODY: HOME' $ECHO_HOST
➜ curl $ECHO_HOST/?echo_env_body=HOME
➜ curl --header 'X-ECHO-ENV-BODY: HOSTNAME' $ECHO_HOST
➜ curl $ECHO_HOST/?echo_env_body=HOSTNAME

"/root"
"c53a9ed79fa2"
```

```bash
for i in {1..10}
do
➜ curl $ECHO_HOST/?echo_env_body=HOSTNAME
done

"c53a9ed79fa2"
"f10c3af61e40"
"c53a9ed79fa2"
"f10c3af61e40"
"c53a9ed79fa2"
```

#### Custom response latency
Expand Down
19 changes: 10 additions & 9 deletions docs/pages/includes/section-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ nav_exclude: true

## Configuration

| Environment | Helm | CLI | Default |
|---------------------|--------------------------------|-----------------------|---------------|
| PORT | service.port | --port | `80` |
| LOGS__IGNORE__PING | application.logs.ignore.ping | --logs:ignore:ping | `false` |
| ENABLE__HOST | application.enable.host | --enable:host | `true` |
| ENABLE__HTTP | application.enable.http | --enable:http | `true` |
| ENABLE__REQUEST | application.enable.request | --enable:request | `true` |
| ENABLE__ENVIRONMENT | application.enable.environment | --enable:environment | `true` |
| ENABLE__FILE | application.enable.file | --enable:file | `true` |
| Environment | Helm | CLI | Default |
|------------------------------------|----------------------------------|------------------------------------|---------------|
| PORT | service.port | --port | `80` |
| LOGS__IGNORE__PING | application.logs.ignore.ping | --logs:ignore:ping | `false` |
| ENABLE__HOST | application.enable.host | --enable:host | `true` |
| ENABLE__HTTP | application.enable.http | --enable:http | `true` |
| ENABLE__REQUEST | application.enable.request | --enable:request | `true` |
| ENABLE__ENVIRONMENT | application.enable.environment | --enable:environment | `true` |
| ENABLE__FILE | application.enable.file | --enable:file | `true` |
| COMMANDS__HTTPCODE__HEADERRESPONSE | commands.httpCode.headerResponse | --commands:httpCode:headerResponse | `true` |
45 changes: 41 additions & 4 deletions docs/pages/includes/section-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ I use [jq](https://stedolan.github.io/jq) for nice `curl` results ;)

| Query | Header | Content | Conditions |
|---------------------|-----------------------|----------------------------------| ------------------------- |
| ?echo_code= | X-ECHO-CODE | HTTP code (ex `200`, `404`) | 200 <= `CODE` <= 599 |
| ?echo_code= | X-ECHO-CODE | HTTP code `200`, `404` | 200 <= `CODE` <= 599 |
| | | `404-401` or `200-500-301` | |
| ?echo_body= | X-ECHO-BODY | Body message | |
| ?echo_env_body= | X-ECHO-ENV-BODY | The key of environment variable | Enable environment `true` |
| ?echo_time= | X-ECHO-TIME | Wait time in `ms` | 0 <= `TIME` <= 30.000 |
| ?echo_file= | X-ECHO-FILE | Path of Directory or File | Enable file `true` |

#### Custom HTTP Status Code

Expand All @@ -29,6 +31,28 @@ I use [jq](https://stedolan.github.io/jq) for nice `curl` results ;)
HTTP/1.1 404 Not Found
```

```bash
➜ curl -I --header 'X-ECHO-CODE: 404-300' $ECHO_HOST
➜ curl $ECHO_HOST/?echo_code=404-300

HTTP/1.1 404 Not Found
HTTP/1.1 300 Multiple Choices
```

```bash
for i in {1..10}
do
➜ curl -I $ECHO_HOST/?echo_code=200-400-500
done

HTTP/1.1 500 Internal Server Error
HTTP/1.1 400 Bad Request
HTTP/1.1 200 OK
HTTP/1.1 500 Internal Server Error
HTTP/1.1 200 OK
HTTP/1.1 500 Internal Server Error
```

#### Custom Body

```bash
Expand All @@ -41,10 +65,23 @@ HTTP/1.1 404 Not Found
#### Custom Body with Environment variable value

```bash
➜ curl --header 'X-ECHO-ENV-BODY: HOME' $ECHO_HOST
➜ curl $ECHO_HOST/?echo_env_body=HOME
➜ curl --header 'X-ECHO-ENV-BODY: HOSTNAME' $ECHO_HOST
➜ curl $ECHO_HOST/?echo_env_body=HOSTNAME

"/root"
"c53a9ed79fa2"
```

```bash
for i in {1..10}
do
➜ curl $ECHO_HOST/?echo_env_body=HOSTNAME
done

"c53a9ed79fa2"
"f10c3af61e40"
"c53a9ed79fa2"
"f10c3af61e40"
"c53a9ed79fa2"
```

#### Custom response latency
Expand Down
3 changes: 2 additions & 1 deletion src/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"httpCode": {
"query": "echo_code",
"header": "x-echo-code"
"header": "x-echo-code",
"headerResponse": true
},
"time": {
"query": "echo_time",
Expand Down
33 changes: 26 additions & 7 deletions src/middlewares/customHttpCodeMiddleware.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
const config = require('../nconf');
const isNormalInteger = (str) => /^\+?(0|[1-9]\d*)$/.test(str);

const setupHttpCode = (value, res) => {
if (value &&
value >= 200 &&
value <= 599) {
res.status(value);
if (value){
var values = value.split("-");
var validValues = [];
values.forEach(element => {
if (element &&
isNormalInteger(element) &&
element >= 200 &&
element <= 599) {
validValues.push(element);
}
});

if (validValues.length > 0){
res.status(validValues[Math.floor(Math.random() * validValues.length)]);
}

if (config.get('commands:httpCode:headerResponse')){
res.header('X-ECHO-RANDOM-STATUS', validValues);
}
}
}

module.exports = (req, res, next) => {
setupHttpCode(req.headers[config.get('commands:httpCode:header')], res);
setupHttpCode(req.query[config.get('commands:httpCode:query')], res);
next();
try {
setupHttpCode(req.headers[config.get('commands:httpCode:header')], res);
setupHttpCode(req.query[config.get('commands:httpCode:query')], res);
} finally {
next();
}
}
97 changes: 97 additions & 0 deletions test/custom.code.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const request = require('supertest');
const assert = require('assert');

describe('Custom HTTP Code', function () {
var server;
Expand Down Expand Up @@ -90,4 +91,100 @@ describe('Custom HTTP Code', function () {
.get('/?echo_code=500')
.expect(500, done);
});
for (let a=200;a<203;a++){
for (let b=400;b<403;b++){
it(`Custom rand status ${a} or ${b} with header`, (done) => {
request(server)
.get('/')
.set('X-ECHO-CODE', `${a}-${b}`)
.end((err, res) => {
assert.equal(true, res.status == a || res.status == b);
assert.equal(res.header['x-echo-random-status'], `${a}, ${b}`);
done();
})
});
}
}
for (let a=200;a<202;a++){
for (let b=300;b<302;b++){
for (let c=400;c<402;c++){
it(`Custom rand status ${a}, ${b}, ${c} with header`, (done) => {
request(server)
.get('/')
.set('X-ECHO-CODE', `${a}-${b}-${c}`)
.end((err, res) => {
assert.equal(true, res.status == a || res.status == b || res.status == c);
assert.equal(res.header['x-echo-random-status'], `${a}, ${b}, ${c}`);
done();
})
});
}
}
}
for (let a=200;a<203;a++){
for (let b=400;b<403;b++){
it(`Custom rand status ${a} or ${b} with query`, (done) => {
request(server)
.get('/?echo_code='+`${a}-${b}`)
.end((err, res) => {
assert.equal(true, res.status == a || res.status == b);
assert.equal(res.header['x-echo-random-status'], `${a}, ${b}`);
done();
})
});
}
}
for (let a=200;a<202;a++){
for (let b=300;b<302;b++){
for (let c=400;c<402;c++){
it(`Custom rand status ${a}, ${b}, ${c} with query`, (done) => {
request(server)
.get('/?echo_code='+`${a}-${b}-${c}`)
.end((err, res) => {
assert.equal(true, res.status == a || res.status == b || res.status == c);
assert.equal(res.header['x-echo-random-status'], `${a}, ${b}, ${c}`);
done();
})
});
}
}
}
it("Custom with null separator", (done) => {
request(server)
.get('/')
.set('X-ECHO-CODE', "200.401")
.end((err, res) => {
assert.equal(200, res.status);
assert.equal(res.header['x-echo-random-status'], undefined);
done();
})
});
it("Custom with null args in Query", (done) => {
request(server)
.get('/?echo_code=200/404')
.end((err, res) => {
assert.equal(200, res.status);
assert.equal(res.header['x-echo-random-status'], undefined);
done();
})
});
it("Custom with null separator but correct value", (done) => {
request(server)
.get('/')
.set('X-ECHO-CODE', "200.401-401")
.end((err, res) => {
assert.equal(401, res.status);
assert.equal(res.header['x-echo-random-status'], '401');
done();
})
});
it("Custom with null separator but correct value", (done) => {
request(server)
.get('/?echo_code=200.401-404')
.end((err, res) => {
assert.equal(res.status, 404);
assert.equal(res.header['x-echo-random-status'], '404');
done();
});
});
});

0 comments on commit e887971

Please sign in to comment.