From a4dd9c0ab27561dd370523ba5e58464976dd0bd0 Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Wed, 20 Apr 2022 00:51:56 -0700 Subject: [PATCH 1/2] Fix escaped strings --- .../hooks/use_send_current_request_to_es/send_request_to_es.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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'); From 45e6d68fe04e2c66baddbd0e0da77e78da9d421d Mon Sep 17 00:00:00 2001 From: Muhammad Ibragimov Date: Wed, 20 Apr 2022 05:51:02 -0700 Subject: [PATCH 2/2] Add tests --- .../send_request_to_es.test.ts | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) 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); + }); + }); + }); });