Skip to content

Commit

Permalink
Ensure http interceptors are shares across lifecycle methods (#57150)
Browse files Browse the repository at this point in the history
  • Loading branch information
joshdover committed Feb 10, 2020
1 parent 82972ef commit af37d5f
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/core/public/core_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
const fatalErrors = await this.fatalErrors.start();
Expand Down
29 changes: 28 additions & 1 deletion src/core/public/http/http_service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
13 changes: 10 additions & 3 deletions src/core/public/http/http_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,15 @@ 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();
const basePath = new BasePath(injectedMetadata.getBasePath());
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),
Expand All @@ -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() {
Expand Down

0 comments on commit af37d5f

Please sign in to comment.