diff --git a/src/core/server/http/http_service.mock.ts b/src/core/server/http/http_service.mock.ts index 7dccacee8509af4..f13b6b2fbbf66f8 100644 --- a/src/core/server/http/http_service.mock.ts +++ b/src/core/server/http/http_service.mock.ts @@ -37,9 +37,11 @@ const createSetupContractMock = () => { get: jest.fn(), isAuthenticated: jest.fn(), }, + shouldListen: jest.fn(), createNewServer: async (cfg: Partial): Promise => ({} as HttpServerSetup), }; + setupContract.shouldListen.mockReturnValue(true); return setupContract; }; diff --git a/src/core/server/http/http_service.test.ts b/src/core/server/http/http_service.test.ts index 16f946ffcc7ae49..bc4acdb396cf469 100644 --- a/src/core/server/http/http_service.test.ts +++ b/src/core/server/http/http_service.test.ts @@ -193,8 +193,9 @@ test('returns http server contract on setup', async () => { })); const service = new HttpService({ configService, env, logger }); - const { createNewServer, ...setupHttpServer } = await service.setup(); + const { createNewServer, shouldListen, ...setupHttpServer } = await service.setup(); expect(createNewServer).toBeDefined(); + expect(shouldListen).toBeDefined(); expect(setupHttpServer).toEqual(httpServer); }); diff --git a/src/core/server/http/http_service.ts b/src/core/server/http/http_service.ts index fec3774e2f36640..6957d72855dc115 100644 --- a/src/core/server/http/http_service.ts +++ b/src/core/server/http/http_service.ts @@ -31,6 +31,7 @@ import { HttpsRedirectServer } from './https_redirect_server'; /** @public */ export interface HttpServiceSetup extends HttpServerSetup { createNewServer: (cfg: Partial) => Promise; + shouldListen: () => boolean; } /** @public */ export interface HttpServiceStart { @@ -78,7 +79,11 @@ export class HttpService implements CoreService) { const { port } = cfg; const config = await this.config$.pipe(first()).toPromise(); diff --git a/src/core/server/http/integration_tests/http_service.test.ts b/src/core/server/http/integration_tests/http_service.test.ts index 100efc4e2a607ae..469a9d9ec585a98 100644 --- a/src/core/server/http/integration_tests/http_service.test.ts +++ b/src/core/server/http/integration_tests/http_service.test.ts @@ -76,7 +76,7 @@ describe('http service', () => { .get(root, legacyUrl) .expect(200, 'ok from legacy server'); - expect(response.header['set-cookie']).toBe(undefined); + expect(response.header['set-cookie']).toHaveLength(1); }); it('Should pass associated auth state to Legacy platform', async () => { @@ -106,7 +106,7 @@ describe('http service', () => { expect(response.body.state).toEqual(user); expect(response.body.status).toEqual('authenticated'); - expect(response.header['set-cookie']).toBe(undefined); + expect(response.header['set-cookie']).toHaveLength(1); }); }); @@ -118,7 +118,7 @@ describe('http service', () => { afterEach(async () => await root.shutdown()); it('Should support passing request through to the route handler', async () => { - const router = new Router(''); + const router = new Router('/new-platform'); router.get({ path: '/', validate: false }, async (req, res) => res.ok({ content: 'ok' })); const { http } = await root.setup(); @@ -130,7 +130,7 @@ describe('http service', () => { http.registerRouter(router); await root.start(); - await kbnTestServer.request.get(root, '/').expect(200, { content: 'ok' }); + await kbnTestServer.request.get(root, '/new-platform/').expect(200, { content: 'ok' }); }); it('Should support redirecting to configured url', async () => { @@ -139,7 +139,7 @@ describe('http service', () => { http.registerOnPostAuth(async (req, t) => t.redirected(redirectTo)); await root.start(); - const response = await kbnTestServer.request.get(root, '/').expect(302); + const response = await kbnTestServer.request.get(root, '/new-platform/').expect(302); expect(response.header.location).toBe(redirectTo); }); @@ -151,7 +151,7 @@ describe('http service', () => { await root.start(); await kbnTestServer.request - .get(root, '/') + .get(root, '/new-platform/') .expect(400, { statusCode: 400, error: 'Bad Request', message: 'unexpected error' }); }); @@ -162,7 +162,7 @@ describe('http service', () => { }); await root.start(); - await kbnTestServer.request.get(root, '/').expect({ + await kbnTestServer.request.get(root, '/new-platform/').expect({ statusCode: 500, error: 'Internal Server Error', message: 'An internal server error occurred', @@ -183,7 +183,7 @@ describe('http service', () => { } return t.next(); }); - const router = new Router(''); + const router = new Router('/new-platform'); router.get({ path: '/', validate: false }, async (req, res) => // @ts-ignore. don't complain customField is not defined on Request type res.ok({ customField: String(req.customField) }) @@ -191,7 +191,9 @@ describe('http service', () => { http.registerRouter(router); await root.start(); - await kbnTestServer.request.get(root, '/').expect(200, { customField: 'undefined' }); + await kbnTestServer.request + .get(root, '/new-platform/') + .expect(200, { customField: 'undefined' }); }); }); @@ -205,10 +207,10 @@ describe('http service', () => { it('supports Url change on the flight', async () => { const { http } = await root.setup(); http.registerOnPreAuth((req, t) => { - return t.redirected('/new-url', { forward: true }); + return t.redirected('/new-platform/new-url', { forward: true }); }); - const router = new Router('/'); + const router = new Router('/new-platform'); router.get({ path: '/new-url', validate: false }, async (req, res) => res.ok({ key: 'new-url-reached' }) ); diff --git a/src/core/server/index.ts b/src/core/server/index.ts index 4cebe24f39f1553..73a137c15baa375 100644 --- a/src/core/server/index.ts +++ b/src/core/server/index.ts @@ -115,7 +115,7 @@ export interface InternalCoreSetup { * @public */ export interface InternalCoreStart { - http: HttpServiceStart; + // http: HttpServiceStart; plugins: PluginsServiceStart; } diff --git a/src/core/server/legacy/legacy_service.test.ts b/src/core/server/legacy/legacy_service.test.ts index 759a2eb76fd0c48..525bc626c788506 100644 --- a/src/core/server/legacy/legacy_service.test.ts +++ b/src/core/server/legacy/legacy_service.test.ts @@ -18,6 +18,7 @@ */ import { BehaviorSubject, Subject, throwError } from 'rxjs'; +// import { merge } from 'lodash'; jest.mock('./legacy_platform_proxy'); jest.mock('../../../legacy/server/kbn_server'); @@ -75,6 +76,7 @@ beforeEach(() => { http: { options: { someOption: 'foo', someAnotherOption: 'bar' }, server: { listener: { addListener: jest.fn() }, route: jest.fn() }, + shouldListen: () => true, }, plugins: { contracts: new Map([['plugin-id', 'plugin-value']]), @@ -339,7 +341,7 @@ describe('once LegacyService is set up without connection info', () => { const disabledHttpStartDeps = { core: { http: { - isListening: () => false, + // isListening: () => false, }, plugins: { contracts: new Map() }, }, @@ -347,6 +349,8 @@ describe('once LegacyService is set up without connection info', () => { }; let legacyService: LegacyService; beforeEach(async () => { + setupDeps.core.http.shouldListen = () => false; + legacyService = new LegacyService({ env, logger, configService: configService as any }); await legacyService.setup(setupDeps); @@ -382,6 +386,8 @@ describe('once LegacyService is set up without connection info', () => { describe('once LegacyService is set up in `devClusterMaster` mode', () => { beforeEach(() => { + // TODO fix me shouldnt mutate + setupDeps.core.http.shouldListen = () => false; configService.atPath.mockImplementation(path => { return new BehaviorSubject( path === 'dev' ? { basePathProxyTargetPort: 100500 } : { basePath: '/abc' } @@ -404,9 +410,9 @@ describe('once LegacyService is set up in `devClusterMaster` mode', () => { await devClusterLegacyService.setup(setupDeps); await devClusterLegacyService.start({ core: { - http: { - isListening: () => false, - }, + // http: { + // isListening: () => false, + // }, plugins: { contracts: new Map() }, }, plugins: {}, @@ -432,9 +438,9 @@ describe('once LegacyService is set up in `devClusterMaster` mode', () => { await devClusterLegacyService.setup(setupDeps); await devClusterLegacyService.start({ core: { - http: { - isListening: () => false, - }, + // http: { + // isListening: () => false, + // }, plugins: { contracts: new Map() }, }, plugins: {}, diff --git a/src/core/server/legacy/legacy_service.ts b/src/core/server/legacy/legacy_service.ts index fd1b46d7fa71189..5cd59585240b13b 100644 --- a/src/core/server/legacy/legacy_service.ts +++ b/src/core/server/legacy/legacy_service.ts @@ -154,7 +154,7 @@ export class LegacyService implements CoreService { // bridge with the "legacy" Kibana. If server isn't run (e.g. if process is // managed by ClusterManager or optimizer) then we won't have that info, // so we can't start "legacy" server either. - serverOptions: startDeps.core.http.isListening() + serverOptions: setupDeps.core.http.shouldListen() ? { ...setupDeps.core.http.options, listener: this.setupProxyListener(setupDeps.core.http.server), diff --git a/src/core/server/server.ts b/src/core/server/server.ts index 4f56e20f2c021b3..1c004a4659af418 100644 --- a/src/core/server/server.ts +++ b/src/core/server/server.ts @@ -83,11 +83,11 @@ export class Server { } public async start() { - const httpStart = await this.http.start(); + // const httpStart = const pluginsStart = await this.plugins.start({}); const coreStart = { - http: httpStart, + // http: httpStart, plugins: pluginsStart, }; @@ -96,6 +96,8 @@ export class Server { plugins: mapToObject(pluginsStart.contracts), }); + await this.http.start(); + return coreStart; } diff --git a/src/legacy/server/http/index.js b/src/legacy/server/http/index.js index 0b9c66074220972..b064bebb0f89ea3 100644 --- a/src/legacy/server/http/index.js +++ b/src/legacy/server/http/index.js @@ -21,14 +21,14 @@ import { format } from 'url'; import { resolve } from 'path'; import _ from 'lodash'; import Boom from 'boom'; -import Hapi from 'hapi'; +// import Hapi from 'hapi'; import { setupVersionCheck } from './version_check'; import { registerHapiPlugins } from './register_hapi_plugins'; import { setupBasePathProvider } from './setup_base_path_provider'; import { setupXsrf } from './xsrf'; export default async function (kbnServer, server, config) { - kbnServer.server = new Hapi.Server(kbnServer.newPlatform.params.serverOptions); + kbnServer.server = kbnServer.newPlatform.setup.core.http.server;//new Hapi.Server(kbnServer.newPlatform.params.serverOptions); server = kbnServer.server; setupBasePathProvider(kbnServer);