Skip to content

Commit

Permalink
issue #291 mutability of headers (#311)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmtdm authored Oct 25, 2022
1 parent 78ffca6 commit 442bc56
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};

Expand Down
31 changes: 31 additions & 0 deletions test/unit/connection.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 442bc56

Please sign in to comment.