Skip to content

Commit

Permalink
GS: use the request's basePath when processing server-side result urls (
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Sep 8, 2020
1 parent 26d71c1 commit 766fd2b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 6 deletions.
4 changes: 2 additions & 2 deletions x-pack/plugins/global_search/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ export interface GlobalSearchProviderResult {
icon?: string;
/**
* The url associated with this result.
* This can be either an absolute url, a path relative to the basePath, or a structure specifying if the basePath should be prepended.
* This can be either an absolute url, a path relative to the incoming request's basePath, or a structure specifying if the basePath should be prepended.
*
* @example
* `result.url = 'https://kibana-instance:8080/base-path/app/my-app/my-result-type/id';`
* `result.url = '/app/my-app/my-result-type/id';`
* `result.url = { path: '/base-path/app/my-app/my-result-type/id', prependBasePath: false };`
* `result.url = { path: '/base-path/s/my-other-space/app/my-app/my-result-type/id', prependBasePath: false };`
*/
url: GlobalSearchProviderResultUrl;
/** the score of the result, from 1 (lowest) to 100 (highest) */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ describe('SearchService', () => {

beforeEach(() => {
service = new SearchService();
basePath = httpServiceMock.createBasePath();
basePath.prepend.mockImplementation((path) => `/base-path${path}`);
basePath = httpServiceMock.createBasePath('/base-path');
basePath.get.mockReturnValue('/base-path/s/space');
coreStart = coreMock.createStart();
licenseChecker = licenseCheckerMock.create();
});
Expand Down Expand Up @@ -283,7 +283,7 @@ describe('SearchService', () => {
expect(batch.results).toHaveLength(2);
expect(batch.results[0]).toEqual({
...resultA,
url: '/base-path/foo/bar',
url: '/base-path/s/space/foo/bar',
});
expect(batch.results[1]).toEqual({
...resultB,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { processProviderResult } from '../../common/process_result';
import { GlobalSearchConfigType } from '../config';
import { getContextFactory, GlobalSearchContextFactory } from './context';
import { GlobalSearchResultProvider, GlobalSearchFindOptions } from '../types';
import { getRequestBasePath } from './utils';

/** @public */
export interface SearchServiceSetup {
Expand Down Expand Up @@ -132,6 +133,7 @@ export class SearchService {
}

const context = this.contextFactory!(request);
const basePath = getRequestBasePath(request, this.basePath!);

const timeout$ = timer(this.config!.search_timeout.asMilliseconds()).pipe(map(mapToUndefined));
const aborted$ = options.aborted$ ? merge(options.aborted$, timeout$) : timeout$;
Expand All @@ -143,7 +145,7 @@ export class SearchService {
};

const processResult = (result: GlobalSearchProviderResult) =>
processProviderResult(result, this.basePath!);
processProviderResult(result, basePath);

const providersResults$ = [...this.providers.values()].map((provider) =>
provider.find(term, providerOptions, context).pipe(
Expand Down
34 changes: 34 additions & 0 deletions x-pack/plugins/global_search/server/services/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { httpServiceMock, httpServerMock } from '../../../../../src/core/server/mocks';
import { getRequestBasePath } from './utils';

describe('getRequestBasePath', () => {
let basePath: ReturnType<typeof httpServiceMock.createBasePath>;
let request: ReturnType<typeof httpServerMock.createKibanaRequest>;

beforeEach(() => {
basePath = httpServiceMock.createBasePath();
request = httpServerMock.createKibanaRequest();
});

it('return a IBasePath prepending the request basePath', () => {
basePath.get.mockReturnValue('/base-path/s/my-space');
const requestBasePath = getRequestBasePath(request, basePath);

const fullPath = requestBasePath.prepend('/app/dashboard/some-id');

expect(fullPath).toBe('/base-path/s/my-space/app/dashboard/some-id');

expect(basePath.get).toHaveBeenCalledTimes(1);
expect(basePath.get).toHaveBeenCalledWith(request);

expect(basePath.prepend).not.toHaveBeenCalled();
});
});

httpServiceMock.createBasePath();
20 changes: 20 additions & 0 deletions x-pack/plugins/global_search/server/services/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import type { IBasePath, KibanaRequest } from 'src/core/server';
import type { IBasePath as BasePathAccessor } from '../../common/utils';

export const getRequestBasePath = (
request: KibanaRequest,
basePath: IBasePath
): BasePathAccessor => {
const requestBasePath = basePath.get(request);
return {
prepend: (path) => {
return `${requestBasePath}/${path}`.replace(/\/{2,}/g, '/');
},
};
};

0 comments on commit 766fd2b

Please sign in to comment.