diff --git a/README.md b/README.md index 3cdb298..642c9a6 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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) diff --git a/docs/assets/images/curl.png b/docs/assets/images/curl.png new file mode 100644 index 0000000..94f5b9c Binary files /dev/null and b/docs/assets/images/curl.png differ diff --git a/docs/index.md b/docs/index.md index 212e45e..27ace84 100644 --- a/docs/index.md +++ b/docs/index.md @@ -23,6 +23,7 @@ Available: - Request (Query, Body, IPs, Host, Urls...) - Headers - Environment variables +- Control via Headers/Query ## Table of contents @@ -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 %} diff --git a/src/app.js b/src/app.js index 53076c9..0b618c9 100644 --- a/src/app.js +++ b/src/app.js @@ -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 \ No newline at end of file diff --git a/src/middlewares/customHttpBodyMiddleware.js b/src/middlewares/customHttpBodyMiddleware.js new file mode 100644 index 0000000..4960b4b --- /dev/null +++ b/src/middlewares/customHttpBodyMiddleware.js @@ -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(); + } +} \ No newline at end of file diff --git a/src/middlewares/customHttpCodeMiddleware.js b/src/middlewares/customHttpCodeMiddleware.js new file mode 100644 index 0000000..25d46b4 --- /dev/null +++ b/src/middlewares/customHttpCodeMiddleware.js @@ -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(); +} \ No newline at end of file diff --git a/src/middlewares/customHttpEnvBodyMiddleware.js b/src/middlewares/customHttpEnvBodyMiddleware.js new file mode 100644 index 0000000..7d4da46 --- /dev/null +++ b/src/middlewares/customHttpEnvBodyMiddleware.js @@ -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(); + } +} \ No newline at end of file diff --git a/src/middlewares/logMiddleware.js b/src/middlewares/logMiddleware.js new file mode 100644 index 0000000..810845a --- /dev/null +++ b/src/middlewares/logMiddleware.js @@ -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(); +} \ No newline at end of file diff --git a/test/custom.body.js b/test/custom.body.js new file mode 100644 index 0000000..ba0276c --- /dev/null +++ b/test/custom.body.js @@ -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); + }); +}); \ No newline at end of file diff --git a/test/custom.code.js b/test/custom.code.js new file mode 100644 index 0000000..307a185 --- /dev/null +++ b/test/custom.code.js @@ -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); + }); +}); \ No newline at end of file diff --git a/test/custom.environment.js b/test/custom.environment.js new file mode 100644 index 0000000..2bfdbb3 --- /dev/null +++ b/test/custom.environment.js @@ -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); + }); +}); \ No newline at end of file diff --git a/test/environment.js b/test/environment.js new file mode 100644 index 0000000..6da67c6 --- /dev/null +++ b/test/environment.js @@ -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); + }); +}); \ No newline at end of file