Project

General

Profile

1
/**
2
 * @license Angular v4.4.6
3
 * (c) 2010-2017 Google, Inc. https://angular.io/
4
 * License: MIT
5
 */
6
(function (global, factory) {
7
	typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common/http'), require('@angular/core'), require('rxjs/Observable')) :
8
	typeof define === 'function' && define.amd ? define(['exports', '@angular/common/http', '@angular/core', 'rxjs/Observable'], factory) :
9
	(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.testing || {}),global.ng.common.http,global.ng.core,global.Rx));
10
}(this, (function (exports,_angular_common_http,_angular_core,rxjs_Observable) { 'use strict';
11

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

    
555
exports.HttpTestingController = HttpTestingController;
556
exports.HttpClientTestingModule = HttpClientTestingModule;
557
exports.TestRequest = TestRequest;
558
exports.ɵa = HttpClientTestingBackend;
559

    
560
Object.defineProperty(exports, '__esModule', { value: true });
561

    
562
})));
563
//# sourceMappingURL=common-http-testing.umd.js.map
(1-1/16)