Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure http interceptors are shares across lifecycle methods #57150

Merged
merged 3 commits into from
Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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