Skip to content

Commit

Permalink
Revert "fix(http): Don't override the backend when using the InMemory…
Browse files Browse the repository at this point in the history
…WebAPI (angular#52425)"

This reverts commit 49b037f. Reason: it
breaks tests in aio-local.
  • Loading branch information
alxhub authored and tbondwilkinson committed Dec 6, 2023
1 parent 570d7bb commit bdffcdc
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 45 deletions.
2 changes: 1 addition & 1 deletion packages/common/http/src/private_export.ts
Expand Up @@ -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';
Expand Up @@ -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 {
/**
Expand All @@ -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]
Expand All @@ -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]
}
]
};
}
Expand Down
Expand Up @@ -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()
Expand All @@ -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]
Expand All @@ -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]
}
]
};
}
Expand Down
Expand Up @@ -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';
Expand Down Expand Up @@ -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);
});
});
});


Expand Down

0 comments on commit bdffcdc

Please sign in to comment.