Skip to content

Commit 3144f45

Browse files
committed
fix(http): Don't on Passthru outside of reactive context
Priori to this change, the InMemory API threw when request was emited outside an injection context and that request hit the passThru. This commit fixes this.
1 parent c90b6b3 commit 3144f45

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
HttpResponse,
1717
HttpXhrBackend,
1818
} from '@angular/common/http';
19-
import {Inject, Injectable, Optional} from '@angular/core';
19+
import {inject, Inject, Injectable, Injector, Optional, runInInjectionContext} from '@angular/core';
2020
import {Observable} from 'rxjs';
2121
import {map} from 'rxjs/operators';
2222

@@ -58,6 +58,8 @@ import {
5858
*/
5959
@Injectable()
6060
export class HttpClientBackendService extends BackendService implements HttpBackend {
61+
private injector = inject(Injector);
62+
6163
constructor(
6264
inMemDbService: InMemoryDbService,
6365
@Inject(InMemoryBackendConfig) @Optional() config: InMemoryBackendConfigArgs,
@@ -109,7 +111,7 @@ export class HttpClientBackendService extends BackendService implements HttpBack
109111

110112
protected override createPassThruBackend() {
111113
try {
112-
return new HttpXhrBackend(this.xhrFactory);
114+
return runInInjectionContext(this.injector, () => new HttpXhrBackend(this.xhrFactory));
113115
} catch (ex: any) {
114116
ex.message = 'Cannot create passThru404 backend; ' + (ex.message || '');
115117
throw ex;

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

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import {
2424
import {importProvidersFrom, Injectable} from '@angular/core';
2525
import {TestBed, waitForAsync} from '@angular/core/testing';
2626
import {HttpClientBackendService, HttpClientInMemoryWebApiModule} from 'angular-in-memory-web-api';
27-
import {Observable, zip, of} from 'rxjs';
27+
import {Observable, of, zip} from 'rxjs';
2828
import {concatMap, map, tap} from 'rxjs/operators';
2929

3030
import {Hero} from './fixtures/hero';
@@ -555,7 +555,6 @@ describe('HttpClient Backend Service', () => {
555555
beforeEach(() => {
556556
TestBed.configureTestingModule({
557557
imports: [
558-
HttpClientModule,
559558
HttpClientInMemoryWebApiModule.forRoot(HeroInMemDataService, {
560559
delay,
561560
passThruUnknownUrl: true,
@@ -593,6 +592,21 @@ describe('HttpClient Backend Service', () => {
593592
}));
594593
});
595594

595+
describe('HttpClient passThru creation', () => {
596+
it('should create the passThru backend', () => {
597+
TestBed.configureTestingModule({
598+
imports: [
599+
HttpClientInMemoryWebApiModule.forRoot(HeroInMemDataService, {
600+
delay,
601+
passThruUnknownUrl: true,
602+
}),
603+
],
604+
});
605+
const httpBackend = TestBed.inject<any>(HttpBackend);
606+
expect(() => httpBackend.createPassThruBackend()).not.toThrow();
607+
});
608+
});
609+
596610
describe('Http dataEncapsulation = true', () => {
597611
let http: HttpClient;
598612

0 commit comments

Comments
 (0)