From 6e8d198c060d5dad17a0c0ab654b048bacd99779 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov <53621505+mibragimov@users.noreply.github.com> Date: Thu, 21 Apr 2022 14:25:32 +0500 Subject: [PATCH] [Dev Tools] Fix cat APIs returning as escaped string (#130638) * Fix escaped strings * Add tests Co-authored-by: Muhammad Ibragimov --- .../send_request_to_es.test.ts | 32 +++++++++++++++++++ .../send_request_to_es.ts | 2 +- 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.test.ts b/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.test.ts index a308cf150f804..8578e271f37b3 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.test.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.test.ts @@ -98,4 +98,36 @@ describe('sendRequestToES', () => { expect(mockedSendRequestToES).toHaveBeenCalledTimes(1); } }); + describe('successful response value', () => { + describe('with text', () => { + it('should return value with lines separated', async () => { + mockedSendRequestToES.mockResolvedValue('\ntest_index-1 [] \ntest_index-2 []\n'); + const response = await sendRequestToES({ + http: mockContextValue.services.http, + requests: [{ method: 'GET', url: 'test-1', data: [] }], + }); + + expect(response).toMatchInlineSnapshot(` + " + test_index-1 [] + test_index-2 [] + " + `); + expect(mockedSendRequestToES).toHaveBeenCalledTimes(1); + }); + }); + + describe('with parsed json', () => { + it('should stringify value', async () => { + mockedSendRequestToES.mockResolvedValue(JSON.stringify({ test: 'some value' })); + const response = await sendRequestToES({ + http: mockContextValue.services.http, + requests: [{ method: 'GET', url: 'test-2', data: [] }], + }); + + expect(typeof response).toBe('string'); + expect(mockedSendRequestToES).toHaveBeenCalledTimes(1); + }); + }); + }); }); diff --git a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.ts b/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.ts index 94e184748908c..451198aaf2d2b 100644 --- a/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.ts +++ b/src/plugins/console/public/application/hooks/use_send_current_request_to_es/send_request_to_es.ts @@ -93,7 +93,7 @@ export function sendRequestToES(args: EsRequestArgs): Promise if (body instanceof ArrayBuffer) { value = body; } else { - value = JSON.stringify(body, null, 2); + value = typeof body === 'string' ? body : JSON.stringify(body, null, 2); } const warnings = response.headers.get('warning');