From 0abe291f54aa9fe0b91ba73b0b4ea4f5b10c04d8 Mon Sep 17 00:00:00 2001 From: "troy.molsberry@mixmode.ai" Date: Wed, 19 Oct 2022 14:54:50 -0700 Subject: [PATCH 1/3] issue #291 mutability of headers Signed-off-by: troy.molsberry@mixmode.ai --- lib/Connection.js | 2 +- test/unit/connection.test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) 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); From 2dc7944c01ca9868ae5505260dde70860145594a Mon Sep 17 00:00:00 2001 From: "troy.molsberry@mixmode.ai" Date: Fri, 21 Oct 2022 09:46:01 -0700 Subject: [PATCH 2/3] rebase and update changelog.md Signed-off-by: troy.molsberry@mixmode.ai --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index ca8b55a4f..975b12bc6 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 +- mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291)) ### Security From 03302ebdbfeec4dedba85969102ebaf009f6266b Mon Sep 17 00:00:00 2001 From: "troy.molsberry@mixmode.ai" Date: Fri, 21 Oct 2022 09:51:14 -0700 Subject: [PATCH 3/3] proper case of changelog.md entry Signed-off-by: troy.molsberry@mixmode.ai --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 975b12bc6..e769dc497 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) ### Removed ### Fixed -- mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291)) +- Fix mutability of connection headers ([#291](https://github.com/opensearch-project/opensearch-js/issues/291)) ### Security