Skip to content

Commit

Permalink
feat(cookies): feature flag
Browse files Browse the repository at this point in the history
feat(cookies): feature flag
  • Loading branch information
Ealenn authored Jul 17, 2022
2 parents 621effb + 554d6d0 commit a05960b
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 20 deletions.
21 changes: 11 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,17 @@ Available:

## <a name='Configuration'></a>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__HEADER | application.enable.header | --enable:header | `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__COOKIES | application.enable.cookies | --enable:cookies | `true` |
| ENABLE__HEADER | application.enable.header | --enable:header | `true` |
| ENABLE__ENVIRONMENT | application.enable.environment | --enable:environment | `true` |
| ENABLE__FILE | application.enable.file | --enable:file | `true` |

## <a name='UseEcho-Server'></a>Use Echo-Server

Expand Down
1 change: 1 addition & 0 deletions docs/pages/configuration/feature-toggle.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This configuration is used to deactivate some elements in the response.
| 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__COOKIES | application.enable.cookies | --enable:cookies | `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` |
3 changes: 2 additions & 1 deletion src/global.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"host": true,
"http": true,
"request": true,
"cookies": true,
"environment": true,
"file": true,
"header": true
Expand Down Expand Up @@ -55,4 +56,4 @@
"header": "x-echo-file"
}
}
}
}
25 changes: 18 additions & 7 deletions src/response/request.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
const config = require('../nconf');

module.exports = (req) => config.get('enable:request') ? {
params: req.params,
query: req.query,
cookies: req.cookies,
body: req.body,
headers: req.headers
} : undefined;
module.exports = (req) => {
if (config.get('enable:request')) {
const isCookiesEnabled = config.get('enable:cookies');
if (!isCookiesEnabled) {
delete req.headers["cookie"];
}

return {
params: req.params,
query: req.query,
cookies: isCookiesEnabled ? req.cookies : [],
body: req.body,
headers: req.headers
};
} else {
return undefined
}
}
25 changes: 24 additions & 1 deletion test/cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,27 @@ describe('Request with Cookies', function () {
})
.expect(200, done);
});
});
});

describe('Request with Cookies but disabled in configuration', function () {
var server;
beforeEach(function () {
require('../src/nconf').set('enable:cookies', false);
server = require('../src/app');
});
afterEach(function () {
require('../src/nconf').set('enable:cookies', true);
server.close();
});

it('GET', function test(done) {
request(server)
.get('/')
.set('Cookie', ['testOne=valueOne;testTwo=valueTwo'])
.send()
.expect(function (res) {
assert.deepEqual(res.body.request.cookies, []);
})
.expect(200, done);
});
});
32 changes: 31 additions & 1 deletion test/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ describe('Headers', function () {
afterEach(function () {
server.close();
});
it('GET (cookie)', function test(done) {
request(server)
.get('/')
.set('cookie', 'key=value')
.expect(function (res) {
assert.strictEqual(res.body.request.headers['cookie'], 'key=value')
})
.expect(200, done);
});
it('GET', function test(done) {
request(server)
.get('/')
Expand Down Expand Up @@ -56,4 +65,25 @@ describe('Headers', function () {
})
.expect(200, done);
});
});
});

describe('Headers', function () {
var server;
beforeEach(function () {
require('../src/nconf').set('enable:cookies', false);
server = require('../src/app');
});
afterEach(function () {
require('../src/nconf').set('enable:cookies', true);
server.close();
});
it('GET', function test(done) {
request(server)
.get('/')
.set('cookie', 'key=value')
.expect(function (res) {
assert.deepEqual(res.body.request.headers['cookie'], undefined)
})
.expect(200, done);
});
});

0 comments on commit a05960b

Please sign in to comment.