[APM] Fix debug param issue (#19676)

* [APM] Fix debug param issue

* [APM] Fix _debug issue and add test
This commit is contained in:
Søren Louv-Jansen 2018-06-05 17:56:01 +02:00 committed by GitHub
parent a9011b32c1
commit a4285916dd
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 125 additions and 10 deletions

View file

@ -0,0 +1,25 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
export class SessionStorageMock {
store = {};
clear() {
this.store = {};
}
getItem(key) {
return this.store[key] || null;
}
setItem(key, value) {
this.store[key] = value.toString();
}
removeItem(key) {
delete this.store[key];
}
}

View file

@ -0,0 +1,79 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import * as kfetchModule from 'ui/kfetch';
import { SessionStorageMock } from './SessionStorageMock';
import { callApi } from '../rest';
describe('rest', () => {
let kfetchSpy;
beforeEach(() => {
kfetchSpy = jest.spyOn(kfetchModule, 'kfetch').mockResolvedValue({
my_key: 'hello world'
});
global.sessionStorage = new SessionStorageMock();
});
afterEach(() => {
kfetchSpy.mockClear();
});
describe('callApi', () => {
describe('debug param', () => {
describe('when apm_debug is true', () => {
beforeEach(() => {
sessionStorage.setItem('apm_debug', 'true');
});
it('should add debug param for APM endpoints', async () => {
await callApi({ pathname: `/api/apm/status/server` });
expect(kfetchSpy).toHaveBeenCalledWith(
{ pathname: '/api/apm/status/server', query: { _debug: true } },
{ camelcase: true }
);
});
it('should not add debug param for non-APM endpoints', async () => {
await callApi({ pathname: `/api/kibana` });
expect(kfetchSpy).toHaveBeenCalledWith(
{ pathname: '/api/kibana' },
{ camelcase: true }
);
});
});
});
describe('camelcase', () => {
it('camelcase param should be true by default', async () => {
const res = await callApi({ pathname: `/api/kibana` });
expect(kfetchSpy).toHaveBeenCalledWith(
{ pathname: '/api/kibana' },
{ camelcase: true }
);
expect(res).toEqual({ myKey: 'hello world' });
});
it('should respect settings', async () => {
const res = await callApi(
{ pathname: `/api/kibana` },
{ camelcase: false }
);
expect(kfetchSpy).toHaveBeenCalledWith(
{ pathname: '/api/kibana' },
{ camelcase: false }
);
expect(res).toEqual({ my_key: 'hello world' });
});
});
});
});

View file

@ -7,25 +7,36 @@
import 'isomorphic-fetch';
import { camelizeKeys } from 'humps';
import { kfetch } from 'ui/kfetch';
import { memoize, isEmpty, first } from 'lodash';
import { memoize, isEmpty, first, startsWith } from 'lodash';
import chrome from 'ui/chrome';
import { convertKueryToEsQuery } from './kuery';
import { getFromSavedObject } from 'ui/index_patterns/static_utils';
async function callApi(fetchOptions, kibanaOptions) {
function fetchOptionsWithDebug(fetchOptions) {
const debugEnabled =
sessionStorage.getItem('apm_debug') === 'true' &&
startsWith(fetchOptions.pathname, '/api/apm');
if (!debugEnabled) {
return fetchOptions;
}
return {
...fetchOptions,
query: {
...fetchOptions.query,
_debug: true
}
};
}
export async function callApi(fetchOptions, kibanaOptions) {
const combinedKibanaOptions = {
camelcase: true,
...kibanaOptions
};
const combinedFetchOptions = {
...fetchOptions,
query: {
...fetchOptions.query,
_debug: sessionStorage.getItem('apm_debug') || false
}
};
const combinedFetchOptions = fetchOptionsWithDebug(fetchOptions);
const res = await kfetch(combinedFetchOptions, combinedKibanaOptions);
return combinedKibanaOptions.camelcase ? camelizeKeys(res) : res;
}