Skip to content

Commit 291ba38

Browse files
JeanMechethePunderWoman
authored andcommitted
fix(http): Don't override the backend when using the InMemoryWebAPI (#52425)
When using `withFetch`, the `PRIMARY_HTTP_BACKEND` token is set. The InMemory Backend services will also set that token. This means that providers order will matter and the latest on the list will be the one instantiated PR Close #52425
1 parent 12f979d commit 291ba38

File tree

4 files changed

+41
-7
lines changed

4 files changed

+41
-7
lines changed

packages/common/http/src/private_export.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
export {HTTP_ROOT_INTERCEPTOR_FNS as ɵHTTP_ROOT_INTERCEPTOR_FNS} from './interceptor';
9+
export {HTTP_ROOT_INTERCEPTOR_FNS as ɵHTTP_ROOT_INTERCEPTOR_FNS, PRIMARY_HTTP_BACKEND as ɵPRIMARY_HTTP_BACKEND} from './interceptor';

packages/misc/angular-in-memory-web-api/src/http-client-in-memory-web-api-module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {XhrFactory} from '@angular/common';
10-
import {HttpBackend} from '@angular/common/http';
10+
import {HttpBackend, ɵPRIMARY_HTTP_BACKEND as PRIMARY_HTTP_BACKEND} from '@angular/common/http';
1111
import {ModuleWithProviders, NgModule, Type} from '@angular/core';
1212

1313
import {HttpClientBackendService} from './http-client-backend-service';
@@ -31,6 +31,8 @@ export class HttpClientInMemoryWebApiModule {
3131
* Usually imported in the root application module.
3232
* Can import in a lazy feature module too, which will shadow modules loaded earlier
3333
*
34+
* Note: If you use the `FetchBackend`, make sure forRoot is invoked after in the providers list
35+
*
3436
* @param dbCreator - Class that creates seed data for in-memory database. Must implement
3537
* InMemoryDbService.
3638
* @param [options]
@@ -49,7 +51,8 @@ export class HttpClientInMemoryWebApiModule {
4951
provide: HttpBackend,
5052
useFactory: httpClientInMemBackendServiceFactory,
5153
deps: [InMemoryDbService, InMemoryBackendConfig, XhrFactory]
52-
}
54+
},
55+
{provide: PRIMARY_HTTP_BACKEND, useExisting: HttpBackend}
5356
]
5457
};
5558
}

packages/misc/angular-in-memory-web-api/src/in-memory-web-api-module.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
*/
88

99
import {XhrFactory} from '@angular/common';
10-
import {HttpBackend} from '@angular/common/http';
10+
import {HttpBackend, ɵPRIMARY_HTTP_BACKEND as PRIMARY_HTTP_BACKEND} from '@angular/common/http';
1111
import {ModuleWithProviders, NgModule, Type} from '@angular/core';
1212

1313
import {httpClientInMemBackendServiceFactory} from './http-client-in-memory-web-api-module';
@@ -23,6 +23,8 @@ export class InMemoryWebApiModule {
2323
* Usually imported in the root application module.
2424
* Can import in a lazy feature module too, which will shadow modules loaded earlier
2525
*
26+
* Note: If you use the `FetchBackend`, make sure forRoot is invoked after in the providers list
27+
*
2628
* @param dbCreator - Class that creates seed data for in-memory database. Must implement
2729
* InMemoryDbService.
2830
* @param [options]
@@ -41,7 +43,8 @@ export class InMemoryWebApiModule {
4143
provide: HttpBackend,
4244
useFactory: httpClientInMemBackendServiceFactory,
4345
deps: [InMemoryDbService, InMemoryBackendConfig, XhrFactory]
44-
}
46+
},
47+
{provide: PRIMARY_HTTP_BACKEND, useExisting: HttpBackend}
4548
]
4649
};
4750
}

packages/misc/angular-in-memory-web-api/test/http-client-backend-service_spec.ts

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
import 'jasmine-ajax';
1010

11-
import {HTTP_INTERCEPTORS, HttpBackend, HttpClient, HttpClientModule, HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http';
12-
import {Injectable} from '@angular/core';
11+
import {FetchBackend, HTTP_INTERCEPTORS, HttpBackend, HttpClient, HttpClientModule, HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse, provideHttpClient, withFetch} from '@angular/common/http';
12+
import {importProvidersFrom, Injectable} from '@angular/core';
1313
import {TestBed, waitForAsync} from '@angular/core/testing';
1414
import {HttpClientBackendService, HttpClientInMemoryWebApiModule} from 'angular-in-memory-web-api';
1515
import {Observable, zip} from 'rxjs';
@@ -565,6 +565,34 @@ describe('HttpClient Backend Service', () => {
565565
failRequest);
566566
}));
567567
});
568+
569+
describe('when using the FetchBackend', () => {
570+
it('should be the an InMemory Service', () => {
571+
TestBed.configureTestingModule({
572+
providers: [
573+
provideHttpClient(withFetch()),
574+
importProvidersFrom(
575+
HttpClientInMemoryWebApiModule.forRoot(HeroInMemDataService, {delay})),
576+
{provide: HeroService, useClass: HttpClientHeroService}
577+
]
578+
});
579+
580+
expect(TestBed.inject(HttpBackend)).toBeInstanceOf(HttpClientBackendService);
581+
});
582+
583+
it('should be a FetchBackend', () => {
584+
// In this test, providers order matters
585+
TestBed.configureTestingModule({
586+
providers: [
587+
importProvidersFrom(
588+
HttpClientInMemoryWebApiModule.forRoot(HeroInMemDataService, {delay})),
589+
provideHttpClient(withFetch()), {provide: HeroService, useClass: HttpClientHeroService}
590+
]
591+
});
592+
593+
expect(TestBed.inject(HttpBackend)).toBeInstanceOf(FetchBackend);
594+
});
595+
});
568596
});
569597

570598

0 commit comments

Comments
 (0)