Skip to content

Commit

Permalink
Adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kobelb committed Oct 5, 2020
1 parent 37bd0a0 commit 51244dd
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 2 deletions.
21 changes: 20 additions & 1 deletion src/core/server/elasticsearch/client/client_config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const createConfig = (
};

describe('parseClientOptions', () => {
it('includes `KIBANA_HEADERS`', () => {
it('includes headers designing the HTTP request as originating from Kibana by default', () => {
const config = createConfig({});

expect(parseClientOptions(config, false)).toEqual(
Expand Down Expand Up @@ -69,6 +69,25 @@ describe('parseClientOptions', () => {
);
});

it('`customHeaders` take precedence to default kibana headers', () => {
const customHeader = {
[Object.keys(KIBANA_HEADERS)[0]]: 'foo',
};
const config = createConfig({
customHeaders: {
...customHeader,
},
});

expect(parseClientOptions(config, false)).toEqual(
expect.objectContaining({
headers: {
...customHeader,
},
})
);
});

it('`keepAlive` option', () => {
expect(parseClientOptions(createConfig({ keepAlive: true }), false)).toEqual(
expect.objectContaining({ agent: { keepAlive: true } })
Expand Down
46 changes: 46 additions & 0 deletions src/core/server/elasticsearch/client/cluster_client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,52 @@ describe('ClusterClient', () => {
});
});

it('respect the precedence of config headers over default headers', () => {
const headerKey = Object.keys(KIBANA_HEADERS)[0];
const config = createConfig({
customHeaders: {
[headerKey]: 'foo',
},
});
getAuthHeaders.mockReturnValue({});

const clusterClient = new ClusterClient(config, logger, getAuthHeaders);
const request = httpServerMock.createKibanaRequest();

clusterClient.asScoped(request);

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: {
[headerKey]: 'foo',
'x-opaque-id': expect.any(String),
},
});
});

it('respect the precedence of request headers over default headers', () => {
const headerKey = Object.keys(KIBANA_HEADERS)[0];
const config = createConfig({
requestHeadersWhitelist: [headerKey],
});
getAuthHeaders.mockReturnValue({});

const clusterClient = new ClusterClient(config, logger, getAuthHeaders);
const request = httpServerMock.createKibanaRequest({
headers: { [headerKey]: 'foo' },
});

clusterClient.asScoped(request);

expect(scopedClient.child).toHaveBeenCalledTimes(1);
expect(scopedClient.child).toHaveBeenCalledWith({
headers: {
[headerKey]: 'foo',
'x-opaque-id': expect.any(String),
},
});
});

it('respect the precedence of x-opaque-id header over config headers', () => {
const config = createConfig({
customHeaders: {
Expand Down
2 changes: 1 addition & 1 deletion src/core/server/elasticsearch/kibana_headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
*/

export const KIBANA_HEADERS = {
'X-elastic-product-origin': 'kibana',
'x-elastic-product-origin': 'kibana',
};

0 comments on commit 51244dd

Please sign in to comment.