forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request elastic#7996 from epixa/7964-esclientheaders
Configurable headers for all elasticsearch requests Former-commit-id: bca4732
- Loading branch information
Showing
8 changed files
with
116 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/core_plugins/elasticsearch/lib/__tests__/set_headers.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import expect from 'expect.js'; | ||
import sinon from 'sinon'; | ||
import setHeaders from '../set_headers'; | ||
|
||
describe('plugins/elasticsearch', function () { | ||
describe('lib/set_headers', function () { | ||
it('throws if not given an object as the first argument', function () { | ||
const fn = () => setHeaders(null, {}); | ||
expect(fn).to.throwError(); | ||
}); | ||
|
||
it('throws if not given an object as the second argument', function () { | ||
const fn = () => setHeaders({}, null); | ||
expect(fn).to.throwError(); | ||
}); | ||
|
||
it('returns a new object', function () { | ||
const originalHeaders = {}; | ||
const newHeaders = {}; | ||
const returnedHeaders = setHeaders(originalHeaders, newHeaders); | ||
expect(returnedHeaders).not.to.be(originalHeaders); | ||
expect(returnedHeaders).not.to.be(newHeaders); | ||
}); | ||
|
||
it('returns object with newHeaders merged with originalHeaders', function () { | ||
const originalHeaders = { foo: 'bar' }; | ||
const newHeaders = { one: 'two' }; | ||
const returnedHeaders = setHeaders(originalHeaders, newHeaders); | ||
expect(returnedHeaders).to.eql({ foo: 'bar', one: 'two' }); | ||
}); | ||
|
||
it('returns object where newHeaders takes precedence for any matching keys', function () { | ||
const originalHeaders = { foo: 'bar' }; | ||
const newHeaders = { one: 'two', foo: 'notbar' }; | ||
const returnedHeaders = setHeaders(originalHeaders, newHeaders); | ||
expect(returnedHeaders).to.eql({ foo: 'notbar', one: 'two' }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { isPlainObject } from 'lodash'; | ||
|
||
export default function setHeaders(originalHeaders, newHeaders) { | ||
if (!isPlainObject(originalHeaders)) { | ||
throw new Error(`Expected originalHeaders to be an object, but ${typeof originalHeaders} given`); | ||
} | ||
if (!isPlainObject(newHeaders)) { | ||
throw new Error(`Expected newHeaders to be an object, but ${typeof newHeaders} given`); | ||
} | ||
|
||
return { | ||
...originalHeaders, | ||
...newHeaders | ||
}; | ||
} |