From bdffcdccf51b0f7c35298717ee7534d1e8080e42 Mon Sep 17 00:00:00 2001 From: Alex Rickabaugh Date: Tue, 31 Oct 2023 16:16:16 -0700 Subject: [PATCH] Revert "fix(http): Don't override the backend when using the InMemoryWebAPI (#52425)" This reverts commit 49b037f8116d56520035a1cc0797b65f1b0e3ad9. Reason: it breaks tests in aio-local. --- packages/common/http/src/private_export.ts | 2 +- .../http-client-in-memory-web-api-module.ts | 23 +++++++++---- .../src/in-memory-web-api-module.ts | 15 +++++---- .../test/http-client-backend-service_spec.ts | 32 ++----------------- 4 files changed, 27 insertions(+), 45 deletions(-) diff --git a/packages/common/http/src/private_export.ts b/packages/common/http/src/private_export.ts index bc0c9866932a2..d5b0ff7cb9f87 100644 --- a/packages/common/http/src/private_export.ts +++ b/packages/common/http/src/private_export.ts @@ -6,4 +6,4 @@ * found in the LICENSE file at https://angular.io/license */ -export {HTTP_ROOT_INTERCEPTOR_FNS as ɵHTTP_ROOT_INTERCEPTOR_FNS, PRIMARY_HTTP_BACKEND as ɵPRIMARY_HTTP_BACKEND} from './interceptor'; +export {HTTP_ROOT_INTERCEPTOR_FNS as ɵHTTP_ROOT_INTERCEPTOR_FNS} from './interceptor'; diff --git a/packages/misc/angular-in-memory-web-api/src/http-client-in-memory-web-api-module.ts b/packages/misc/angular-in-memory-web-api/src/http-client-in-memory-web-api-module.ts index 8b32b61bbc584..e2fe53ea15ca4 100644 --- a/packages/misc/angular-in-memory-web-api/src/http-client-in-memory-web-api-module.ts +++ b/packages/misc/angular-in-memory-web-api/src/http-client-in-memory-web-api-module.ts @@ -6,12 +6,21 @@ * found in the LICENSE file at https://angular.io/license */ -import {HttpBackend, ɵPRIMARY_HTTP_BACKEND as PRIMARY_HTTP_BACKEND} from '@angular/common/http'; +import {XhrFactory} from '@angular/common'; +import {HttpBackend} from '@angular/common/http'; import {ModuleWithProviders, NgModule, Type} from '@angular/core'; import {HttpClientBackendService} from './http-client-backend-service'; import {InMemoryBackendConfig, InMemoryBackendConfigArgs, InMemoryDbService} from './interfaces'; +// Internal - Creates the in-mem backend for the HttpClient module +// AoT requires factory to be exported +export function httpClientInMemBackendServiceFactory( + dbService: InMemoryDbService, options: InMemoryBackendConfig, + xhrFactory: XhrFactory): HttpBackend { + return new HttpClientBackendService(dbService, options, xhrFactory) as HttpBackend; +} + @NgModule() export class HttpClientInMemoryWebApiModule { /** @@ -22,8 +31,6 @@ export class HttpClientInMemoryWebApiModule { * Usually imported in the root application module. * Can import in a lazy feature module too, which will shadow modules loaded earlier * - * Note: If you use the `FetchBackend`, make sure forRoot is invoked after in the providers list - * * @param dbCreator - Class that creates seed data for in-memory database. Must implement * InMemoryDbService. * @param [options] @@ -37,10 +44,12 @@ export class HttpClientInMemoryWebApiModule { return { ngModule: HttpClientInMemoryWebApiModule, providers: [ - HttpClientBackendService, {provide: InMemoryDbService, useClass: dbCreator}, - {provide: InMemoryBackendConfig, useValue: options}, - {provide: HttpBackend, useExisting: HttpClientBackendService}, - {provide: PRIMARY_HTTP_BACKEND, useExisting: HttpClientBackendService} + {provide: InMemoryDbService, useClass: dbCreator}, + {provide: InMemoryBackendConfig, useValue: options}, { + provide: HttpBackend, + useFactory: httpClientInMemBackendServiceFactory, + deps: [InMemoryDbService, InMemoryBackendConfig, XhrFactory] + } ] }; } diff --git a/packages/misc/angular-in-memory-web-api/src/in-memory-web-api-module.ts b/packages/misc/angular-in-memory-web-api/src/in-memory-web-api-module.ts index 21030dbc67c88..6234aa513bfc2 100644 --- a/packages/misc/angular-in-memory-web-api/src/in-memory-web-api-module.ts +++ b/packages/misc/angular-in-memory-web-api/src/in-memory-web-api-module.ts @@ -6,10 +6,11 @@ * found in the LICENSE file at https://angular.io/license */ -import {HttpBackend, ɵPRIMARY_HTTP_BACKEND as PRIMARY_HTTP_BACKEND} from '@angular/common/http'; +import {XhrFactory} from '@angular/common'; +import {HttpBackend} from '@angular/common/http'; import {ModuleWithProviders, NgModule, Type} from '@angular/core'; -import {HttpClientBackendService} from './http-client-backend-service'; +import {httpClientInMemBackendServiceFactory} from './http-client-in-memory-web-api-module'; import {InMemoryBackendConfig, InMemoryBackendConfigArgs, InMemoryDbService} from './interfaces'; @NgModule() @@ -22,8 +23,6 @@ export class InMemoryWebApiModule { * Usually imported in the root application module. * Can import in a lazy feature module too, which will shadow modules loaded earlier * - * Note: If you use the `FetchBackend`, make sure forRoot is invoked after in the providers list - * * @param dbCreator - Class that creates seed data for in-memory database. Must implement * InMemoryDbService. * @param [options] @@ -38,9 +37,11 @@ export class InMemoryWebApiModule { ngModule: InMemoryWebApiModule, providers: [ {provide: InMemoryDbService, useClass: dbCreator}, - {provide: InMemoryBackendConfig, useValue: options}, - {provide: HttpBackend, useClass: HttpClientBackendService}, - {provide: PRIMARY_HTTP_BACKEND, useExisting: HttpClientBackendService} + {provide: InMemoryBackendConfig, useValue: options}, { + provide: HttpBackend, + useFactory: httpClientInMemBackendServiceFactory, + deps: [InMemoryDbService, InMemoryBackendConfig, XhrFactory] + } ] }; } diff --git a/packages/misc/angular-in-memory-web-api/test/http-client-backend-service_spec.ts b/packages/misc/angular-in-memory-web-api/test/http-client-backend-service_spec.ts index 0d6dab79ad73d..d857fb969611a 100644 --- a/packages/misc/angular-in-memory-web-api/test/http-client-backend-service_spec.ts +++ b/packages/misc/angular-in-memory-web-api/test/http-client-backend-service_spec.ts @@ -8,8 +8,8 @@ import 'jasmine-ajax'; -import {FetchBackend, HTTP_INTERCEPTORS, HttpBackend, HttpClient, HttpClientModule, HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse, provideHttpClient, withFetch} from '@angular/common/http'; -import {importProvidersFrom, Injectable} from '@angular/core'; +import {HTTP_INTERCEPTORS, HttpBackend, HttpClient, HttpClientModule, HttpEvent, HttpEventType, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse} from '@angular/common/http'; +import {Injectable} from '@angular/core'; import {TestBed, waitForAsync} from '@angular/core/testing'; import {HttpClientBackendService, HttpClientInMemoryWebApiModule} from 'angular-in-memory-web-api'; import {Observable, zip} from 'rxjs'; @@ -565,34 +565,6 @@ describe('HttpClient Backend Service', () => { failRequest); })); }); - - describe('when using the FetchBackend', () => { - it('should be the an InMemory Service', () => { - TestBed.configureTestingModule({ - providers: [ - provideHttpClient(withFetch()), - importProvidersFrom( - HttpClientInMemoryWebApiModule.forRoot(HeroInMemDataService, {delay})), - {provide: HeroService, useClass: HttpClientHeroService} - ] - }); - - expect(TestBed.inject(HttpBackend)).toBeInstanceOf(HttpClientBackendService); - }); - - it('should be a FetchBackend', () => { - // In this test, providers order matters - TestBed.configureTestingModule({ - providers: [ - importProvidersFrom( - HttpClientInMemoryWebApiModule.forRoot(HeroInMemDataService, {delay})), - provideHttpClient(withFetch()), {provide: HeroService, useClass: HttpClientHeroService} - ] - }); - - expect(TestBed.inject(HttpBackend)).toBeInstanceOf(FetchBackend); - }); - }); });