-
Notifications
You must be signed in to change notification settings - Fork 8.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[GS] use the request's basePath when processing server-side result urls #76747
Merged
pgayvallet
merged 2 commits into
elastic:master
from
pgayvallet:kbn-xxx-fix-GS-server-basePath
Sep 8, 2020
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
x-pack/plugins/global_search/server/services/utils.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '/'); | ||
}, | ||
}; | ||
}; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it possible to reuse
basePath.prepend
here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK there is no way to only retrieve the request-specific (
requestScopePath
) part of the basePath via the service, asget
returns the 'whole' base path:kibana/src/core/server/http/base_path_service.ts
Lines 43 to 49 in bf04235
and
prepend
only allow to preprend using the server's base path, not a request-scoped basePath:kibana/src/core/server/http/base_path_service.ts
Lines 70 to 77 in bf04235
But maybe adding a new core API is better. I was surprised to be the first to have such need on the server-side TBH.
I can add something like
prependRequestPath(path: string, request: KibanaRequest)
to the server-sideBasePath
?