-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[http] explicitly create the server listener #183591
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
pgayvallet
merged 8 commits into
elastic:main
from
pgayvallet:kbn-7104-prelim-change-listener-config
May 21, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
0d53e16
[http] explicitly create the server listener
pgayvallet 870849d
fix health server usages
pgayvallet 38c4ee6
fix more tests
pgayvallet 77ca04a
Merge remote-tracking branch 'upstream/main' into kbn-7104-prelim-cha…
pgayvallet b6c5318
review comments
pgayvallet d2aba9c
Merge remote-tracking branch 'upstream/main' into kbn-7104-prelim-cha…
pgayvallet 746d04d
Merge remote-tracking branch 'upstream/main' into kbn-7104-prelim-cha…
pgayvallet a045140
Merge remote-tracking branch 'upstream/main' into kbn-7104-prelim-cha…
pgayvallet 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
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
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
47 changes: 47 additions & 0 deletions
47
packages/kbn-server-http-tools/src/get_listener.test.mocks.ts
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 |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| /* | ||
| * 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 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 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| export const getServerTLSOptionsMock = jest.fn(); | ||
|
|
||
| jest.doMock('./get_tls_options', () => { | ||
| const actual = jest.requireActual('./get_tls_options'); | ||
| return { | ||
| ...actual, | ||
| getServerTLSOptions: getServerTLSOptionsMock, | ||
| }; | ||
| }); | ||
|
|
||
| export const createHttpServerMock = jest.fn(() => { | ||
| return { | ||
| on: jest.fn(), | ||
| setTimeout: jest.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| jest.doMock('http', () => { | ||
| const actual = jest.requireActual('http'); | ||
| return { | ||
| ...actual, | ||
| createServer: createHttpServerMock, | ||
| }; | ||
| }); | ||
|
|
||
| export const createHttpsServerMock = jest.fn(() => { | ||
| return { | ||
| on: jest.fn(), | ||
| setTimeout: jest.fn(), | ||
| }; | ||
| }); | ||
|
|
||
| jest.doMock('https', () => { | ||
| const actual = jest.requireActual('https'); | ||
| return { | ||
| ...actual, | ||
| createServer: createHttpsServerMock, | ||
| }; | ||
| }); |
139 changes: 139 additions & 0 deletions
139
packages/kbn-server-http-tools/src/get_listener.test.ts
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 |
|---|---|---|
| @@ -0,0 +1,139 @@ | ||
| /* | ||
| * 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 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 or the Server | ||
| * Side Public License, v 1. | ||
| */ | ||
|
|
||
| import { | ||
| getServerTLSOptionsMock, | ||
| createHttpServerMock, | ||
| createHttpsServerMock, | ||
| } from './get_listener.test.mocks'; | ||
| import moment from 'moment'; | ||
| import { ByteSizeValue } from '@kbn/config-schema'; | ||
| import type { IHttpConfig } from './types'; | ||
| import { getServerListener } from './get_listener'; | ||
|
|
||
| const createConfig = (parts: Partial<IHttpConfig>): IHttpConfig => ({ | ||
| host: 'localhost', | ||
| port: 5601, | ||
| socketTimeout: 120000, | ||
| keepaliveTimeout: 120000, | ||
| payloadTimeout: 20000, | ||
| shutdownTimeout: moment.duration(30, 'seconds'), | ||
| maxPayload: ByteSizeValue.parse('1048576b'), | ||
| ...parts, | ||
| cors: { | ||
| enabled: false, | ||
| allowCredentials: false, | ||
| allowOrigin: ['*'], | ||
| ...parts.cors, | ||
| }, | ||
| ssl: { | ||
| enabled: false, | ||
| ...parts.ssl, | ||
| }, | ||
| restrictInternalApis: false, | ||
| }); | ||
|
|
||
| describe('getServerListener', () => { | ||
| beforeEach(() => { | ||
| getServerTLSOptionsMock.mockReset(); | ||
| createHttpServerMock.mockClear(); | ||
| createHttpsServerMock.mockClear(); | ||
| }); | ||
|
|
||
| describe('when TLS is enabled', () => { | ||
| it('calls getServerTLSOptions with the correct parameters', () => { | ||
| const config = createConfig({ ssl: { enabled: true } }); | ||
|
|
||
| getServerListener(config); | ||
|
|
||
| expect(getServerTLSOptionsMock).toHaveBeenCalledTimes(1); | ||
| expect(getServerTLSOptionsMock).toHaveBeenCalledWith(config.ssl); | ||
| }); | ||
|
|
||
| it('calls https.createServer with the correct parameters', () => { | ||
| const config = createConfig({ ssl: { enabled: true } }); | ||
|
|
||
| getServerTLSOptionsMock.mockReturnValue({ stub: true }); | ||
|
|
||
| getServerListener(config); | ||
|
|
||
| expect(createHttpsServerMock).toHaveBeenCalledTimes(1); | ||
| expect(createHttpsServerMock).toHaveBeenCalledWith({ | ||
| stub: true, | ||
| keepAliveTimeout: config.keepaliveTimeout, | ||
| }); | ||
| }); | ||
|
|
||
| it('properly configures the listener', () => { | ||
| const config = createConfig({ ssl: { enabled: true } }); | ||
| const server = getServerListener(config); | ||
|
|
||
| expect(server.setTimeout).toHaveBeenCalledTimes(1); | ||
| expect(server.setTimeout).toHaveBeenCalledWith(config.socketTimeout); | ||
|
|
||
| expect(server.on).toHaveBeenCalledTimes(2); | ||
| expect(server.on).toHaveBeenCalledWith('clientError', expect.any(Function)); | ||
| expect(server.on).toHaveBeenCalledWith('timeout', expect.any(Function)); | ||
| }); | ||
|
|
||
| it('returns the https server', () => { | ||
| const config = createConfig({ ssl: { enabled: true } }); | ||
|
|
||
| const server = getServerListener(config); | ||
|
|
||
| const expectedServer = createHttpsServerMock.mock.results[0].value; | ||
|
|
||
| expect(server).toBe(expectedServer); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when TLS is disabled', () => { | ||
| it('does not call getServerTLSOptions', () => { | ||
| const config = createConfig({ ssl: { enabled: false } }); | ||
|
|
||
| getServerListener(config); | ||
|
|
||
| expect(getServerTLSOptionsMock).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('calls http.createServer with the correct parameters', () => { | ||
| const config = createConfig({ ssl: { enabled: false } }); | ||
|
|
||
| getServerTLSOptionsMock.mockReturnValue({ stub: true }); | ||
|
|
||
| getServerListener(config); | ||
|
|
||
| expect(createHttpServerMock).toHaveBeenCalledTimes(1); | ||
| expect(createHttpServerMock).toHaveBeenCalledWith({ | ||
| keepAliveTimeout: config.keepaliveTimeout, | ||
| }); | ||
| }); | ||
|
|
||
| it('properly configures the listener', () => { | ||
| const config = createConfig({ ssl: { enabled: false } }); | ||
| const server = getServerListener(config); | ||
|
|
||
| expect(server.setTimeout).toHaveBeenCalledTimes(1); | ||
| expect(server.setTimeout).toHaveBeenCalledWith(config.socketTimeout); | ||
|
|
||
| expect(server.on).toHaveBeenCalledTimes(2); | ||
| expect(server.on).toHaveBeenCalledWith('clientError', expect.any(Function)); | ||
| expect(server.on).toHaveBeenCalledWith('timeout', expect.any(Function)); | ||
| }); | ||
|
|
||
| it('returns the http server', () => { | ||
| const config = createConfig({ ssl: { enabled: false } }); | ||
|
|
||
| const server = getServerListener(config); | ||
|
|
||
| const expectedServer = createHttpServerMock.mock.results[0].value; | ||
|
|
||
| expect(server).toBe(expectedServer); | ||
| }); | ||
| }); | ||
| }); |
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.
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.
The root
createServerfunction is even simplified, because the options returned bycreateServerOptionsnow contain everything.Note: I did not delete the function because it still provides a good isolation and separation of concern.