diff --git a/CHANGELOG.md b/CHANGELOG.md index ca8b55a4f..e769dc497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Removed ### Fixed +- Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291)) ### Security diff --git a/lib/Connection.js b/lib/Connection.js index 2e4a5a547..bd71d2c53 100644 --- a/lib/Connection.js +++ b/lib/Connection.js @@ -217,7 +217,7 @@ class Connection { origin: url.origin, // https://github.com/elastic/elasticsearch-js/issues/843 port: url.port !== '' ? url.port : undefined, - headers: this.headers, + headers: Object.assign({}, this.headers), agent: this.agent, }; diff --git a/test/unit/connection.test.js b/test/unit/connection.test.js index cb0458dc9..d9f0268db 100644 --- a/test/unit/connection.test.js +++ b/test/unit/connection.test.js @@ -625,6 +625,37 @@ test('Custom headers for connection', (t) => { }); }); +test('mutability of connection headers', (t) => { + t.plan(3); + + function handler(req, res) { + t.match(req.headers, { + 'x-foo': 'bar', + }); + req.headers['x-custom-test'] = true; + res.end('ok'); + } + + buildServer(handler, ({ port }, server) => { + const connection = new Connection({ + url: new URL(`http://localhost:${port}`), + headers: { 'x-foo': 'bar' }, + }); + connection.request( + { + path: '/hello', + method: 'GET', + }, + (err) => { + t.error(err); + // should not update the default + t.same(connection.headers, { 'x-foo': 'bar' }); + server.stop(); + } + ); + }); +}); + // TODO: add a check that the response is not decompressed test('asStream set to true', (t) => { t.plan(2);