Project

General

Profile

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