From 5c825d4d4e113246445ca2f14f5a5fb2b474aa75 Mon Sep 17 00:00:00 2001 From: Rudy Marchandise Date: Sat, 16 Jul 2022 20:12:59 +0200 Subject: [PATCH] feat(cookies): add some tests --- docs/pages/configuration/feature-toggle.md | 1 + src/response/request.js | 27 ++++++++-------------- test/cookie.js | 25 +++++++++++++++++++- 3 files changed, 34 insertions(+), 19 deletions(-) diff --git a/docs/pages/configuration/feature-toggle.md b/docs/pages/configuration/feature-toggle.md index aed81bb..883d001 100644 --- a/docs/pages/configuration/feature-toggle.md +++ b/docs/pages/configuration/feature-toggle.md @@ -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` | diff --git a/src/response/request.js b/src/response/request.js index 3f9e1f1..c74c69b 100644 --- a/src/response/request.js +++ b/src/response/request.js @@ -2,24 +2,15 @@ const config = require('../nconf'); module.exports = (req) => { if (config.get('enable:request')) { - if (config.get('enable:cookies')) { - return { - params: req.params, - query: req.query, - cookies: req.cookies, - body: req.body, - headers: req.headers - } - } else { - delete req.headers["cookie"]; - return { - params: req.params, - query: req.query, - body: req.body, - headers: req.headers - } - } + const isCookiesEnabled = config.get('enable:cookies'); + return { + params: req.params, + query: req.query, + cookies: isCookiesEnabled ? req.cookies : [], + body: req.body, + headers: req.headers + }; } else { - return undefined + return undefined } } diff --git a/test/cookie.js b/test/cookie.js index a0ed8c4..a70261c 100644 --- a/test/cookie.js +++ b/test/cookie.js @@ -66,4 +66,27 @@ describe('Request with Cookies', function () { }) .expect(200, done); }); -}); \ No newline at end of file +}); + +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); + }); +});