2017-03-23 00:13:24 +00:00
|
|
|
/**
|
|
|
|
|
* @license
|
2020-05-19 19:08:49 +00:00
|
|
|
* Copyright Google LLC All Rights Reserved.
|
2017-03-23 00:13:24 +00:00
|
|
|
*
|
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
2024-09-20 15:23:15 +00:00
|
|
|
* found in the LICENSE file at https://angular.dev/license
|
2017-03-23 00:13:24 +00:00
|
|
|
*/
|
|
|
|
|
|
2024-01-29 21:06:27 +00:00
|
|
|
import {
|
|
|
|
|
HttpErrorResponse,
|
|
|
|
|
HttpEvent,
|
|
|
|
|
HttpHeaders,
|
|
|
|
|
HttpRequest,
|
|
|
|
|
HttpResponse,
|
|
|
|
|
HttpStatusCode,
|
|
|
|
|
} from '@angular/common/http';
|
2018-02-27 22:06:06 +00:00
|
|
|
import {Observer} from 'rxjs';
|
2017-03-23 00:13:24 +00:00
|
|
|
|
2020-06-22 08:09:47 +00:00
|
|
|
/**
|
|
|
|
|
* Type that describes options that can be used to create an error
|
|
|
|
|
* in `TestRequest`.
|
|
|
|
|
*/
|
|
|
|
|
type TestRequestErrorOptions = {
|
2024-01-29 21:06:27 +00:00
|
|
|
headers?: HttpHeaders | {[name: string]: string | string[]};
|
|
|
|
|
status?: number;
|
|
|
|
|
statusText?: string;
|
2020-06-22 08:09:47 +00:00
|
|
|
};
|
|
|
|
|
|
2017-03-23 00:13:24 +00:00
|
|
|
/**
|
|
|
|
|
* A mock requests that was received and is ready to be answered.
|
|
|
|
|
*
|
|
|
|
|
* This interface allows access to the underlying `HttpRequest`, and allows
|
|
|
|
|
* responding with `HttpEvent`s or `HttpErrorResponse`s.
|
|
|
|
|
*
|
2018-10-19 14:06:08 +00:00
|
|
|
* @publicApi
|
2017-03-23 00:13:24 +00:00
|
|
|
*/
|
|
|
|
|
export class TestRequest {
|
|
|
|
|
/**
|
|
|
|
|
* Whether the request was cancelled after it was sent.
|
|
|
|
|
*/
|
2020-04-13 23:40:21 +00:00
|
|
|
get cancelled(): boolean {
|
|
|
|
|
return this._cancelled;
|
|
|
|
|
}
|
2017-03-23 00:13:24 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @internal set by `HttpClientTestingBackend`
|
|
|
|
|
*/
|
|
|
|
|
_cancelled = false;
|
|
|
|
|
|
2024-01-29 21:06:27 +00:00
|
|
|
constructor(
|
|
|
|
|
public request: HttpRequest<any>,
|
|
|
|
|
private observer: Observer<HttpEvent<any>>,
|
|
|
|
|
) {}
|
2017-03-23 00:13:24 +00:00
|
|
|
|
2017-07-13 00:00:23 +00:00
|
|
|
/**
|
|
|
|
|
* Resolve the request by returning a body plus additional HTTP information (such as response
|
|
|
|
|
* headers) if provided.
|
2019-03-11 08:33:52 +00:00
|
|
|
* If the request specifies an expected body type, the body is converted into the requested type.
|
|
|
|
|
* Otherwise, the body is converted to `JSON` by default.
|
2017-07-13 00:00:23 +00:00
|
|
|
*
|
|
|
|
|
* Both successful and unsuccessful responses can be delivered via `flush()`.
|
|
|
|
|
*/
|
2020-07-02 15:18:13 +00:00
|
|
|
flush(
|
2024-01-29 21:06:27 +00:00
|
|
|
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 {
|
2017-03-23 00:13:24 +00:00
|
|
|
if (this.cancelled) {
|
|
|
|
|
throw new Error(`Cannot flush a cancelled request.`);
|
|
|
|
|
}
|
2017-07-07 21:56:36 +00:00
|
|
|
const url = this.request.urlWithParams;
|
2017-03-23 00:13:24 +00:00
|
|
|
const headers =
|
2024-01-29 21:06:27 +00:00
|
|
|
opts.headers instanceof HttpHeaders ? opts.headers : new HttpHeaders(opts.headers);
|
2017-03-23 00:13:24 +00:00
|
|
|
body = _maybeConvertBody(this.request.responseType, body);
|
2024-01-29 21:06:27 +00:00
|
|
|
let statusText: string | undefined = opts.statusText;
|
2021-01-26 21:42:04 +00:00
|
|
|
let status: number = opts.status !== undefined ? opts.status : HttpStatusCode.Ok;
|
2017-03-23 00:13:24 +00:00
|
|
|
if (opts.status === undefined) {
|
|
|
|
|
if (body === null) {
|
2021-01-26 21:42:04 +00:00
|
|
|
status = HttpStatusCode.NoContent;
|
2024-01-15 17:14:53 +00:00
|
|
|
statusText ||= 'No Content';
|
2017-03-23 00:13:24 +00:00
|
|
|
} else {
|
2024-01-15 17:14:53 +00:00
|
|
|
statusText ||= 'OK';
|
2017-03-23 00:13:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (statusText === undefined) {
|
|
|
|
|
throw new Error('statusText is required when setting a custom status.');
|
|
|
|
|
}
|
|
|
|
|
if (status >= 200 && status < 300) {
|
2017-07-17 19:16:27 +00:00
|
|
|
this.observer.next(new HttpResponse<any>({body, headers, status, statusText, url}));
|
2017-03-23 00:13:24 +00:00
|
|
|
this.observer.complete();
|
|
|
|
|
} else {
|
2017-07-17 19:16:27 +00:00
|
|
|
this.observer.error(new HttpErrorResponse({error: body, headers, status, statusText, url}));
|
2017-03-23 00:13:24 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-13 00:00:23 +00:00
|
|
|
/**
|
|
|
|
|
* Resolve the request by returning an `ErrorEvent` (e.g. simulating a network failure).
|
2020-06-22 08:09:47 +00:00
|
|
|
* @deprecated Http requests never emit an `ErrorEvent`. Please specify a `ProgressEvent`.
|
|
|
|
|
*/
|
|
|
|
|
error(error: ErrorEvent, opts?: TestRequestErrorOptions): void;
|
|
|
|
|
/**
|
|
|
|
|
* Resolve the request by returning an `ProgressEvent` (e.g. simulating a network failure).
|
2017-07-13 00:00:23 +00:00
|
|
|
*/
|
2020-06-22 08:09:47 +00:00
|
|
|
error(error: ProgressEvent, opts?: TestRequestErrorOptions): void;
|
2024-01-29 21:06:27 +00:00
|
|
|
error(error: ProgressEvent | ErrorEvent, opts: TestRequestErrorOptions = {}): void {
|
2017-03-23 00:13:24 +00:00
|
|
|
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 =
|
2024-01-29 21:06:27 +00:00
|
|
|
opts.headers instanceof HttpHeaders ? opts.headers : new HttpHeaders(opts.headers);
|
|
|
|
|
this.observer.error(
|
|
|
|
|
new HttpErrorResponse({
|
|
|
|
|
error,
|
|
|
|
|
headers,
|
|
|
|
|
status: opts.status || 0,
|
|
|
|
|
statusText: opts.statusText || '',
|
|
|
|
|
url: this.request.urlWithParams,
|
|
|
|
|
}),
|
|
|
|
|
);
|
2017-03-23 00:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
2017-07-13 00:00:23 +00:00
|
|
|
/**
|
|
|
|
|
* Deliver an arbitrary `HttpEvent` (such as a progress event) on the response stream for this
|
|
|
|
|
* request.
|
|
|
|
|
*/
|
2017-03-23 00:13:24 +00:00
|
|
|
event(event: HttpEvent<any>): void {
|
|
|
|
|
if (this.cancelled) {
|
|
|
|
|
throw new Error(`Cannot send events to a cancelled request.`);
|
|
|
|
|
}
|
|
|
|
|
this.observer.next(event);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to convert a response body to an ArrayBuffer.
|
|
|
|
|
*/
|
2024-01-29 21:06:27 +00:00
|
|
|
function _toArrayBufferBody(
|
|
|
|
|
body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[],
|
|
|
|
|
): ArrayBuffer {
|
2017-03-23 00:13:24 +00:00
|
|
|
if (typeof ArrayBuffer === 'undefined') {
|
|
|
|
|
throw new Error('ArrayBuffer responses are not supported on this platform.');
|
|
|
|
|
}
|
|
|
|
|
if (body instanceof ArrayBuffer) {
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
throw new Error('Automatic conversion to ArrayBuffer is not supported for response type.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to convert a response body to a Blob.
|
|
|
|
|
*/
|
2024-01-29 21:06:27 +00:00
|
|
|
function _toBlob(
|
|
|
|
|
body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[],
|
|
|
|
|
): Blob {
|
2017-03-23 00:13:24 +00:00
|
|
|
if (typeof Blob === 'undefined') {
|
|
|
|
|
throw new Error('Blob responses are not supported on this platform.');
|
|
|
|
|
}
|
|
|
|
|
if (body instanceof Blob) {
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
if (ArrayBuffer && body instanceof ArrayBuffer) {
|
|
|
|
|
return new Blob([body]);
|
|
|
|
|
}
|
|
|
|
|
throw new Error('Automatic conversion to Blob is not supported for response type.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to convert a response body to JSON data.
|
|
|
|
|
*/
|
|
|
|
|
function _toJsonBody(
|
2024-01-29 21:06:27 +00:00
|
|
|
body:
|
|
|
|
|
| ArrayBuffer
|
|
|
|
|
| Blob
|
|
|
|
|
| boolean
|
|
|
|
|
| string
|
|
|
|
|
| number
|
|
|
|
|
| Object
|
|
|
|
|
| (boolean | string | number | Object | null)[],
|
|
|
|
|
format: string = 'JSON',
|
|
|
|
|
): Object | string | number | (Object | string | number)[] {
|
2017-03-23 00:13:24 +00:00
|
|
|
if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
|
|
|
|
|
throw new Error(`Automatic conversion to ${format} is not supported for ArrayBuffers.`);
|
|
|
|
|
}
|
|
|
|
|
if (typeof Blob !== 'undefined' && body instanceof Blob) {
|
|
|
|
|
throw new Error(`Automatic conversion to ${format} is not supported for Blobs.`);
|
|
|
|
|
}
|
2024-01-29 21:06:27 +00:00
|
|
|
if (
|
|
|
|
|
typeof body === 'string' ||
|
|
|
|
|
typeof body === 'number' ||
|
|
|
|
|
typeof body === 'object' ||
|
|
|
|
|
typeof body === 'boolean' ||
|
|
|
|
|
Array.isArray(body)
|
|
|
|
|
) {
|
2017-03-23 00:13:24 +00:00
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
throw new Error(`Automatic conversion to ${format} is not supported for response type.`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Helper function to convert a response body to a string.
|
|
|
|
|
*/
|
2024-01-29 21:06:27 +00:00
|
|
|
function _toTextBody(
|
|
|
|
|
body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[],
|
|
|
|
|
): string {
|
2017-03-23 00:13:24 +00:00
|
|
|
if (typeof body === 'string') {
|
|
|
|
|
return body;
|
|
|
|
|
}
|
|
|
|
|
if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
|
|
|
|
|
throw new Error('Automatic conversion to text is not supported for ArrayBuffers.');
|
|
|
|
|
}
|
|
|
|
|
if (typeof Blob !== 'undefined' && body instanceof Blob) {
|
|
|
|
|
throw new Error('Automatic conversion to text is not supported for Blobs.');
|
|
|
|
|
}
|
|
|
|
|
return JSON.stringify(_toJsonBody(body, 'text'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Convert a response body to the requested type.
|
|
|
|
|
*/
|
|
|
|
|
function _maybeConvertBody(
|
2024-01-29 21:06:27 +00:00
|
|
|
responseType: string,
|
|
|
|
|
body: ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[] | null,
|
|
|
|
|
): ArrayBuffer | Blob | string | number | Object | (string | number | Object | null)[] | null {
|
2018-01-09 16:25:01 +00:00
|
|
|
if (body === null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2017-03-23 00:13:24 +00:00
|
|
|
switch (responseType) {
|
|
|
|
|
case 'arraybuffer':
|
|
|
|
|
return _toArrayBufferBody(body);
|
|
|
|
|
case 'blob':
|
|
|
|
|
return _toBlob(body);
|
|
|
|
|
case 'json':
|
|
|
|
|
return _toJsonBody(body);
|
|
|
|
|
case 'text':
|
|
|
|
|
return _toTextBody(body);
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unsupported responseType: ${responseType}`);
|
|
|
|
|
}
|
|
|
|
|
}
|