angular/goldens/public-api/common/http/testing/testing.md
Paul Gschwendtner 489cf42cd0 fix(common): incorrect error type for XHR errors in TestRequest (#36082)
Currently the `HttpClient` always wraps errors from XHR requests, but
the underlying errors are always of type `ProgressEvent`, or don't have
a native error if the status code is just indicating failure (e.g. 404).

This behavior does not match in the `TestRequest` class provided by
`@angular/common/http/testing` where errors are considered being
of type `ErrorEvent`. This is incorrect because `ErrorEvent`s provide
information for errors in scripts or files which are evaluated. Since
the `HttpClient` never evaluates scripts/files, and also since XHR requests
clearly are documented to emit `ProgressEvent`'s, we should change the
`TestSupport` to retrieve such `ProgressEvent`'s instead of incompatible
objects of type `ErrorEvent`.

In favor of having a deprecation period, we keep supporting `ErrorEvent`
in the `TestRequest.error` signature. Eventually, we can remove this
signature in the future.

Resources:
  * https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/error_event
  * https://developer.mozilla.org/en-US/docs/Web/API/ErrorEvent
  * https://xhr.spec.whatwg.org/#event-xhr-errpr

Related to: https://github.com/angular/angular/issues/34748.

DEPRECATED: `TestRequest` from `@angular/common/http/testing` no longer
accepts `ErrorEvent` when simulating XHR errors. Instead instances of
`ProgressEvent` should be passed, matching with the native browser behavior.

PR Close #36082
2021-11-19 21:26:52 +00:00

69 lines
2.6 KiB
Markdown

## API Report File for "@angular/common_http_testing"
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { HttpEvent } from '@angular/common/http';
import { HttpHeaders } from '@angular/common/http';
import { HttpRequest } from '@angular/common/http';
import * as i0 from '@angular/core';
import * as i1 from '@angular/common/http';
import { Observer } from 'rxjs';
// @public
export class HttpClientTestingModule {
// (undocumented)
static ɵfac: i0.ɵɵFactoryDeclaration<HttpClientTestingModule, never>;
// (undocumented)
static ɵinj: i0.ɵɵInjectorDeclaration<HttpClientTestingModule>;
// (undocumented)
static ɵmod: i0.ɵɵNgModuleDeclaration<HttpClientTestingModule, never, [typeof i1.HttpClientModule], never>;
}
// @public
export abstract class HttpTestingController {
abstract expectNone(url: string, description?: string): void;
abstract expectNone(params: RequestMatch, description?: string): void;
abstract expectNone(matchFn: ((req: HttpRequest<any>) => boolean), description?: string): void;
abstract expectNone(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): void;
abstract expectOne(url: string, description?: string): TestRequest;
abstract expectOne(params: RequestMatch, description?: string): TestRequest;
abstract expectOne(matchFn: ((req: HttpRequest<any>) => boolean), description?: string): TestRequest;
abstract expectOne(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean), description?: string): TestRequest;
abstract match(match: string | RequestMatch | ((req: HttpRequest<any>) => boolean)): TestRequest[];
abstract verify(opts?: {
ignoreCancelled?: boolean;
}): void;
}
// @public
export interface RequestMatch {
// (undocumented)
method?: string;
// (undocumented)
url?: string;
}
// @public
export class TestRequest {
constructor(request: HttpRequest<any>, observer: Observer<HttpEvent<any>>);
get cancelled(): boolean;
// @deprecated
error(error: ErrorEvent, opts?: TestRequestErrorOptions): void;
error(error: ProgressEvent, opts?: TestRequestErrorOptions): void;
event(event: HttpEvent<any>): void;
flush(body: ArrayBuffer | Blob | boolean | string | number | Object | (boolean | string | number | Object | null)[] | null, opts?: {
headers?: HttpHeaders | {
[name: string]: string | string[];
};
status?: number;
statusText?: string;
}): void;
// (undocumented)
request: HttpRequest<any>;
}
// (No @packageDocumentation comment for this package)
```