-
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
[Http] Router refactor #205502
[Http] Router refactor #205502
Changes from 17 commits
34bb576
f50d4cc
495b9e4
f5670a1
829f8c8
14bdbcb
9b8e30a
ebd8add
4da954e
eec58bd
b382675
cdb39be
6c55327
43be1ad
97d3375
d6f5e4e
427b619
0c4643c
8a446af
c96fd9b
532280b
bb2bf16
0829811
ac9c4bb
a4618b8
2a23a05
6010378
f71ab61
718d77b
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 |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { hapiMocks } from '@kbn/hapi-mocks'; | ||
import { validateHapiRequest } from './route'; | ||
import { createRouter } from './versioned_router/mocks'; | ||
import { loggingSystemMock } from '@kbn/core-logging-server-mocks'; | ||
import { Logger } from '@kbn/logging'; | ||
import { RouteValidator } from './validator'; | ||
import { schema } from '@kbn/config-schema'; | ||
import { Router } from './router'; | ||
import { RouteAccess } from '@kbn/core-http-server'; | ||
|
||
describe('validateHapiRequest', () => { | ||
let router: Router; | ||
let log: Logger; | ||
beforeEach(() => { | ||
router = createRouter(); | ||
log = loggingSystemMock.createLogger(); | ||
}); | ||
it('validates hapi requests and returns kibana requests: ok case', () => { | ||
const { ok, error } = validateHapiRequest(hapiMocks.createRequest({ payload: { ok: true } }), { | ||
log, | ||
routeInfo: { access: 'public', httpResource: false }, | ||
router, | ||
routeSchemas: RouteValidator.from({ body: schema.object({ ok: schema.literal(true) }) }), | ||
}); | ||
expect(ok?.body).toEqual({ ok: true }); | ||
expect(error).toBeUndefined(); | ||
expect(log.error).not.toHaveBeenCalled(); | ||
}); | ||
it('validates hapi requests and returns kibana requests: error case', () => { | ||
const { ok, error } = validateHapiRequest(hapiMocks.createRequest({ payload: { ok: false } }), { | ||
log, | ||
routeInfo: { access: 'public', httpResource: false }, | ||
router, | ||
routeSchemas: RouteValidator.from({ body: schema.object({ ok: schema.literal(true) }) }), | ||
}); | ||
expect(ok).toBeUndefined(); | ||
expect(error?.status).toEqual(400); | ||
expect(error?.payload).toMatch(/expected value to equal/); | ||
expect(log.error).toHaveBeenCalledTimes(1); | ||
expect(log.error).toHaveBeenCalledWith('400 Bad Request', { | ||
error: { message: '[request body.ok]: expected value to equal [true]' }, | ||
http: { request: { method: undefined, path: undefined }, response: { status_code: 400 } }, | ||
}); | ||
}); | ||
|
||
it('emits post validation events on the router', () => { | ||
const deps = { | ||
log, | ||
routeInfo: { access: 'public' as RouteAccess, httpResource: false }, | ||
router, | ||
routeSchemas: RouteValidator.from({ body: schema.object({ ok: schema.literal(true) }) }), | ||
}; | ||
{ | ||
const { ok, error } = validateHapiRequest( | ||
hapiMocks.createRequest({ payload: { ok: false } }), | ||
deps | ||
); | ||
expect(ok).toBeUndefined(); | ||
expect(error).toBeDefined(); | ||
expect(router.emitPostValidate).toHaveBeenCalledTimes(1); | ||
expect(router.emitPostValidate).toHaveBeenCalledWith(expect.any(Object), { | ||
deprecated: undefined, | ||
isInternalApiRequest: false, | ||
isPublicAccess: true, | ||
}); | ||
} | ||
{ | ||
const { ok, error } = validateHapiRequest( | ||
hapiMocks.createRequest({ payload: { ok: true } }), | ||
deps | ||
); | ||
expect(ok).toBeDefined(); | ||
expect(error).toBeUndefined(); | ||
expect(router.emitPostValidate).toHaveBeenCalledTimes(2); | ||
expect(router.emitPostValidate).toHaveBeenNthCalledWith(2, expect.any(Object), { | ||
deprecated: undefined, | ||
isInternalApiRequest: false, | ||
isPublicAccess: true, | ||
}); | ||
} | ||
}); | ||
}); |
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. 99% of the added logic in this file comes from (1) create better reusability of these lower level functionalities between router and versioned router (specifically, creating |
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.
This is new test coverage