Skip to content

Commit

Permalink
Customize Responses Headers (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ealenn authored May 21, 2020
1 parent e53b780 commit 9bbfdd9
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 4 deletions.
23 changes: 22 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ Available:

- GET / POST / PUT / PATCH / DELETE
- Request (Query, Body, IPs, Host, Urls...)
- Headers
- Request Headers / Response Headers
- Environment variables
- Control via Headers/Query
- Folders and Files

Docker OS/ARCH :

Expand Down Expand Up @@ -46,6 +47,7 @@ Docker OS/ARCH :
| ENABLE__REQUEST | application.enable.request | --enable:request | `true` |
| ENABLE__ENVIRONMENT | application.enable.environment | --enable:environment | `true` |
| ENABLE__FILE | application.enable.file | --enable:file | `true` |
| ENABLE__HEADER | application.enable.header | --enable:header | `true` |

## Use Echo-Server

Expand Down Expand Up @@ -128,6 +130,25 @@ HTTP/1.1 500 Internal Server Error
"c53a9ed79fa2"
```

#### Custom Headers

```bash
➜ curl --header 'X-ECHO-HEADER: One:1' $ECHO_HOST
➜ curl $ECHO_HOST/?echo_header=One:1

HTTP/1.1 200 OK
One: 1
```

```bash
➜ curl --header 'X-ECHO-HEADER: One:1, Two:2' $ECHO_HOST
➜ curl "$ECHO_HOST/?echo_header=One:1,%20Two:2"

HTTP/1.1 200 OK
One: 1
Two: 2
```

#### Custom response latency

```bash
Expand Down
4 changes: 3 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ nav_order: 1
[![GitHub stars](https://img.shields.io/github/stars/Ealenn/Echo-Server?style=for-the-badge&logo=github)](https://github.com/Ealenn/Echo-Server/stargazers)
[![GitHub issues](https://img.shields.io/github/issues/Ealenn/Echo-Server?style=for-the-badge&logo=github)](https://github.com/Ealenn/Echo-Server/issues)
[![DockerHub](https://img.shields.io/docker/pulls/ealen/echo-server.svg?style=for-the-badge&logo=docker)](https://hub.docker.com/repository/docker/ealen/echo-server)
[![DockerHub](https://img.shields.io/badge/SIZE-%3C%2013%20MB-1488C6?style=for-the-badge&logo=docker)](https://hub.docker.com/repository/docker/ealen/echo-server)
[![DockerHub](https://img.shields.io/badge/SIZE-%3C%2030%20MB-1488C6?style=for-the-badge&logo=docker)](https://hub.docker.com/repository/docker/ealen/echo-server)

An echo server is a server that replicates the request sent by the client and sends it back.

Expand All @@ -25,6 +25,8 @@ Available:
- Environment variables
- Control via Headers/Query

Docker OS/ARCH : linux/amd64 - linux/arm/v6 - linux/arm/v7 - linux/arm64 - linux/386

## Table of contents

- [Docker](/pages/docker.html) : Use docker container
Expand Down
2 changes: 1 addition & 1 deletion docs/pages/includes/section-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ nav_exclude: 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` |
| ENABLE__HEADER | application.enable.header | --enable:header | `true` |
19 changes: 19 additions & 0 deletions docs/pages/includes/section-tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,25 @@ HTTP/1.1 500 Internal Server Error
"c53a9ed79fa2"
```

#### Custom Headers

```bash
➜ curl --header 'X-ECHO-HEADER: One:1' $ECHO_HOST
➜ curl $ECHO_HOST/?echo_header=One:1

HTTP/1.1 200 OK
One: 1
```

```bash
➜ curl --header 'X-ECHO-HEADER: One:1, Two:2' $ECHO_HOST
➜ curl "$ECHO_HOST/?echo_header=One:1,%20Two:2"

HTTP/1.1 200 OK
One: 1
Two: 2
```

#### Custom response latency

```bash
Expand Down
1 change: 1 addition & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ app.use(require('./middlewares/logMiddleware'));
app.use(require('./middlewares/showFileMiddleware'));
app.use(require('./middlewares/customResponseTime'));
app.use(require('./middlewares/customHttpCodeMiddleware'));
app.use(require('./middlewares/customHttpHeadersMiddleware'));
app.use(require('./middlewares/customHttpEnvBodyMiddleware'));
app.use(require('./middlewares/customHttpBodyMiddleware'));

Expand Down
7 changes: 6 additions & 1 deletion src/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"http": true,
"request": true,
"environment": true,
"file": true
"file": true,
"header": true
},
"commands": {
"httpBody": {
Expand All @@ -26,6 +27,10 @@
"header": "x-echo-code",
"headerResponse": true
},
"httpHeaders": {
"query": "echo_header",
"header": "x-echo-header"
},
"time": {
"query": "echo_time",
"header": "x-echo-time"
Expand Down
24 changes: 24 additions & 0 deletions src/middlewares/customHttpHeadersMiddleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const config = require('../nconf');

const setupHttpHeaders = (value, res) => {
if (value) {
var elements = value.split(', ');
elements.forEach(customHeader => {
var array = customHeader.split(':');
res.header(array[0], array.slice(1, array.length).join(':'));
});
}
}

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

describe('Custom Headers', function () {
var server;
beforeEach(function () {
server = require('../src/app');
});
afterEach(function () {
server.close();
});
it('Simple Header', (done) => {
request(server)
.get('/')
.set('X-ECHO-HEADER', 'Accept-Language: en-US')
.expect(function (res) {
assert.equal(res.header['accept-language'], 'en-US')
})
.expect(200, done);
});
it('Complexe Header', (done) => {
request(server)
.get('/')
.set('X-ECHO-HEADER', 'Host: en.echo-server.local:3000')
.expect(function (res) {
assert.equal(res.header['host'], 'en.echo-server.local:3000')
})
.expect(200, done);
});
it('Multiple Headers', (done) => {
request(server)
.get('/')
.set('X-ECHO-HEADER', 'One: 1, Two: 2')
.expect(function (res) {
assert.equal(res.header['one'], '1')
assert.equal(res.header['two'], '2')
})
.expect(200, done);
});
it('Simple Query', (done) => {
request(server)
.get('/?echo_header=Accept-Language: en-US')
.expect(function (res) {
assert.equal(res.header['accept-language'], 'en-US')
})
.expect(200, done);
});
it('Complexe Query', (done) => {
request(server)
.get('/?echo_header=One:1, Two:2')
.expect(function (res) {
assert.equal(res.header['one'], '1')
assert.equal(res.header['two'], '2')
})
.expect(200, done);
});
});

0 comments on commit 9bbfdd9

Please sign in to comment.