-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[Http] Added version header to unversioned, public routes #195464
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
Merged
jloleysens
merged 9 commits into
elastic:main
from
jloleysens:http/http-api-version-header-in-responses
Oct 9, 2024
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2b233c7
moved inject headers logic to common utils
jloleysens f03943a
added integration test
jloleysens d8da971
added integration test ii
jloleysens f42774d
added logic to inject public version to all public, unversioned route…
jloleysens c2ce52e
include version in response if validation failed too (versioned router)
jloleysens 7a79b00
include the versioned header if route validation failed or if custom …
jloleysens f57aec2
update comment
jloleysens fb859d1
added if
jloleysens ad619cb
added test for internal, unversioned validation
jloleysens 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,20 +7,22 @@ | |
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import { Router, type RouterOptions } from './router'; | ||
| import type { ResponseToolkit, ResponseObject } from '@hapi/hapi'; | ||
| import { loggingSystemMock } from '@kbn/core-logging-server-mocks'; | ||
| import { isConfigSchema, schema } from '@kbn/config-schema'; | ||
| import { createFooValidation } from './router.test.util'; | ||
| import { createRequestMock } from '@kbn/hapi-mocks/src/request'; | ||
| import { createFooValidation } from './router.test.util'; | ||
| import { Router, type RouterOptions } from './router'; | ||
| import type { RouteValidatorRequestAndResponses } from '@kbn/core-http-server'; | ||
|
|
||
| const mockResponse: any = { | ||
| const mockResponse = { | ||
| code: jest.fn().mockImplementation(() => mockResponse), | ||
| header: jest.fn().mockImplementation(() => mockResponse), | ||
| }; | ||
| const mockResponseToolkit: any = { | ||
| } as unknown as jest.Mocked<ResponseObject>; | ||
|
|
||
| const mockResponseToolkit = { | ||
| response: jest.fn().mockReturnValue(mockResponse), | ||
| }; | ||
| } as unknown as jest.Mocked<ResponseToolkit>; | ||
|
|
||
| const logger = loggingSystemMock.create().get(); | ||
| const enhanceWithContext = (fn: (...args: any[]) => any) => fn.bind(null, {}); | ||
|
|
@@ -132,6 +134,42 @@ describe('Router', () => { | |
| } | ||
| ); | ||
|
|
||
| it('adds versioned header v2023-10-31 to public, unversioned routes', async () => { | ||
| const router = new Router('', logger, enhanceWithContext, routerOptions); | ||
| router.post( | ||
| { | ||
| path: '/public', | ||
| options: { | ||
| access: 'public', | ||
| }, | ||
| validate: false, | ||
| }, | ||
| (context, req, res) => res.ok({ headers: { AAAA: 'test' } }) // with some fake headers | ||
| ); | ||
| router.post( | ||
| { | ||
| path: '/internal', | ||
| options: { | ||
| access: 'internal', | ||
| }, | ||
| validate: false, | ||
| }, | ||
| (context, req, res) => res.ok() | ||
| ); | ||
| const [{ handler: publicHandler }, { handler: internalHandler }] = router.getRoutes(); | ||
|
|
||
| await publicHandler(createRequestMock(), mockResponseToolkit); | ||
| expect(mockResponse.header).toHaveBeenCalledTimes(2); | ||
| const [first, second] = mockResponse.header.mock.calls | ||
| .concat() | ||
| .sort(([k1], [k2]) => k1.localeCompare(k2)); | ||
| expect(first).toEqual(['AAAA', 'test']); | ||
|
Contributor
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. AAAAAAAA |
||
| expect(second).toEqual(['elastic-api-version', '2023-10-31']); | ||
|
|
||
| await internalHandler(createRequestMock(), mockResponseToolkit); | ||
| expect(mockResponse.header).toHaveBeenCalledTimes(2); // no additional calls | ||
| }); | ||
|
|
||
| it('constructs lazily provided validations once (idempotency)', async () => { | ||
| const router = new Router('', logger, enhanceWithContext, routerOptions); | ||
| const { fooValidation } = testValidation; | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
27 changes: 0 additions & 27 deletions
27
...ore/http/core-http-router-server-internal/src/versioned_router/inject_response_headers.ts
This file was deleted.
Oops, something went wrong.
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Slightly better than
any, at least we can use a typed object in these tests