[Console] Fix Kibana DevTool Copy as CURL does not url encode special chars in indice date math. (#130970)

* Fix cURL encoding for ES and Kibana requests. Add unit tests

* move changing global object in beforeEach

Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
This commit is contained in:
Hrant Muradyan 2022-05-05 10:18:27 +04:00 committed by GitHub
parent fe92ccfbc4
commit 880c3218b4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 51 additions and 5 deletions

View file

@ -31,3 +31,7 @@ POST /_sql?format=txt
"query": "SELECT prenom FROM claude_index WHERE prenom = 'claude' ",
"fetch_size": 1
}
GET <index_1-{now/d-2d}>,<index_1-{now/d-1d}>,<index_1-{now/d}>/_search?pretty
GET kbn:/api/spaces/space

View file

@ -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()
);
});

View file

@ -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, '/'))}`;
};