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