[Data View Editor] Ensure proper encoding on request to find matching indices for a new Data View (#138587) (#138660)

(cherry picked from commit 12166e01a6)

Co-authored-by: Tim Sullivan <tsullivan@users.noreply.github.com>
This commit is contained in:
Kibana Machine 2022-08-11 15:32:57 -04:00 committed by GitHub
parent 2898258f17
commit fc9060a86b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 6 deletions

View file

@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import { getIndices, responseToItemArray } from './get_indices';
import { getIndices, getIndicesViaResolve, responseToItemArray } from './get_indices';
import { httpServiceMock } from '@kbn/core/public/mocks';
import { ResolveIndexResponseItemIndexAttrs } from '../types';
@ -39,6 +39,10 @@ const http = httpServiceMock.createStartContract();
http.get.mockResolvedValue(successfulResolveResponse);
describe('getIndices', () => {
afterEach(() => {
jest.clearAllMocks();
});
it('should work in a basic case', async () => {
const uncalledSearchClient = jest.fn();
const result = await getIndices({
@ -103,6 +107,23 @@ describe('getIndices', () => {
expect(responseToItemArray({}, getTags)).toEqual([]);
});
describe('getIndicesViaResolve', () => {
it('should encode the pattern for a working URI', async () => {
const spy = jest.spyOn(http, 'get');
const pattern = 'test-%';
await getIndicesViaResolve({
http,
pattern,
showAllIndices: true,
isRollupIndex: () => false,
});
expect(spy).toHaveBeenCalledWith(
'/internal/index-pattern-management/resolve_index/test-%25',
{ query: { expand_wildcards: 'all' } }
);
});
});
describe('errors', () => {
it('should handle thrown errors gracefully', async () => {
http.get.mockImplementationOnce(() => {

View file

@ -50,11 +50,15 @@ export const getIndicesViaResolve = async ({
pattern: string;
showAllIndices: boolean;
isRollupIndex: (indexName: string) => boolean;
}) =>
http
.get<ResolveIndexResponse>(`/internal/index-pattern-management/resolve_index/${pattern}`, {
query: showAllIndices ? { expand_wildcards: 'all' } : undefined,
})
}) => {
const encodedPattern = encodeURIComponent(pattern);
return http
.get<ResolveIndexResponse>(
`/internal/index-pattern-management/resolve_index/${encodedPattern}`,
{
query: showAllIndices ? { expand_wildcards: 'all' } : undefined,
}
)
.then((response) => {
if (!response) {
return [];
@ -62,6 +66,7 @@ export const getIndicesViaResolve = async ({
return responseToItemArray(response, getIndexTags(isRollupIndex));
}
});
};
export async function getIndices({
http,