mirror of
https://github.com/angular/angular
synced 2026-05-24 09:28:37 +00:00
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
This commit is contained in:
parent
61f0ba8895
commit
7426dbbd56
3 changed files with 51 additions and 4 deletions
|
|
@ -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(
|
||||
|
|
|
|||
|
|
@ -7,6 +7,9 @@ ts_project(
|
|||
srcs = glob(
|
||||
["**/*.ts"],
|
||||
),
|
||||
interop_deps = [
|
||||
"//packages/private/testing",
|
||||
],
|
||||
# Visible to //:saucelabs_unit_tests_poc target
|
||||
visibility = ["//:__pkg__"],
|
||||
deps = [
|
||||
|
|
|
|||
|
|
@ -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<any>;
|
||||
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);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue