testing.mjs 13 KB

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