diff --git a/src/core/public/core_system.ts b/src/core/public/core_system.ts index 8638d55d32c366..47f854d95386f5 100644 --- a/src/core/public/core_system.ts +++ b/src/core/public/core_system.ts @@ -211,7 +211,7 @@ export class CoreSystem { const injectedMetadata = await this.injectedMetadata.start(); const uiSettings = await this.uiSettings.start(); const docLinks = await this.docLinks.start({ injectedMetadata }); - const http = await this.http.start({ injectedMetadata, fatalErrors: this.fatalErrorsSetup! }); + const http = await this.http.start(); const savedObjects = await this.savedObjects.start({ http }); const i18n = await this.i18n.start(); await this.integrations.start({ uiSettings }); diff --git a/src/core/public/http/http_service.test.ts b/src/core/public/http/http_service.test.ts index f95d25d116976f..a40fcb06273dd4 100644 --- a/src/core/public/http/http_service.test.ts +++ b/src/core/public/http/http_service.test.ts @@ -25,13 +25,40 @@ import { fatalErrorsServiceMock } from '../fatal_errors/fatal_errors_service.moc import { injectedMetadataServiceMock } from '../injected_metadata/injected_metadata_service.mock'; import { HttpService } from './http_service'; +describe('interceptors', () => { + afterEach(() => fetchMock.restore()); + + it('shares interceptors across setup and start', async () => { + fetchMock.get('*', {}); + const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); + const fatalErrors = fatalErrorsServiceMock.createSetupContract(); + const httpService = new HttpService(); + + const setup = httpService.setup({ fatalErrors, injectedMetadata }); + const setupInterceptor = jest.fn(); + setup.intercept({ request: setupInterceptor }); + + const start = httpService.start(); + const startInterceptor = jest.fn(); + start.intercept({ request: startInterceptor }); + + await setup.get('/blah'); + expect(setupInterceptor).toHaveBeenCalledTimes(1); + expect(startInterceptor).toHaveBeenCalledTimes(1); + + await start.get('/other-blah'); + expect(setupInterceptor).toHaveBeenCalledTimes(2); + expect(startInterceptor).toHaveBeenCalledTimes(2); + }); +}); + describe('#stop()', () => { it('calls loadingCount.stop()', () => { const injectedMetadata = injectedMetadataServiceMock.createSetupContract(); const fatalErrors = fatalErrorsServiceMock.createSetupContract(); const httpService = new HttpService(); httpService.setup({ fatalErrors, injectedMetadata }); - httpService.start({ fatalErrors, injectedMetadata }); + httpService.start(); httpService.stop(); expect(loadingServiceMock.stop).toHaveBeenCalled(); }); diff --git a/src/core/public/http/http_service.ts b/src/core/public/http/http_service.ts index 567cdd310cbdfc..8965747ba68374 100644 --- a/src/core/public/http/http_service.ts +++ b/src/core/public/http/http_service.ts @@ -35,6 +35,7 @@ interface HttpDeps { export class HttpService implements CoreService { private readonly anonymousPaths = new AnonymousPathsService(); private readonly loadingCount = new LoadingCountService(); + private service?: HttpSetup; public setup({ injectedMetadata, fatalErrors }: HttpDeps): HttpSetup { const kibanaVersion = injectedMetadata.getKibanaVersion(); @@ -42,7 +43,7 @@ export class HttpService implements CoreService { const fetchService = new Fetch({ basePath, kibanaVersion }); const loadingCount = this.loadingCount.setup({ fatalErrors }); - return { + this.service = { basePath, anonymousPaths: this.anonymousPaths.setup({ basePath }), intercept: fetchService.intercept.bind(fetchService), @@ -56,10 +57,16 @@ export class HttpService implements CoreService { put: fetchService.put.bind(fetchService), ...loadingCount, }; + + return this.service; } - public start(deps: HttpDeps) { - return this.setup(deps); + public start() { + if (!this.service) { + throw new Error(`HttpService#setup() must be called first!`); + } + + return this.service; } public stop() {