Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[APM] Ensure correct encoding of body in callApi #51600

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions x-pack/legacy/plugins/apm/public/services/__test__/callApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ describe('callApi', () => {
http = ({
get: jest.fn().mockReturnValue({
my_key: 'hello_world'
}),
post: jest.fn().mockReturnValue({
my_key: 'hello_world'
})
} as unknown) as HttpMock;

Expand All @@ -32,6 +35,54 @@ describe('callApi', () => {
clearCache();
});

describe('encoding', () => {
describe('when getting', () => {
it('it should correctly encode the request parameters', async () => {
await callApi(http, {
pathname: `/api/kibana`,
query: { start: '2010', end: '2011' }
});

expect(http.get).toHaveBeenCalledTimes(1);

expect(http.get).toHaveBeenCalledWith('/api/kibana', {
query: {
start: '2010',
end: '2011'
}
});
});
});
describe('when posting', () => {
it('it should correctly encode the request parameters', async () => {
await callApi(http, {
method: 'POST',
pathname: `/api/kibana`,
query: { start: '2010', end: '2011' },
body: {
foo: {
bar: 1
}
}
});

expect(http.post).toHaveBeenCalledTimes(1);

expect(http.post).toHaveBeenCalledWith('/api/kibana', {
query: {
start: '2010',
end: '2011'
},
body: JSON.stringify({
foo: {
bar: 1
}
})
});
});
});
});

describe('apm_debug', () => {
beforeEach(() => {
sessionStorage.setItem('apm_debug', 'true');
Expand Down
13 changes: 8 additions & 5 deletions x-pack/legacy/plugins/apm/public/services/rest/callApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,24 @@ import LRU from 'lru-cache';
import hash from 'object-hash';
import { HttpServiceBase, HttpFetchOptions } from 'kibana/public';

export type FetchOptions = HttpFetchOptions & {
export type FetchOptions = Omit<HttpFetchOptions, 'body'> & {
pathname: string;
forceCache?: boolean;
method?: string;
body?: object;
};

function fetchOptionsWithDebug(fetchOptions: FetchOptions) {
const debugEnabled =
sessionStorage.getItem('apm_debug') === 'true' &&
startsWith(fetchOptions.pathname, '/api/apm');

const isGet = !fetchOptions.method || fetchOptions.method === 'GET';
const isGetRequest = !fetchOptions.method || fetchOptions.method === 'GET';

const { body, ...rest } = fetchOptions;

// Need an empty body to pass route validation
const body = isGet
const bodyObject = isGetRequest
? {}
: {
body: JSON.stringify(
Expand All @@ -32,8 +35,8 @@ function fetchOptionsWithDebug(fetchOptions: FetchOptions) {
};

return {
...fetchOptions,
...body,
...rest,
...bodyObject,
query: {
...fetchOptions.query,
...(debugEnabled ? { _debug: true } : {})
Expand Down
4 changes: 2 additions & 2 deletions x-pack/legacy/plugins/apm/public/services/rest/ml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export async function startMLJob({
return callApi<StartedMLJobApiResponse>(http, {
method: 'POST',
pathname: `/api/ml/modules/setup/apm_transaction`,
body: JSON.stringify({
body: {
prefix: getMlPrefix(serviceName, transactionType),
groups,
indexPatternName: transactionIndices,
Expand All @@ -71,7 +71,7 @@ export async function startMLJob({
filter
}
}
})
}
});
}

Expand Down
2 changes: 1 addition & 1 deletion x-pack/legacy/plugins/apm/public/services/rest/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ export async function createWatch(id, watch) {
return callApi({
method: 'PUT',
pathname: `/api/watcher/watch/${id}`,
body: JSON.stringify({ type: 'json', id, watch })
body: { type: 'json', id, watch }
});
}