From 7426dbbd56d7e37e97df9b7c35fd2bf2bdf44755 Mon Sep 17 00:00:00 2001 From: Mansi Shrivastava Date: Sat, 3 May 2025 22:14:04 +0530 Subject: [PATCH] refactor(common): allow HttpErrorResponse with successful status codes (#61107) This adds the ability to mock out errors even when successful status codes are returned. Fixes #26161 PR Close #61107 --- packages/common/http/testing/src/request.ts | 3 -- packages/common/http/testing/test/BUILD.bazel | 3 ++ .../common/http/testing/test/request_spec.ts | 49 ++++++++++++++++++- 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/packages/common/http/testing/src/request.ts b/packages/common/http/testing/src/request.ts index 0d8908d628d..f054c81f4a7 100644 --- a/packages/common/http/testing/src/request.ts +++ b/packages/common/http/testing/src/request.ts @@ -117,9 +117,6 @@ export class TestRequest { if (this.cancelled) { throw new Error(`Cannot return an error for a cancelled request.`); } - if (opts.status && opts.status >= 200 && opts.status < 300) { - throw new Error(`error() called with a successful status.`); - } const headers = opts.headers instanceof HttpHeaders ? opts.headers : new HttpHeaders(opts.headers); this.observer.error( diff --git a/packages/common/http/testing/test/BUILD.bazel b/packages/common/http/testing/test/BUILD.bazel index a7a816477ca..3434caa1995 100644 --- a/packages/common/http/testing/test/BUILD.bazel +++ b/packages/common/http/testing/test/BUILD.bazel @@ -7,6 +7,9 @@ ts_project( srcs = glob( ["**/*.ts"], ), + interop_deps = [ + "//packages/private/testing", + ], # Visible to //:saucelabs_unit_tests_poc target visibility = ["//:__pkg__"], deps = [ diff --git a/packages/common/http/testing/test/request_spec.ts b/packages/common/http/testing/test/request_spec.ts index fc96e141eed..8dc52eb47a3 100644 --- a/packages/common/http/testing/test/request_spec.ts +++ b/packages/common/http/testing/test/request_spec.ts @@ -6,8 +6,11 @@ * found in the LICENSE file at https://angular.dev/license */ -import {HttpClient} from '../../index'; +import {HttpClient, HttpRequest, HttpErrorResponse} from '../../index'; import {HttpClientTestingBackend} from '../../testing/src/backend'; +import {TestRequest} from '../src/request'; +import {isBrowser} from '@angular/private/testing'; +import {Observer} from 'rxjs'; describe('HttpClient TestRequest', () => { it('accepts a null body', () => { @@ -109,4 +112,48 @@ describe('HttpClient TestRequest', () => { ); } }); + + describe('successful errors', () => { + let request: TestRequest; + let observer: Observer; + let lastError: any; + + beforeEach(() => { + const httpRequest = new HttpRequest('GET', '/test'); + observer = { + next: jasmine.createSpy('next'), + error: (err: any) => { + lastError = err; + }, + complete: jasmine.createSpy('complete'), + }; + request = new TestRequest(httpRequest, observer); + }); + + if (isBrowser) { + it('should allow creating HttpErrorResponse with successful status', () => { + const error = new ProgressEvent('error'); + request.error(error, {status: 200, statusText: 'OK'}); + + expect(lastError).toBeDefined(); + expect(lastError).toBeInstanceOf(HttpErrorResponse); + expect(lastError.status).toBe(200); + expect(lastError.statusText).toBe('OK'); + }); + + it('should allow creating HttpErrorResponse with any status code', () => { + const error = new ProgressEvent('error'); + request.error(error, {status: 404, statusText: 'Not Found'}); + + expect(lastError).toBeDefined(); + expect(lastError).toBeInstanceOf(HttpErrorResponse); + expect(lastError.status).toBe(404); + expect(lastError.statusText).toBe('Not Found'); + }); + } else { + it('dummy test for node tests', () => { + expect(true).toBe(true); + }); + } + }); });