1 | /**
|
---|
2 | * @license Angular v12.2.13
|
---|
3 | * (c) 2010-2021 Google LLC. https://angular.io/
|
---|
4 | * License: MIT
|
---|
5 | */
|
---|
6 |
|
---|
7 | (function (global, factory) {
|
---|
8 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common/http'), require('@angular/core'), require('rxjs')) :
|
---|
9 | typeof define === 'function' && define.amd ? define('@angular/common/http/testing', ['exports', '@angular/common/http', '@angular/core', 'rxjs'], factory) :
|
---|
10 | (global = global || self, factory((global.ng = global.ng || {}, global.ng.common = global.ng.common || {}, global.ng.common.http = global.ng.common.http || {}, global.ng.common.http.testing = {}), global.ng.common.http, global.ng.core, global.rxjs));
|
---|
11 | }(this, (function (exports, http, core, rxjs) { 'use strict';
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * @license
|
---|
15 | * Copyright Google LLC All Rights Reserved.
|
---|
16 | *
|
---|
17 | * Use of this source code is governed by an MIT-style license that can be
|
---|
18 | * found in the LICENSE file at https://angular.io/license
|
---|
19 | */
|
---|
20 | /**
|
---|
21 | * Controller to be injected into tests, that allows for mocking and flushing
|
---|
22 | * of requests.
|
---|
23 | *
|
---|
24 | * @publicApi
|
---|
25 | */
|
---|
26 | var HttpTestingController = /** @class */ (function () {
|
---|
27 | function HttpTestingController() {
|
---|
28 | }
|
---|
29 | return HttpTestingController;
|
---|
30 | }());
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * @license
|
---|
34 | * Copyright Google LLC All Rights Reserved.
|
---|
35 | *
|
---|
36 | * Use of this source code is governed by an MIT-style license that can be
|
---|
37 | * found in the LICENSE file at https://angular.io/license
|
---|
38 | */
|
---|
39 | /**
|
---|
40 | * A mock requests that was received and is ready to be answered.
|
---|
41 | *
|
---|
42 | * This interface allows access to the underlying `HttpRequest`, and allows
|
---|
43 | * responding with `HttpEvent`s or `HttpErrorResponse`s.
|
---|
44 | *
|
---|
45 | * @publicApi
|
---|
46 | */
|
---|
47 | var TestRequest = /** @class */ (function () {
|
---|
48 | function TestRequest(request, observer) {
|
---|
49 | this.request = request;
|
---|
50 | this.observer = observer;
|
---|
51 | /**
|
---|
52 | * @internal set by `HttpClientTestingBackend`
|
---|
53 | */
|
---|
54 | this._cancelled = false;
|
---|
55 | }
|
---|
56 | Object.defineProperty(TestRequest.prototype, "cancelled", {
|
---|
57 | /**
|
---|
58 | * Whether the request was cancelled after it was sent.
|
---|
59 | */
|
---|
60 | get: function () {
|
---|
61 | return this._cancelled;
|
---|
62 | },
|
---|
63 | enumerable: false,
|
---|
64 | configurable: true
|
---|
65 | });
|
---|
66 | /**
|
---|
67 | * Resolve the request by returning a body plus additional HTTP information (such as response
|
---|
68 | * headers) if provided.
|
---|
69 | * If the request specifies an expected body type, the body is converted into the requested type.
|
---|
70 | * Otherwise, the body is converted to `JSON` by default.
|
---|
71 | *
|
---|
72 | * Both successful and unsuccessful responses can be delivered via `flush()`.
|
---|
73 | */
|
---|
74 | TestRequest.prototype.flush = function (body, opts) {
|
---|
75 | if (opts === void 0) { opts = {}; }
|
---|
76 | if (this.cancelled) {
|
---|
77 | throw new Error("Cannot flush a cancelled request.");
|
---|
78 | }
|
---|
79 | var url = this.request.urlWithParams;
|
---|
80 | var headers = (opts.headers instanceof http.HttpHeaders) ? opts.headers : new http.HttpHeaders(opts.headers);
|
---|
81 | body = _maybeConvertBody(this.request.responseType, body);
|
---|
82 | var statusText = opts.statusText;
|
---|
83 | var status = opts.status !== undefined ? opts.status : 200 /* Ok */;
|
---|
84 | if (opts.status === undefined) {
|
---|
85 | if (body === null) {
|
---|
86 | status = 204 /* NoContent */;
|
---|
87 | statusText = statusText || 'No Content';
|
---|
88 | }
|
---|
89 | else {
|
---|
90 | statusText = statusText || 'OK';
|
---|
91 | }
|
---|
92 | }
|
---|
93 | if (statusText === undefined) {
|
---|
94 | throw new Error('statusText is required when setting a custom status.');
|
---|
95 | }
|
---|
96 | if (status >= 200 && status < 300) {
|
---|
97 | this.observer.next(new http.HttpResponse({ body: body, headers: headers, status: status, statusText: statusText, url: url }));
|
---|
98 | this.observer.complete();
|
---|
99 | }
|
---|
100 | else {
|
---|
101 | this.observer.error(new http.HttpErrorResponse({ error: body, headers: headers, status: status, statusText: statusText, url: url }));
|
---|
102 | }
|
---|
103 | };
|
---|
104 | /**
|
---|
105 | * Resolve the request by returning an `ErrorEvent` (e.g. simulating a network failure).
|
---|
106 | */
|
---|
107 | TestRequest.prototype.error = function (error, opts) {
|
---|
108 | if (opts === void 0) { opts = {}; }
|
---|
109 | if (this.cancelled) {
|
---|
110 | throw new Error("Cannot return an error for a cancelled request.");
|
---|
111 | }
|
---|
112 | if (opts.status && opts.status >= 200 && opts.status < 300) {
|
---|
113 | throw new Error("error() called with a successful status.");
|
---|
114 | }
|
---|
115 | var headers = (opts.headers instanceof http.HttpHeaders) ? opts.headers : new http.HttpHeaders(opts.headers);
|
---|
116 | this.observer.error(new http.HttpErrorResponse({
|
---|
117 | error: error,
|
---|
118 | headers: headers,
|
---|
119 | status: opts.status || 0,
|
---|
120 | statusText: opts.statusText || '',
|
---|
121 | url: this.request.urlWithParams,
|
---|
122 | }));
|
---|
123 | };
|
---|
124 | /**
|
---|
125 | * Deliver an arbitrary `HttpEvent` (such as a progress event) on the response stream for this
|
---|
126 | * request.
|
---|
127 | */
|
---|
128 | TestRequest.prototype.event = function (event) {
|
---|
129 | if (this.cancelled) {
|
---|
130 | throw new Error("Cannot send events to a cancelled request.");
|
---|
131 | }
|
---|
132 | this.observer.next(event);
|
---|
133 | };
|
---|
134 | return TestRequest;
|
---|
135 | }());
|
---|
136 | /**
|
---|
137 | * Helper function to convert a response body to an ArrayBuffer.
|
---|
138 | */
|
---|
139 | function _toArrayBufferBody(body) {
|
---|
140 | if (typeof ArrayBuffer === 'undefined') {
|
---|
141 | throw new Error('ArrayBuffer responses are not supported on this platform.');
|
---|
142 | }
|
---|
143 | if (body instanceof ArrayBuffer) {
|
---|
144 | return body;
|
---|
145 | }
|
---|
146 | throw new Error('Automatic conversion to ArrayBuffer is not supported for response type.');
|
---|
147 | }
|
---|
148 | /**
|
---|
149 | * Helper function to convert a response body to a Blob.
|
---|
150 | */
|
---|
151 | function _toBlob(body) {
|
---|
152 | if (typeof Blob === 'undefined') {
|
---|
153 | throw new Error('Blob responses are not supported on this platform.');
|
---|
154 | }
|
---|
155 | if (body instanceof Blob) {
|
---|
156 | return body;
|
---|
157 | }
|
---|
158 | if (ArrayBuffer && body instanceof ArrayBuffer) {
|
---|
159 | return new Blob([body]);
|
---|
160 | }
|
---|
161 | throw new Error('Automatic conversion to Blob is not supported for response type.');
|
---|
162 | }
|
---|
163 | /**
|
---|
164 | * Helper function to convert a response body to JSON data.
|
---|
165 | */
|
---|
166 | function _toJsonBody(body, format) {
|
---|
167 | if (format === void 0) { format = 'JSON'; }
|
---|
168 | if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
|
---|
169 | throw new Error("Automatic conversion to " + format + " is not supported for ArrayBuffers.");
|
---|
170 | }
|
---|
171 | if (typeof Blob !== 'undefined' && body instanceof Blob) {
|
---|
172 | throw new Error("Automatic conversion to " + format + " is not supported for Blobs.");
|
---|
173 | }
|
---|
174 | if (typeof body === 'string' || typeof body === 'number' || typeof body === 'object' ||
|
---|
175 | typeof body === 'boolean' || Array.isArray(body)) {
|
---|
176 | return body;
|
---|
177 | }
|
---|
178 | throw new Error("Automatic conversion to " + format + " is not supported for response type.");
|
---|
179 | }
|
---|
180 | /**
|
---|
181 | * Helper function to convert a response body to a string.
|
---|
182 | */
|
---|
183 | function _toTextBody(body) {
|
---|
184 | if (typeof body === 'string') {
|
---|
185 | return body;
|
---|
186 | }
|
---|
187 | if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
|
---|
188 | throw new Error('Automatic conversion to text is not supported for ArrayBuffers.');
|
---|
189 | }
|
---|
190 | if (typeof Blob !== 'undefined' && body instanceof Blob) {
|
---|
191 | throw new Error('Automatic conversion to text is not supported for Blobs.');
|
---|
192 | }
|
---|
193 | return JSON.stringify(_toJsonBody(body, 'text'));
|
---|
194 | }
|
---|
195 | /**
|
---|
196 | * Convert a response body to the requested type.
|
---|
197 | */
|
---|
198 | function _maybeConvertBody(responseType, body) {
|
---|
199 | if (body === null) {
|
---|
200 | return null;
|
---|
201 | }
|
---|
202 | switch (responseType) {
|
---|
203 | case 'arraybuffer':
|
---|
204 | return _toArrayBufferBody(body);
|
---|
205 | case 'blob':
|
---|
206 | return _toBlob(body);
|
---|
207 | case 'json':
|
---|
208 | return _toJsonBody(body);
|
---|
209 | case 'text':
|
---|
210 | return _toTextBody(body);
|
---|
211 | default:
|
---|
212 | throw new Error("Unsupported responseType: " + responseType);
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * @license
|
---|
218 | * Copyright Google LLC All Rights Reserved.
|
---|
219 | *
|
---|
220 | * Use of this source code is governed by an MIT-style license that can be
|
---|
221 | * found in the LICENSE file at https://angular.io/license
|
---|
222 | */
|
---|
223 | /**
|
---|
224 | * A testing backend for `HttpClient` which both acts as an `HttpBackend`
|
---|
225 | * and as the `HttpTestingController`.
|
---|
226 | *
|
---|
227 | * `HttpClientTestingBackend` works by keeping a list of all open requests.
|
---|
228 | * As requests come in, they're added to the list. Users can assert that specific
|
---|
229 | * requests were made and then flush them. In the end, a verify() method asserts
|
---|
230 | * that no unexpected requests were made.
|
---|
231 | *
|
---|
232 | *
|
---|
233 | */
|
---|
234 | var HttpClientTestingBackend = /** @class */ (function () {
|
---|
235 | function HttpClientTestingBackend() {
|
---|
236 | /**
|
---|
237 | * List of pending requests which have not yet been expected.
|
---|
238 | */
|
---|
239 | this.open = [];
|
---|
240 | }
|
---|
241 | /**
|
---|
242 | * Handle an incoming request by queueing it in the list of open requests.
|
---|
243 | */
|
---|
244 | HttpClientTestingBackend.prototype.handle = function (req) {
|
---|
245 | var _this = this;
|
---|
246 | return new rxjs.Observable(function (observer) {
|
---|
247 | var testReq = new TestRequest(req, observer);
|
---|
248 | _this.open.push(testReq);
|
---|
249 | observer.next({ type: http.HttpEventType.Sent });
|
---|
250 | return function () {
|
---|
251 | testReq._cancelled = true;
|
---|
252 | };
|
---|
253 | });
|
---|
254 | };
|
---|
255 | /**
|
---|
256 | * Helper function to search for requests in the list of open requests.
|
---|
257 | */
|
---|
258 | HttpClientTestingBackend.prototype._match = function (match) {
|
---|
259 | if (typeof match === 'string') {
|
---|
260 | return this.open.filter(function (testReq) { return testReq.request.urlWithParams === match; });
|
---|
261 | }
|
---|
262 | else if (typeof match === 'function') {
|
---|
263 | return this.open.filter(function (testReq) { return match(testReq.request); });
|
---|
264 | }
|
---|
265 | else {
|
---|
266 | return this.open.filter(function (testReq) { return (!match.method || testReq.request.method === match.method.toUpperCase()) &&
|
---|
267 | (!match.url || testReq.request.urlWithParams === match.url); });
|
---|
268 | }
|
---|
269 | };
|
---|
270 | /**
|
---|
271 | * Search for requests in the list of open requests, and return all that match
|
---|
272 | * without asserting anything about the number of matches.
|
---|
273 | */
|
---|
274 | HttpClientTestingBackend.prototype.match = function (match) {
|
---|
275 | var _this = this;
|
---|
276 | var results = this._match(match);
|
---|
277 | results.forEach(function (result) {
|
---|
278 | var index = _this.open.indexOf(result);
|
---|
279 | if (index !== -1) {
|
---|
280 | _this.open.splice(index, 1);
|
---|
281 | }
|
---|
282 | });
|
---|
283 | return results;
|
---|
284 | };
|
---|
285 | /**
|
---|
286 | * Expect that a single outstanding request matches the given matcher, and return
|
---|
287 | * it.
|
---|
288 | *
|
---|
289 | * Requests returned through this API will no longer be in the list of open requests,
|
---|
290 | * and thus will not match twice.
|
---|
291 | */
|
---|
292 | HttpClientTestingBackend.prototype.expectOne = function (match, description) {
|
---|
293 | description = description || this.descriptionFromMatcher(match);
|
---|
294 | var matches = this.match(match);
|
---|
295 | if (matches.length > 1) {
|
---|
296 | throw new Error("Expected one matching request for criteria \"" + description + "\", found " + matches.length + " requests.");
|
---|
297 | }
|
---|
298 | if (matches.length === 0) {
|
---|
299 | var message = "Expected one matching request for criteria \"" + description + "\", found none.";
|
---|
300 | if (this.open.length > 0) {
|
---|
301 | // Show the methods and URLs of open requests in the error, for convenience.
|
---|
302 | var requests = this.open
|
---|
303 | .map(function (testReq) {
|
---|
304 | var url = testReq.request.urlWithParams;
|
---|
305 | var method = testReq.request.method;
|
---|
306 | return method + " " + url;
|
---|
307 | })
|
---|
308 | .join(', ');
|
---|
309 | message += " Requests received are: " + requests + ".";
|
---|
310 | }
|
---|
311 | throw new Error(message);
|
---|
312 | }
|
---|
313 | return matches[0];
|
---|
314 | };
|
---|
315 | /**
|
---|
316 | * Expect that no outstanding requests match the given matcher, and throw an error
|
---|
317 | * if any do.
|
---|
318 | */
|
---|
319 | HttpClientTestingBackend.prototype.expectNone = function (match, description) {
|
---|
320 | description = description || this.descriptionFromMatcher(match);
|
---|
321 | var matches = this.match(match);
|
---|
322 | if (matches.length > 0) {
|
---|
323 | throw new Error("Expected zero matching requests for criteria \"" + description + "\", found " + matches.length + ".");
|
---|
324 | }
|
---|
325 | };
|
---|
326 | /**
|
---|
327 | * Validate that there are no outstanding requests.
|
---|
328 | */
|
---|
329 | HttpClientTestingBackend.prototype.verify = function (opts) {
|
---|
330 | if (opts === void 0) { opts = {}; }
|
---|
331 | var open = this.open;
|
---|
332 | // It's possible that some requests may be cancelled, and this is expected.
|
---|
333 | // The user can ask to ignore open requests which have been cancelled.
|
---|
334 | if (opts.ignoreCancelled) {
|
---|
335 | open = open.filter(function (testReq) { return !testReq.cancelled; });
|
---|
336 | }
|
---|
337 | if (open.length > 0) {
|
---|
338 | // Show the methods and URLs of open requests in the error, for convenience.
|
---|
339 | var requests = open.map(function (testReq) {
|
---|
340 | var url = testReq.request.urlWithParams.split('?')[0];
|
---|
341 | var method = testReq.request.method;
|
---|
342 | return method + " " + url;
|
---|
343 | })
|
---|
344 | .join(', ');
|
---|
345 | throw new Error("Expected no open requests, found " + open.length + ": " + requests);
|
---|
346 | }
|
---|
347 | };
|
---|
348 | HttpClientTestingBackend.prototype.descriptionFromMatcher = function (matcher) {
|
---|
349 | if (typeof matcher === 'string') {
|
---|
350 | return "Match URL: " + matcher;
|
---|
351 | }
|
---|
352 | else if (typeof matcher === 'object') {
|
---|
353 | var method = matcher.method || '(any)';
|
---|
354 | var url = matcher.url || '(any)';
|
---|
355 | return "Match method: " + method + ", URL: " + url;
|
---|
356 | }
|
---|
357 | else {
|
---|
358 | return "Match by function: " + matcher.name;
|
---|
359 | }
|
---|
360 | };
|
---|
361 | return HttpClientTestingBackend;
|
---|
362 | }());
|
---|
363 | HttpClientTestingBackend.decorators = [
|
---|
364 | { type: core.Injectable }
|
---|
365 | ];
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * @license
|
---|
369 | * Copyright Google LLC All Rights Reserved.
|
---|
370 | *
|
---|
371 | * Use of this source code is governed by an MIT-style license that can be
|
---|
372 | * found in the LICENSE file at https://angular.io/license
|
---|
373 | */
|
---|
374 | /**
|
---|
375 | * Configures `HttpClientTestingBackend` as the `HttpBackend` used by `HttpClient`.
|
---|
376 | *
|
---|
377 | * Inject `HttpTestingController` to expect and flush requests in your tests.
|
---|
378 | *
|
---|
379 | * @publicApi
|
---|
380 | */
|
---|
381 | var HttpClientTestingModule = /** @class */ (function () {
|
---|
382 | function HttpClientTestingModule() {
|
---|
383 | }
|
---|
384 | return HttpClientTestingModule;
|
---|
385 | }());
|
---|
386 | HttpClientTestingModule.decorators = [
|
---|
387 | { type: core.NgModule, args: [{
|
---|
388 | imports: [
|
---|
389 | http.HttpClientModule,
|
---|
390 | ],
|
---|
391 | providers: [
|
---|
392 | HttpClientTestingBackend,
|
---|
393 | { provide: http.HttpBackend, useExisting: HttpClientTestingBackend },
|
---|
394 | { provide: HttpTestingController, useExisting: HttpClientTestingBackend },
|
---|
395 | ],
|
---|
396 | },] }
|
---|
397 | ];
|
---|
398 |
|
---|
399 | /**
|
---|
400 | * @license
|
---|
401 | * Copyright Google LLC All Rights Reserved.
|
---|
402 | *
|
---|
403 | * Use of this source code is governed by an MIT-style license that can be
|
---|
404 | * found in the LICENSE file at https://angular.io/license
|
---|
405 | */
|
---|
406 |
|
---|
407 | /**
|
---|
408 | * @license
|
---|
409 | * Copyright Google LLC All Rights Reserved.
|
---|
410 | *
|
---|
411 | * Use of this source code is governed by an MIT-style license that can be
|
---|
412 | * found in the LICENSE file at https://angular.io/license
|
---|
413 | */
|
---|
414 |
|
---|
415 | /**
|
---|
416 | * Generated bundle index. Do not edit.
|
---|
417 | */
|
---|
418 |
|
---|
419 | exports.HttpClientTestingModule = HttpClientTestingModule;
|
---|
420 | exports.HttpTestingController = HttpTestingController;
|
---|
421 | exports.TestRequest = TestRequest;
|
---|
422 | exports.ɵangular_packages_common_http_testing_testing_a = HttpClientTestingBackend;
|
---|
423 |
|
---|
424 | Object.defineProperty(exports, '__esModule', { value: true });
|
---|
425 |
|
---|
426 | })));
|
---|
427 | //# sourceMappingURL=common-http-testing.umd.js.map
|
---|