Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #291 mutability of headers #311

Merged
merged 3 commits into from
Oct 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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