From 882b4ea0001d0e764a55e07ec552e965a72210a2 Mon Sep 17 00:00:00 2001 From: kobelb Date: Thu, 1 Oct 2020 12:57:57 -0700 Subject: [PATCH] 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... --- .../elasticsearch/client/client_config.test.ts | 14 ++++++++++++++ .../elasticsearch/client/cluster_client.test.ts | 16 +++++++++++----- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/core/server/elasticsearch/client/client_config.test.ts b/src/core/server/elasticsearch/client/client_config.test.ts index 675d8840e7118..476a1d416b78a 100644 --- a/src/core/server/elasticsearch/client/client_config.test.ts +++ b/src/core/server/elasticsearch/client/client_config.test.ts @@ -19,6 +19,7 @@ import { duration } from 'moment'; import { ElasticsearchClientConfig, parseClientOptions } from './client_config'; +import { KIBANA_HEADERS } from '../kibana_headers'; const createConfig = ( parts: Partial = {} @@ -36,6 +37,18 @@ const createConfig = ( }; describe('parseClientOptions', () => { + it('includes `KIBANA_HEADERS`', () => { + const config = createConfig({}); + + expect(parseClientOptions(config, false)).toEqual( + expect.objectContaining({ + headers: { + ...KIBANA_HEADERS, + }, + }) + ); + }); + describe('basic options', () => { it('`customHeaders` option', () => { const config = createConfig({ @@ -48,6 +61,7 @@ describe('parseClientOptions', () => { expect(parseClientOptions(config, false)).toEqual( expect.objectContaining({ headers: { + ...KIBANA_HEADERS, foo: 'bar', hello: 'dolly', }, diff --git a/src/core/server/elasticsearch/client/cluster_client.test.ts b/src/core/server/elasticsearch/client/cluster_client.test.ts index e35d9962e9e7e..4f879a26604b2 100644 --- a/src/core/server/elasticsearch/client/cluster_client.test.ts +++ b/src/core/server/elasticsearch/client/cluster_client.test.ts @@ -24,6 +24,7 @@ import { GetAuthHeaders } from '../../http'; import { elasticsearchClientMock } from './mocks'; import { ClusterClient } from './cluster_client'; import { ElasticsearchClientConfig } from './client_config'; +import { KIBANA_HEADERS } from '../kibana_headers'; const createConfig = ( parts: Partial = {} @@ -127,7 +128,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ - headers: { foo: 'bar', 'x-opaque-id': expect.any(String) }, + headers: { ...KIBANA_HEADERS, foo: 'bar', 'x-opaque-id': expect.any(String) }, }); }); @@ -147,7 +148,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ - headers: { authorization: 'auth', 'x-opaque-id': expect.any(String) }, + headers: { ...KIBANA_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) }, }); }); @@ -171,7 +172,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ - headers: { authorization: 'auth', 'x-opaque-id': expect.any(String) }, + headers: { ...KIBANA_HEADERS, authorization: 'auth', 'x-opaque-id': expect.any(String) }, }); }); @@ -193,6 +194,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ headers: { + ...KIBANA_HEADERS, foo: 'bar', hello: 'dolly', 'x-opaque-id': expect.any(String), @@ -214,6 +216,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ headers: { + ...KIBANA_HEADERS, 'x-opaque-id': 'my-fake-id', }, }); @@ -239,6 +242,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ headers: { + ...KIBANA_HEADERS, foo: 'auth', hello: 'dolly', 'x-opaque-id': expect.any(String), @@ -266,6 +270,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ headers: { + ...KIBANA_HEADERS, foo: 'request', hello: 'dolly', 'x-opaque-id': expect.any(String), @@ -292,6 +297,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ headers: { + ...KIBANA_HEADERS, 'x-opaque-id': 'from request', }, }); @@ -315,7 +321,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ - headers: { authorization: 'auth' }, + headers: { ...KIBANA_HEADERS, authorization: 'auth' }, }); }); @@ -339,7 +345,7 @@ describe('ClusterClient', () => { expect(scopedClient.child).toHaveBeenCalledTimes(1); expect(scopedClient.child).toHaveBeenCalledWith({ - headers: { foo: 'bar' }, + headers: { ...KIBANA_HEADERS, foo: 'bar' }, }); }); });