common-http-testing.umd.js 18 KB

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