Skip to content

Commit

Permalink
Starting to add unit tests...
Browse files Browse the repository at this point in the history
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...
  • Loading branch information
kobelb committed Oct 1, 2020
1 parent 40e9f7c commit 882b4ea
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
14 changes: 14 additions & 0 deletions src/core/server/elasticsearch/client/client_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import { duration } from 'moment';
import { ElasticsearchClientConfig, parseClientOptions } from './client_config';
import { KIBANA_HEADERS } from '../kibana_headers';

const createConfig = (
parts: Partial<ElasticsearchClientConfig> = {}
Expand All @@ -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({
Expand All @@ -48,6 +61,7 @@ describe('parseClientOptions', () => {
expect(parseClientOptions(config, false)).toEqual(
expect.objectContaining({
headers: {
...KIBANA_HEADERS,
foo: 'bar',
hello: 'dolly',
},
Expand Down
16 changes: 11 additions & 5 deletions src/core/server/elasticsearch/client/cluster_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ElasticsearchClientConfig> = {}
Expand Down Expand Up @@ -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) },
});
});

Expand All @@ -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) },
});
});

Expand All @@ -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) },
});
});

Expand All @@ -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),
Expand All @@ -214,6 +216,7 @@ describe('ClusterClient', () => {
expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: {
...KIBANA_HEADERS,
'x-opaque-id': 'my-fake-id',
},
});
Expand All @@ -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),
Expand Down Expand Up @@ -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),
Expand All @@ -292,6 +297,7 @@ describe('ClusterClient', () => {
expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: {
...KIBANA_HEADERS,
'x-opaque-id': 'from request',
},
});
Expand All @@ -315,7 +321,7 @@ describe('ClusterClient', () => {

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: { authorization: 'auth' },
headers: { ...KIBANA_HEADERS, authorization: 'auth' },
});
});

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

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: { foo: 'bar' },
headers: { ...KIBANA_HEADERS, foo: 'bar' },
});
});
});
Expand Down

0 comments on commit 882b4ea

Please sign in to comment.