From 88f2cac00ab13b45fb9e5a6baf0e6630989a0c80 Mon Sep 17 00:00:00 2001 From: Rainer Hahnekamp Date: Thu, 18 Apr 2024 11:53:42 +0200 Subject: [PATCH] fix(http): throw error if `withFetch()` and `withRequestsMadeViaParent()` are used together These changes will ensure that an error is thrown when `withRequestsMadeViaParent()` and `withFetch()` are used together in the same call to `provideHttpClient()`, as this configuration is contradictory and would lead to problems in the execution, i.e. the interceptors from the parent's don't run. --- packages/common/http/src/provider.ts | 7 +++++++ packages/common/http/test/provider_spec.ts | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/packages/common/http/src/provider.ts b/packages/common/http/src/provider.ts index 15ac02f2dc68..e4b3922e8d5c 100644 --- a/packages/common/http/src/provider.ts +++ b/packages/common/http/src/provider.ts @@ -89,6 +89,13 @@ export function provideHttpClient(...features: HttpFeature[]): `Configuration error: found both withXsrfConfiguration() and withNoXsrfProtection() in the same call to provideHttpClient(), which is a contradiction.` : ''); } + + if (featureKinds.has(HttpFeatureKind.RequestsMadeViaParent) && featureKinds.has(HttpFeatureKind.Fetch)) { + throw new Error( + ngDevMode ? + `Configuration error: found both withRequestsMadeViaParent() and withFetch() in the same call to provideHttpClient(), which is a contradiction.` : + ''); + } } const providers: Provider[] = [ diff --git a/packages/common/http/test/provider_spec.ts b/packages/common/http/test/provider_spec.ts index f0c1fe14ba58..4d69768dc99f 100644 --- a/packages/common/http/test/provider_spec.ts +++ b/packages/common/http/test/provider_spec.ts @@ -449,6 +449,24 @@ describe('provideHttpClient', () => { expect(consoleWarnSpy.calls.count()).toBe(0); }); + it('should throw when fetch is used together with withRequestsMadeViaParent()', () => { + TestBed.resetTestingModule(); + + TestBed.configureTestingModule({ + providers: [ + provideHttpClient(), + provideHttpClientTesting(), + ] + }); + + expect(() => createEnvironmentInjector( + [provideHttpClient( + withFetch(), + withRequestsMadeViaParent(), + )], + TestBed.inject(EnvironmentInjector))).toThrow(); + }); + it('should warn during SSR if fetch is not configured', () => { resetFetchBackendWarningFlag();