From 1f58ea9f187de135678a7ca1745f5fc02f8cb0eb Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Wed, 12 Apr 2023 03:05:34 +0000 Subject: [PATCH] Fix command with query parameter rejected in DevTools (#3813) Signed-off-by: Hailong Cui (cherry picked from commit e12ae0b25e03958b3e53d8524d5d0dee36538fe1) Signed-off-by: github-actions[bot] # Conflicts: # CHANGELOG.md --- .../server/routes/api/console/proxy/create_handler.ts | 8 +++++--- .../routes/api/console/proxy/tests/query_string.test.ts | 8 ++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts index f46acbf6cb71..0a401ded813b 100644 --- a/src/plugins/console/server/routes/api/console/proxy/create_handler.ts +++ b/src/plugins/console/server/routes/api/console/proxy/create_handler.ts @@ -70,13 +70,15 @@ function getProxyHeaders(req: OpenSearchDashboardsRequest) { } function toUrlPath(path: string) { - let urlPath = `/${trimStart(path, '/')}`; + const FAKE_BASE = 'http://localhost'; + const urlWithFakeBase = new URL(`${FAKE_BASE}/${trimStart(path, '/')}`); // Appending pretty here to have OpenSearch do the JSON formatting, as doing // in JS can lead to data loss (7.0 will get munged into 7, thus losing indication of // measurement precision) - if (!urlPath.includes('?pretty')) { - urlPath += '?pretty=true'; + if (!urlWithFakeBase.searchParams.get('pretty')) { + urlWithFakeBase.searchParams.append('pretty', 'true'); } + const urlPath = urlWithFakeBase.href.replace(urlWithFakeBase.origin, ''); return urlPath; } diff --git a/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts b/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts index 105bce3bfad2..9aefea1182cc 100644 --- a/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts +++ b/src/plugins/console/server/routes/api/console/proxy/tests/query_string.test.ts @@ -82,6 +82,14 @@ describe('Console Proxy Route', () => { expect(args.path).toBe('/index/id?pretty=true'); }); }); + + describe(`contains query parameter`, () => { + it('adds slash to path before sending request', async () => { + await request('GET', '_cat/tasks?v'); + const [[args]] = opensearchClient.asCurrentUser.transport.request.mock.calls; + expect(args.path).toBe('/_cat/tasks?v=&pretty=true'); + }); + }); }); }); });