diff --git a/src/plugins/console/public/application/models/sense_editor/__fixtures__/editor_input1.txt b/src/plugins/console/public/application/models/sense_editor/__fixtures__/editor_input1.txt index 398a0fdeab61f1..517f22bd8ad6aa 100644 --- a/src/plugins/console/public/application/models/sense_editor/__fixtures__/editor_input1.txt +++ b/src/plugins/console/public/application/models/sense_editor/__fixtures__/editor_input1.txt @@ -31,3 +31,7 @@ POST /_sql?format=txt "query": "SELECT prenom FROM claude_index WHERE prenom = 'claude' ", "fetch_size": 1 } + +GET ,,/_search?pretty + +GET kbn:/api/spaces/space \ No newline at end of file diff --git a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js index ff9d245f61275c..4751d3ca29863e 100644 --- a/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js +++ b/src/plugins/console/public/application/models/sense_editor/sense_editor.test.js @@ -10,6 +10,7 @@ import './sense_editor.test.mocks'; import $ from 'jquery'; import _ from 'lodash'; +import { URL } from 'url'; import { create } from './create'; import { XJson } from '@kbn/es-ui-shared-plugin/public'; @@ -19,6 +20,8 @@ const { collapseLiteralStrings } = XJson; describe('Editor', () => { let input; + let oldUrl; + let olldWindow; beforeEach(function () { // Set up our document body @@ -31,8 +34,19 @@ describe('Editor', () => { input = create(document.querySelector('#ConAppEditor')); $(input.getCoreEditor().getContainer()).show(); input.autocomplete._test.removeChangeListener(); + oldUrl = global.URL; + olldWindow = { ...global.window }; + global.URL = URL; + global.window = Object.create(window); + Object.defineProperty(window, 'location', { + value: { + origin: 'http://localhost:5620', + }, + }); }); afterEach(function () { + global.URL = oldUrl; + global.window = olldWindow; $(input.getCoreEditor().getContainer()).hide(); input.autocomplete._test.addChangeListener(); }); @@ -476,4 +490,20 @@ curl -XPOST "http://localhost:9200/_sql?format=txt" -H "kbn-xsrf: reporting" -H "fetch_size": 1 }'`.trim() ); + + multiReqCopyAsCurlTest( + 'with date math index', + editorInput1, + { start: { lineNumber: 35 }, end: { lineNumber: 35 } }, + ` + curl -XGET "http://localhost:9200/%3Cindex_1-%7Bnow%2Fd-2d%7D%3E%2C%3Cindex_1-%7Bnow%2Fd-1d%7D%3E%2C%3Cindex_1-%7Bnow%2Fd%7D%3E%2F_search?pretty" -H "kbn-xsrf: reporting"`.trim() + ); + + multiReqCopyAsCurlTest( + 'with Kibana API request', + editorInput1, + { start: { lineNumber: 37 }, end: { lineNumber: 37 } }, + ` +curl -XGET "http://localhost:5620/api/spaces/space" -H \"kbn-xsrf: reporting\"`.trim() + ); }); diff --git a/src/plugins/console/public/lib/es/es.ts b/src/plugins/console/public/lib/es/es.ts index 10d0ad95b0496f..5e22c78547b961 100644 --- a/src/plugins/console/public/lib/es/es.ts +++ b/src/plugins/console/public/lib/es/es.ts @@ -7,7 +7,7 @@ */ import type { HttpResponse, HttpSetup } from '@kbn/core/public'; -import { trimStart } from 'lodash'; +import { trimStart, trimEnd } from 'lodash'; import { API_BASE_PATH, KIBANA_API_PREFIX } from '../../../common/constants'; const esVersion: string[] = []; @@ -79,11 +79,23 @@ function getKibanaRequestUrl(path: string) { export function constructUrl(baseUri: string, path: string) { const kibanaRequestUrl = getKibanaRequestUrl(path); + let url = `${trimEnd(baseUri, '/')}/${trimStart(path, '/')}`; if (kibanaRequestUrl) { - return kibanaRequestUrl; + url = kibanaRequestUrl; } - baseUri = baseUri.replace(/\/+$/, ''); - path = path.replace(/^\/+/, ''); - return baseUri + '/' + path; + + const { origin, pathname, search } = new URL(url); + return `${origin}${encodePathname(pathname)}${search ?? ''}`; } + +const encodePathname = (path: string) => { + const decodedPath = new URLSearchParams(`path=${path}`).get('path') ?? ''; + + // Skip if it is valid + if (path === decodedPath) { + return path; + } + + return `/${encodeURIComponent(trimStart(decodedPath, '/'))}`; +};