Skip to content

Commit 882b4ea

Browse files
committed
Starting to add unit tests...
We currently allow end-users to set whatever headers they'd like to be forwarded to Elasticsearch with `elasticsearch.customHeaders` and `elasticsearch.requestHeadersWhitelist`. This is potentially problematic with us always specifying `User-Agent: kibana` as it could interfere with what our end-users have done...
1 parent 40e9f7c commit 882b4ea

2 files changed

Lines changed: 25 additions & 5 deletions

File tree

src/core/server/elasticsearch/client/client_config.test.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import { duration } from 'moment';
2121
import { ElasticsearchClientConfig, parseClientOptions } from './client_config';
22+
import { KIBANA_HEADERS } from '../kibana_headers';
2223

2324
const createConfig = (
2425
parts: Partial<ElasticsearchClientConfig> = {}
@@ -36,6 +37,18 @@ const createConfig = (
3637
};
3738

3839
describe('parseClientOptions', () => {
40+
it('includes `KIBANA_HEADERS`', () => {
41+
const config = createConfig({});
42+
43+
expect(parseClientOptions(config, false)).toEqual(
44+
expect.objectContaining({
45+
headers: {
46+
...KIBANA_HEADERS,
47+
},
48+
})
49+
);
50+
});
51+
3952
describe('basic options', () => {
4053
it('`customHeaders` option', () => {
4154
const config = createConfig({
@@ -48,6 +61,7 @@ describe('parseClientOptions', () => {
4861
expect(parseClientOptions(config, false)).toEqual(
4962
expect.objectContaining({
5063
headers: {
64+
...KIBANA_HEADERS,
5165
foo: 'bar',
5266
hello: 'dolly',
5367
},

src/core/server/elasticsearch/client/cluster_client.test.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import { GetAuthHeaders } from '../../http';
2424
import { elasticsearchClientMock } from './mocks';
2525
import { ClusterClient } from './cluster_client';
2626
import { ElasticsearchClientConfig } from './client_config';
27+
import { KIBANA_HEADERS } from '../kibana_headers';
2728

2829
const createConfig = (
2930
parts: Partial<ElasticsearchClientConfig> = {}
@@ -127,7 +128,7 @@ describe('ClusterClient', () => {
127128

128129
expect(scopedClient.child).toHaveBeenCalledTimes(1);
129130
expect(scopedClient.child).toHaveBeenCalledWith({
130-
headers: { foo: 'bar', 'x-opaque-id': expect.any(String) },
131+
headers: { ...KIBANA_HEADERS, foo: 'bar', 'x-opaque-id': expect.any(String) },
131132
});
132133
});
133134

@@ -147,7 +148,7 @@ describe('ClusterClient', () => {
147148

148149
expect(scopedClient.child).toHaveBeenCalledTimes(1);
149150
expect(scopedClient.child).toHaveBeenCalledWith({
150-
headers: { authorization: 'auth', 'x-opaque-id': expect.any(String) },
151+
headers: { ...KIBANA_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) },
151152
});
152153
});
153154

@@ -171,7 +172,7 @@ describe('ClusterClient', () => {
171172

172173
expect(scopedClient.child).toHaveBeenCalledTimes(1);
173174
expect(scopedClient.child).toHaveBeenCalledWith({
174-
headers: { authorization: 'auth', 'x-opaque-id': expect.any(String) },
175+
headers: { ...KIBANA_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) },
175176
});
176177
});
177178

@@ -193,6 +194,7 @@ describe('ClusterClient', () => {
193194
expect(scopedClient.child).toHaveBeenCalledTimes(1);
194195
expect(scopedClient.child).toHaveBeenCalledWith({
195196
headers: {
197+
...KIBANA_HEADERS,
196198
foo: 'bar',
197199
hello: 'dolly',
198200
'x-opaque-id': expect.any(String),
@@ -214,6 +216,7 @@ describe('ClusterClient', () => {
214216
expect(scopedClient.child).toHaveBeenCalledTimes(1);
215217
expect(scopedClient.child).toHaveBeenCalledWith({
216218
headers: {
219+
...KIBANA_HEADERS,
217220
'x-opaque-id': 'my-fake-id',
218221
},
219222
});
@@ -239,6 +242,7 @@ describe('ClusterClient', () => {
239242
expect(scopedClient.child).toHaveBeenCalledTimes(1);
240243
expect(scopedClient.child).toHaveBeenCalledWith({
241244
headers: {
245+
...KIBANA_HEADERS,
242246
foo: 'auth',
243247
hello: 'dolly',
244248
'x-opaque-id': expect.any(String),
@@ -266,6 +270,7 @@ describe('ClusterClient', () => {
266270
expect(scopedClient.child).toHaveBeenCalledTimes(1);
267271
expect(scopedClient.child).toHaveBeenCalledWith({
268272
headers: {
273+
...KIBANA_HEADERS,
269274
foo: 'request',
270275
hello: 'dolly',
271276
'x-opaque-id': expect.any(String),
@@ -292,6 +297,7 @@ describe('ClusterClient', () => {
292297
expect(scopedClient.child).toHaveBeenCalledTimes(1);
293298
expect(scopedClient.child).toHaveBeenCalledWith({
294299
headers: {
300+
...KIBANA_HEADERS,
295301
'x-opaque-id': 'from request',
296302
},
297303
});
@@ -315,7 +321,7 @@ describe('ClusterClient', () => {
315321

316322
expect(scopedClient.child).toHaveBeenCalledTimes(1);
317323
expect(scopedClient.child).toHaveBeenCalledWith({
318-
headers: { authorization: 'auth' },
324+
headers: { ...KIBANA_HEADERS, authorization: 'auth' },
319325
});
320326
});
321327

@@ -339,7 +345,7 @@ describe('ClusterClient', () => {
339345

340346
expect(scopedClient.child).toHaveBeenCalledTimes(1);
341347
expect(scopedClient.child).toHaveBeenCalledWith({
342-
headers: { foo: 'bar' },
348+
headers: { ...KIBANA_HEADERS, foo: 'bar' },
343349
});
344350
});
345351
});

0 commit comments

Comments
 (0)