Skip to content

Commit

Permalink
Revert "fix(http): Dynamicaly call the global fetch implementation (a…
Browse files Browse the repository at this point in the history
…ngular#57531)" (angular#57571)

This reverts commit 21445a2.

Reason: failing test

PR Close angular#57571
  • Loading branch information
alxhub committed Aug 28, 2024
1 parent b7f3baf commit 013d02d
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 37 deletions.
6 changes: 2 additions & 4 deletions packages/common/http/src/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,9 @@ function getResponseUrl(response: Response): string | null {
*/
@Injectable()
export class FetchBackend implements HttpBackend {
// Note: binding fetch directly causes an "illegal invocation" error
// We use an arrow function to always reference the current global implementation of fetch
// In case it has been (monkey)patched after the FetchBackend has been created
// We need to bind the native fetch to its context or it will throw an "illegal invocation"
private readonly fetchImpl =
inject(FetchFactory, {optional: true})?.fetch ?? ((...args) => globalThis.fetch(...args));
inject(FetchFactory, {optional: true})?.fetch ?? fetch.bind(globalThis);
private readonly ngZone = inject(NgZone);

handle(request: HttpRequest<any>): Observable<HttpEvent<any>> {
Expand Down
33 changes: 0 additions & 33 deletions packages/common/http/test/fetch_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,11 @@ import {Observable, of, Subject} from 'rxjs';
import {catchError, retry, scan, skip, take, toArray} from 'rxjs/operators';

import {
HttpClient,
HttpDownloadProgressEvent,
HttpErrorResponse,
HttpHeaderResponse,
HttpParams,
HttpStatusCode,
provideHttpClient,
withFetch,
} from '../public_api';
import {FetchBackend, FetchFactory} from '../src/fetch';

Expand Down Expand Up @@ -419,36 +416,6 @@ describe('FetchBackend', async () => {
fetchMock.mockFlush(0, 'CORS 0 status');
});
});

describe('dynamic global fetch', () => {
beforeEach(() => {
TestBed.resetTestingModule();
TestBed.configureTestingModule({
providers: [provideHttpClient(withFetch())],
});
});

it('should use the current implementation of the global fetch', async () => {
const fakeFetch = jasmine
.createSpy('', () => Promise.resolve(new Response(JSON.stringify({foo: 'bar'}))))
.and.callThrough();
globalThis.fetch = fakeFetch;

const client = TestBed.inject(HttpClient);
expect(fakeFetch).not.toHaveBeenCalled();
let response = await client.get<unknown>('').toPromise();
expect(fakeFetch).toHaveBeenCalled();
expect(response).toEqual({foo: 'bar'});

// We dynamicaly change the implementation of fetch
const fakeFetch2 = jasmine
.createSpy('', () => Promise.resolve(new Response(JSON.stringify({foo: 'baz'}))))
.and.callThrough();
globalThis.fetch = fakeFetch2;
response = await client.get<unknown>('').toPromise();
expect(response).toEqual({foo: 'baz'});
});
});
});

export class MockFetchFactory extends FetchFactory {
Expand Down

0 comments on commit 013d02d

Please sign in to comment.