[7.6] Ensure http interceptors are shares across lifecycle met… (#57265)

This commit is contained in:
Josh Dover 2020-02-11 07:46:57 -07:00 committed by GitHub
parent e29547a341
commit 9bd1ed3013
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 39 additions and 5 deletions

View file

@ -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 });

View file

@ -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();
});

View file

@ -35,6 +35,7 @@ interface HttpDeps {
export class HttpService implements CoreService<HttpSetup, HttpStart> {
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<HttpSetup, HttpStart> {
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<HttpSetup, HttpStart> {
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() {