Skip to content

Commit

Permalink
Personalize the response (#29)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ealenn authored Feb 1, 2020
1 parent 67d5ea6 commit 46bdcf5
Show file tree
Hide file tree
Showing 12 changed files with 323 additions and 19 deletions.
36 changes: 35 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ Available:
- Request (Query, Body, IPs, Host, Urls...)
- Headers
- Environment variables
- Control via Headers/Query

![docker](./docs/assets/images/docker.png)
![docker](https://ealenn.github.io/Echo-Server/assets/images/docker.png)

## Table of contents

Expand All @@ -37,6 +38,39 @@ Available:
| ENABLE__REQUEST | application.enable.request | --enable:request | `true` |
| ENABLE__ENVIRONMENT | application.enable.environment | --enable:environment | `true` |

## Default response

![curl](https://ealenn.github.io/Echo-Server/assets/images/curl.png)

## Custom response

| Query | Header | Content | Conditions |
|---------------------|---------------------|----------------------------------| ------------------------- |
| ?echo_code= | ECHO_CODE | HTTP code (ex `200`, `404`) | 200 >= `CODE` <= 599 |
| ?echo_body= | ECHO_BODY | Body message | |
| ?echo_env_body= | ECHO_ENV_BODY | The key of environment variable | Enable environment `true` |

```bash
➜ curl -I --header 'ECHO_CODE: 404' localhost:3000
➜ curl localhost:3000/?echo_code=404

HTTP/1.1 404 Not Found
```

```bash
➜ curl --header 'ECHO_BODY: amazing' localhost:3000
➜ curl localhost:3000/?echo_env_body=amazing

"amazing"
```

```bash
➜ curl --header 'ECHO_ENV_BODY: HOME' localhost:3000
➜ curl localhost:3000/?echo_env_body=HOME

"/root"
```

## Docker

[Read the docs](https://ealenn.github.io/Echo-Server/pages/docker.html)
Expand Down
Binary file added docs/assets/images/curl.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
34 changes: 34 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Available:
- Request (Query, Body, IPs, Host, Urls...)
- Headers
- Environment variables
- Control via Headers/Query

## Table of contents

Expand All @@ -34,6 +35,39 @@ Available:
- [Helm](/helm.html) Use `echo-server` helm repository and override values
- [Global Configuration](/configuration.html) Environments variables, CLI arguments...

## Default response

![curl](https://ealenn.github.io/Echo-Server/assets/images/curl.png)

## Custom response

| Query | Header | Content | Conditions |
|---------------------|---------------------|----------------------------------| ------------------------- |
| ?echo_code= | ECHO_CODE | HTTP code (ex `200`, `404`) | 200 >= `CODE` <= 599 |
| ?echo_body= | ECHO_BODY | Body message | |
| ?echo_env_body= | ECHO_ENV_BODY | The key of environment variable | Enable environment `true` |

```bash
➜ curl -I --header 'ECHO_CODE: 404' localhost:3000
➜ curl localhost:3000/?echo_code=404

HTTP/1.1 404 Not Found
```

```bash
➜ curl --header 'ECHO_BODY: amazing' localhost:3000
➜ curl localhost:3000/?echo_env_body=amazing

"amazing"
```

```bash
➜ curl --header 'ECHO_ENV_BODY: HOME' localhost:3000
➜ curl localhost:3000/?echo_env_body=HOME

"/root"
```

---

{% include_relative pages/includes/section-configuration.md %}
33 changes: 15 additions & 18 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,26 @@ const http = require('http')
, express = require('express')
, app = express()
, server = http.createServer(app)
, bodyParser = require('body-parser')
, config = require('./nconf');
, bodyParser = require('body-parser');

app.use(bodyParser.urlencoded({
extended: true
}));
// Parser
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.text());
app.use(bodyParser.json());
app.use(require('cookie-parser')());
app.use(require('multer')().array());

function response(req) {
if (req.originalUrl != "/ping" || !config.get('logs:ignore:ping')) {
console.log(`${Date.now()} | [${req.method}] - ${req.protocol}://${req.get('host')}${req.originalUrl}`);
}

return {
host: require('./response/host')(req),
http: require('./response/http')(req),
request: require('./response/request')(req),
environment: require('./response/environment')(req)
}
};
// Middlewares
app.use(require('./middlewares/logMiddleware'));
app.use(require('./middlewares/customHttpCodeMiddleware'));
app.use(require('./middlewares/customHttpEnvBodyMiddleware'));
app.use(require('./middlewares/customHttpBodyMiddleware'));

app.all('*', (req, res) => res.json(response(req)));
// Router
app.all('*', (req, res) => res.json({
host: require('./response/host')(req),
http: require('./response/http')(req),
request: require('./response/request')(req),
environment: require('./response/environment')(req)
}));
module.exports = server
17 changes: 17 additions & 0 deletions src/middlewares/customHttpBodyMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const returnBody = (value, res) => {
try {
res.json(JSON.parse(value));
} catch (e) {
res.json(value);
}
}

module.exports = (req, res, next) => {
if (req.headers.echo_body) {
returnBody(req.headers.echo_body, res);
} else if (req.query.echo_body) {
returnBody(req.query.echo_body, res);
} else {
next();
}
}
13 changes: 13 additions & 0 deletions src/middlewares/customHttpCodeMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const setupHttpCode = (value, res) => {
if (value &&
value >= 200 &&
value <= 599) {
res.status(value);
}
}

module.exports = (req, res, next) => {
setupHttpCode(req.headers.echo_code, res);
setupHttpCode(req.query.echo_code, res);
next();
}
13 changes: 13 additions & 0 deletions src/middlewares/customHttpEnvBodyMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const config = require('../nconf');

module.exports = (req, res, next) => {
if (!config.get('enable:environment')) {
next();
} else if (req.headers.echo_env_body) {
res.json(process.env[req.headers.echo_env_body]);
} else if (req.query.echo_env_body) {
res.json(process.env[req.query.echo_env_body]);
} else {
next();
}
}
8 changes: 8 additions & 0 deletions src/middlewares/logMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const config = require('../nconf');

module.exports = (req, res, next) => {
if (req.originalUrl != "/ping" || !config.get('logs:ignore:ping')) {
console.log(`${Date.now()} | [${req.method}] - ${req.protocol}://${req.get('host')}${req.originalUrl}`);
}
next();
}
46 changes: 46 additions & 0 deletions test/custom.body.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const assert = require('assert');
const request = require('supertest');

describe('Custom Body', function () {
var server;
beforeEach(function () {
server = require('../src/app');
});
afterEach(function () {
server.close();
});
it('Text with Header', (done) => {
request(server)
.get('/')
.set('ECHO_BODY', 'Example text')
.expect(function (res) {
assert.equal(res.body, "Example text")
})
.expect(200, done);
});
it('Json with Header', (done) => {
request(server)
.get('/')
.set('ECHO_BODY', '{"example": "json"}')
.expect(function (res) {
assert.equal(res.body.example, "json")
})
.expect(200, done);
});
it('Text with Query', (done) => {
request(server)
.get('/?echo_body=Example text')
.expect(function (res) {
assert.equal(res.body, "Example text")
})
.expect(200, done);
});
it('Json with Query', (done) => {
request(server)
.get('/?echo_body={"example": "json"}')
.expect(function (res) {
assert.equal(res.body.example, "json")
})
.expect(200, done);
});
});
93 changes: 93 additions & 0 deletions test/custom.code.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
const request = require('supertest');

describe('Custom HTTP Code', function () {
var server;
beforeEach(function () {
server = require('../src/app');
});
afterEach(function () {
server.close();
});
it("Default 200", (done) => {
request(server)
.get('/')
.expect(200, done);
});
it("Custom 200", (done) => {
request(server)
.get('/')
.set('ECHO_CODE', 200)
.expect(200, done);
});
it("Custom 200 with Query", (done) => {
request(server)
.get('/?echo_code=200')
.expect(200, done);
});
it("Custom 201", (done) => {
request(server)
.get('/')
.set('ECHO_CODE', 201)
.expect(201, done);
});
it("Custom 201 with Query", (done) => {
request(server)
.get('/?echo_code=201')
.expect(201, done);
});
it("Custom 300", (done) => {
request(server)
.get('/')
.set('ECHO_CODE', 300)
.expect(300, done);
});
it("Custom 300 with Query", (done) => {
request(server)
.get('/?echo_code=300')
.expect(300, done);
});
it("Custom 400", (done) => {
request(server)
.get('/')
.set('ECHO_CODE', 400)
.expect(400, done);
});
it("Custom 400 with Query", (done) => {
request(server)
.get('/?echo_code=400')
.expect(400, done);
});
it("Custom 401", (done) => {
request(server)
.get('/')
.set('ECHO_CODE', 401)
.expect(401, done);
});
it("Custom 401 with Query", (done) => {
request(server)
.get('/?echo_code=401')
.expect(401, done);
});
it("Custom 404", (done) => {
request(server)
.get('/')
.set('ECHO_CODE', 404)
.expect(404, done);
});
it("Custom 404 with Query", (done) => {
request(server)
.get('/?echo_code=404')
.expect(404, done);
});
it("Custom 500", (done) => {
request(server)
.get('/')
.set('ECHO_CODE', 500)
.expect(500, done);
});
it("Custom 500 with Query", (done) => {
request(server)
.get('/?echo_code=500')
.expect(500, done);
});
});
29 changes: 29 additions & 0 deletions test/custom.environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const assert = require('assert');
const request = require('supertest');

describe('Custom Body with Environment', function () {
var server;
beforeEach(function () {
server = require('../src/app');
});
afterEach(function () {
server.close();
});
it('LANG', (done) => {
request(server)
.get('/')
.set('ECHO_ENV_BODY', 'LANG')
.expect(function (res) {
assert.equal(res.body, process.env["LANG"])
})
.expect(200, done);
});
it('LANG with Query', (done) => {
request(server)
.get('/?echo_env_body=LANG')
.expect(function (res) {
assert.equal(res.body, process.env["LANG"])
})
.expect(200, done);
});
});
20 changes: 20 additions & 0 deletions test/environment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const assert = require('assert');
const request = require('supertest');

describe('Environment', function () {
var server;
beforeEach(function () {
server = require('../src/app');
});
afterEach(function () {
server.close();
});
it('Equal', (done) => {
request(server)
.get('/')
.expect(function (res) {
assert.equal(res.body.environment[0], process.env[0])
})
.expect(200, done);
});
});

0 comments on commit 46bdcf5

Please sign in to comment.