-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Route tags #37344
Route tags #37344
Changes from 7 commits
60e9a55
c0bf094
5f1c8b8
7c94dc4
d8e9b66
555dafe
360da87
d733454
241620c
e51a2fd
5128c9b
b260ee4
277391a
e25a0da
89de831
c5909de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,7 @@ | |
import { get } from 'lodash'; | ||
import { DiscoveredPlugin, PluginName } from '../../server'; | ||
import { UiSettingsState } from '../ui_settings'; | ||
import { deepFreeze } from '../utils/deep_freeze'; | ||
import { deepFreeze } from '../../utils/'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove trailing slash for import consistency. |
||
import { Capabilities } from '..'; | ||
|
||
/** @public */ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { Request, ResponseToolkit } from 'hapi'; | ||
import { merge } from 'lodash'; | ||
|
||
type DeepPartial<T> = T extends any[] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @elastic/kibana-platform would be useful to declare some type helpers on project level or add a ready toolkit. |
||
? DeepPartialArray<T[number]> | ||
: T extends object | ||
? DeepPartialObject<T> | ||
: T; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-empty-interface | ||
interface DeepPartialArray<T> extends Array<DeepPartial<T>> {} | ||
|
||
type DeepPartialObject<T> = { [P in keyof T]+?: DeepPartial<T[P]> }; | ||
|
||
function createRawRequestMock(customization: DeepPartial<Request> = {}) { | ||
return merge( | ||
{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using 2 anonymous objects here is redundant, so you can remove the first one: return merge(
{
headers: {},
path: '/',
route: { settings: {} },
raw: {
req: {
url: '/',
},
},
},
customization
) as Request; There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
{ | ||
headers: {}, | ||
path: '/', | ||
route: { settings: {} }, | ||
raw: { | ||
req: { | ||
url: '/', | ||
}, | ||
}, | ||
}, | ||
customization | ||
) as Request; | ||
} | ||
|
||
function createRawResponseToolkitMock(customization: DeepPartial<ResponseToolkit> = {}) { | ||
return merge({}, customization) as ResponseToolkit; | ||
} | ||
|
||
export const httpServerMock = { | ||
createRawRequest: createRawRequestMock, | ||
createRawResponseToolkit: createRawResponseToolkitMock, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ import { HttpConfig, Router } from '.'; | |
import { loggingServiceMock } from '../logging/logging_service.mock'; | ||
import { HttpServer } from './http_server'; | ||
import { KibanaRequest } from './router'; | ||
import { httpServerMock } from './http_server.mocks'; | ||
|
||
const chance = new Chance(); | ||
|
||
|
@@ -678,30 +679,18 @@ test('#getBasePathFor() is based on server base path', async () => { | |
}); | ||
|
||
test('#setBasePathFor() cannot be set twice for one request', async () => { | ||
const incomingMessage = { | ||
url: '/', | ||
}; | ||
const kibanaRequestFactory = { | ||
from() { | ||
return KibanaRequest.from( | ||
{ | ||
headers: {}, | ||
path: '/', | ||
raw: { | ||
req: incomingMessage, | ||
}, | ||
} as any, | ||
undefined | ||
); | ||
return KibanaRequest.from(httpServerMock.createRawRequest(), undefined); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typically, passing return KibanaRequest.from(httpServerMock.createRawRequest()); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Yeah, it seems we should do that. And probably mark it as There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. there is no way to make a method as truly |
||
}, | ||
}; | ||
jest.doMock('./router/request', () => ({ | ||
KibanaRequest: jest.fn(() => kibanaRequestFactory), | ||
})); | ||
|
||
const { setBasePathFor } = await server.setup(config); | ||
|
||
const setPath = () => setBasePathFor(kibanaRequestFactory.from(), '/path'); | ||
const req = kibanaRequestFactory.from(); | ||
const setPath = () => setBasePathFor(req, '/path'); | ||
|
||
setPath(); | ||
expect(setPath).toThrowErrorMatchingInlineSnapshot( | ||
|
@@ -739,7 +728,9 @@ test('Should support disabling auth for a route', async () => { | |
const { registerAuth, registerRouter, server: innerServer } = await server.setup(config); | ||
|
||
const router = new Router(''); | ||
router.get({ path: '/', validate: false, authRequired: false }, async (req, res) => res.ok({})); | ||
router.get({ path: '/', validate: false, options: { authRequired: false } }, async (req, res) => | ||
mshustov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res.ok({}) | ||
); | ||
registerRouter(router); | ||
const authenticate = jest.fn(); | ||
await registerAuth(authenticate, cookieOptions); | ||
|
@@ -774,7 +765,7 @@ describe('#auth.isAuthenticated()', () => { | |
const { registerAuth, registerRouter, server: innerServer, auth } = await server.setup(config); | ||
|
||
const router = new Router(''); | ||
router.get({ path: '/', validate: false, authRequired: false }, async (req, res) => | ||
router.get({ path: '/', validate: false, options: { authRequired: false } }, async (req, res) => | ||
mshustov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res.ok({ isAuthenticated: auth.isAuthenticated(req) }) | ||
); | ||
registerRouter(router); | ||
|
@@ -791,7 +782,7 @@ describe('#auth.isAuthenticated()', () => { | |
const { registerRouter, server: innerServer, auth } = await server.setup(config); | ||
|
||
const router = new Router(''); | ||
router.get({ path: '/', validate: false, authRequired: false }, async (req, res) => | ||
router.get({ path: '/', validate: false, options: { authRequired: false } }, async (req, res) => | ||
mshustov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res.ok({ isAuthenticated: auth.isAuthenticated(req) }) | ||
); | ||
registerRouter(router); | ||
|
@@ -840,7 +831,7 @@ describe('#auth.get()', () => { | |
const { registerRouter, registerAuth, server: innerServer, auth } = await server.setup(config); | ||
await registerAuth(authenticate, cookieOptions); | ||
const router = new Router(''); | ||
router.get({ path: '/', validate: false, authRequired: false }, async (req, res) => | ||
router.get({ path: '/', validate: false, options: { authRequired: false } }, async (req, res) => | ||
mshustov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
res.ok(auth.get(req)) | ||
); | ||
|
||
|
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.
Remove trailing slash for import consistency.
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.
let's introduce linter rule explicitly if it's important. imo it doesn't make sense to perform manual check on every PR
related #36829 (comment)