-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[http] explicitly create the server listener (#183591)
## Summary Related to #7104 Adapted from #183465 For `http2` support, we will have to change the way we configure the HAPI server to manually provide the listener instead of passing down the options for HAPI to create it. This PR prepares that work, by creating the `http` or `https` (`tls`) listener and passing it when creating the HAPI server instead of just passing the `tls` options. **Note:** no integration tests were added, because we already have the right coverage for both tls and non-tls mode, so any change of behavior introduced by the PR should be detectable by them.
- Loading branch information
1 parent
2c9a89e
commit db316ad
Showing
21 changed files
with
463 additions
and
170 deletions.
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
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
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
47 changes: 47 additions & 0 deletions
47
packages/kbn-server-http-tools/src/get_listener.test.mocks.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,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 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.