1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 | (function (global, factory) {
|
8 | typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/common/http'), require('@angular/core'), require('rxjs')) :
|
9 | typeof define === 'function' && define.amd ? define('@angular/common/http/testing', ['exports', '@angular/common/http', '@angular/core', 'rxjs'], factory) :
|
10 | (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));
|
11 | }(this, function (exports, http, core, rxjs) { 'use strict';
|
12 |
|
13 | |
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | |
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 | var HttpTestingController = (function () {
|
27 | function HttpTestingController() {
|
28 | }
|
29 | return HttpTestingController;
|
30 | }());
|
31 |
|
32 | |
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 | function __decorate(decorators, target, key, desc) {
|
48 | var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
49 | if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
50 | 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;
|
51 | return c > 3 && r && Object.defineProperty(target, key, r), r;
|
52 | }
|
53 |
|
54 | |
55 |
|
56 |
|
57 |
|
58 |
|
59 |
|
60 |
|
61 | |
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | var TestRequest = (function () {
|
70 | function TestRequest(request, observer) {
|
71 | this.request = request;
|
72 | this.observer = observer;
|
73 | |
74 |
|
75 |
|
76 | this._cancelled = false;
|
77 | }
|
78 | Object.defineProperty(TestRequest.prototype, "cancelled", {
|
79 | |
80 |
|
81 |
|
82 | get: function () { return this._cancelled; },
|
83 | enumerable: true,
|
84 | configurable: true
|
85 | });
|
86 | |
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 |
|
94 | TestRequest.prototype.flush = function (body, opts) {
|
95 | if (opts === void 0) { opts = {}; }
|
96 | if (this.cancelled) {
|
97 | throw new Error("Cannot flush a cancelled request.");
|
98 | }
|
99 | var url = this.request.urlWithParams;
|
100 | var headers = (opts.headers instanceof http.HttpHeaders) ? opts.headers : new http.HttpHeaders(opts.headers);
|
101 | body = _maybeConvertBody(this.request.responseType, body);
|
102 | var statusText = opts.statusText;
|
103 | var status = opts.status !== undefined ? opts.status : 200;
|
104 | if (opts.status === undefined) {
|
105 | if (body === null) {
|
106 | status = 204;
|
107 | statusText = statusText || 'No Content';
|
108 | }
|
109 | else {
|
110 | statusText = statusText || 'OK';
|
111 | }
|
112 | }
|
113 | if (statusText === undefined) {
|
114 | throw new Error('statusText is required when setting a custom status.');
|
115 | }
|
116 | if (status >= 200 && status < 300) {
|
117 | this.observer.next(new http.HttpResponse({ body: body, headers: headers, status: status, statusText: statusText, url: url }));
|
118 | this.observer.complete();
|
119 | }
|
120 | else {
|
121 | this.observer.error(new http.HttpErrorResponse({ error: body, headers: headers, status: status, statusText: statusText, url: url }));
|
122 | }
|
123 | };
|
124 | |
125 |
|
126 |
|
127 | TestRequest.prototype.error = function (error, opts) {
|
128 | if (opts === void 0) { opts = {}; }
|
129 | if (this.cancelled) {
|
130 | throw new Error("Cannot return an error for a cancelled request.");
|
131 | }
|
132 | if (opts.status && opts.status >= 200 && opts.status < 300) {
|
133 | throw new Error("error() called with a successful status.");
|
134 | }
|
135 | var headers = (opts.headers instanceof http.HttpHeaders) ? opts.headers : new http.HttpHeaders(opts.headers);
|
136 | this.observer.error(new http.HttpErrorResponse({
|
137 | error: error,
|
138 | headers: headers,
|
139 | status: opts.status || 0,
|
140 | statusText: opts.statusText || '',
|
141 | url: this.request.urlWithParams,
|
142 | }));
|
143 | };
|
144 | |
145 |
|
146 |
|
147 |
|
148 | TestRequest.prototype.event = function (event) {
|
149 | if (this.cancelled) {
|
150 | throw new Error("Cannot send events to a cancelled request.");
|
151 | }
|
152 | this.observer.next(event);
|
153 | };
|
154 | return TestRequest;
|
155 | }());
|
156 | |
157 |
|
158 |
|
159 | function _toArrayBufferBody(body) {
|
160 | if (typeof ArrayBuffer === 'undefined') {
|
161 | throw new Error('ArrayBuffer responses are not supported on this platform.');
|
162 | }
|
163 | if (body instanceof ArrayBuffer) {
|
164 | return body;
|
165 | }
|
166 | throw new Error('Automatic conversion to ArrayBuffer is not supported for response type.');
|
167 | }
|
168 | |
169 |
|
170 |
|
171 | function _toBlob(body) {
|
172 | if (typeof Blob === 'undefined') {
|
173 | throw new Error('Blob responses are not supported on this platform.');
|
174 | }
|
175 | if (body instanceof Blob) {
|
176 | return body;
|
177 | }
|
178 | if (ArrayBuffer && body instanceof ArrayBuffer) {
|
179 | return new Blob([body]);
|
180 | }
|
181 | throw new Error('Automatic conversion to Blob is not supported for response type.');
|
182 | }
|
183 | |
184 |
|
185 |
|
186 | function _toJsonBody(body, format) {
|
187 | if (format === void 0) { format = 'JSON'; }
|
188 | if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
|
189 | throw new Error("Automatic conversion to " + format + " is not supported for ArrayBuffers.");
|
190 | }
|
191 | if (typeof Blob !== 'undefined' && body instanceof Blob) {
|
192 | throw new Error("Automatic conversion to " + format + " is not supported for Blobs.");
|
193 | }
|
194 | if (typeof body === 'string' || typeof body === 'number' || typeof body === 'object' ||
|
195 | Array.isArray(body)) {
|
196 | return body;
|
197 | }
|
198 | throw new Error("Automatic conversion to " + format + " is not supported for response type.");
|
199 | }
|
200 | |
201 |
|
202 |
|
203 | function _toTextBody(body) {
|
204 | if (typeof body === 'string') {
|
205 | return body;
|
206 | }
|
207 | if (typeof ArrayBuffer !== 'undefined' && body instanceof ArrayBuffer) {
|
208 | throw new Error('Automatic conversion to text is not supported for ArrayBuffers.');
|
209 | }
|
210 | if (typeof Blob !== 'undefined' && body instanceof Blob) {
|
211 | throw new Error('Automatic conversion to text is not supported for Blobs.');
|
212 | }
|
213 | return JSON.stringify(_toJsonBody(body, 'text'));
|
214 | }
|
215 | |
216 |
|
217 |
|
218 | function _maybeConvertBody(responseType, body) {
|
219 | if (body === null) {
|
220 | return null;
|
221 | }
|
222 | switch (responseType) {
|
223 | case 'arraybuffer':
|
224 | return _toArrayBufferBody(body);
|
225 | case 'blob':
|
226 | return _toBlob(body);
|
227 | case 'json':
|
228 | return _toJsonBody(body);
|
229 | case 'text':
|
230 | return _toTextBody(body);
|
231 | default:
|
232 | throw new Error("Unsupported responseType: " + responseType);
|
233 | }
|
234 | }
|
235 |
|
236 | |
237 |
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 | |
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 | var HttpClientTestingBackend = (function () {
|
255 | function HttpClientTestingBackend() {
|
256 | |
257 |
|
258 |
|
259 | this.open = [];
|
260 | }
|
261 | |
262 |
|
263 |
|
264 | HttpClientTestingBackend.prototype.handle = function (req) {
|
265 | var _this = this;
|
266 | return new rxjs.Observable(function (observer) {
|
267 | var testReq = new TestRequest(req, observer);
|
268 | _this.open.push(testReq);
|
269 | observer.next({ type: http.HttpEventType.Sent });
|
270 | return function () { testReq._cancelled = true; };
|
271 | });
|
272 | };
|
273 | |
274 |
|
275 |
|
276 | HttpClientTestingBackend.prototype._match = function (match) {
|
277 | if (typeof match === 'string') {
|
278 | return this.open.filter(function (testReq) { return testReq.request.urlWithParams === match; });
|
279 | }
|
280 | else if (typeof match === 'function') {
|
281 | return this.open.filter(function (testReq) { return match(testReq.request); });
|
282 | }
|
283 | else {
|
284 | return this.open.filter(function (testReq) { return (!match.method || testReq.request.method === match.method.toUpperCase()) &&
|
285 | (!match.url || testReq.request.urlWithParams === match.url); });
|
286 | }
|
287 | };
|
288 | |
289 |
|
290 |
|
291 |
|
292 | HttpClientTestingBackend.prototype.match = function (match) {
|
293 | var _this = this;
|
294 | var results = this._match(match);
|
295 | results.forEach(function (result) {
|
296 | var index = _this.open.indexOf(result);
|
297 | if (index !== -1) {
|
298 | _this.open.splice(index, 1);
|
299 | }
|
300 | });
|
301 | return results;
|
302 | };
|
303 | |
304 |
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 | HttpClientTestingBackend.prototype.expectOne = function (match, description) {
|
311 | description = description || this.descriptionFromMatcher(match);
|
312 | var matches = this.match(match);
|
313 | if (matches.length > 1) {
|
314 | throw new Error("Expected one matching request for criteria \"" + description + "\", found " + matches.length + " requests.");
|
315 | }
|
316 | if (matches.length === 0) {
|
317 | throw new Error("Expected one matching request for criteria \"" + description + "\", found none.");
|
318 | }
|
319 | return matches[0];
|
320 | };
|
321 | |
322 |
|
323 |
|
324 |
|
325 | HttpClientTestingBackend.prototype.expectNone = function (match, description) {
|
326 | description = description || this.descriptionFromMatcher(match);
|
327 | var matches = this.match(match);
|
328 | if (matches.length > 0) {
|
329 | throw new Error("Expected zero matching requests for criteria \"" + description + "\", found " + matches.length + ".");
|
330 | }
|
331 | };
|
332 | |
333 |
|
334 |
|
335 | HttpClientTestingBackend.prototype.verify = function (opts) {
|
336 | if (opts === void 0) { opts = {}; }
|
337 | var open = this.open;
|
338 |
|
339 |
|
340 | if (opts.ignoreCancelled) {
|
341 | open = open.filter(function (testReq) { return !testReq.cancelled; });
|
342 | }
|
343 | if (open.length > 0) {
|
344 |
|
345 | var requests = open.map(function (testReq) {
|
346 | var url = testReq.request.urlWithParams.split('?')[0];
|
347 | var method = testReq.request.method;
|
348 | return method + " " + url;
|
349 | })
|
350 | .join(', ');
|
351 | throw new Error("Expected no open requests, found " + open.length + ": " + requests);
|
352 | }
|
353 | };
|
354 | HttpClientTestingBackend.prototype.descriptionFromMatcher = function (matcher) {
|
355 | if (typeof matcher === 'string') {
|
356 | return "Match URL: " + matcher;
|
357 | }
|
358 | else if (typeof matcher === 'object') {
|
359 | var method = matcher.method || '(any)';
|
360 | var url = matcher.url || '(any)';
|
361 | return "Match method: " + method + ", URL: " + url;
|
362 | }
|
363 | else {
|
364 | return "Match by function: " + matcher.name;
|
365 | }
|
366 | };
|
367 | HttpClientTestingBackend = __decorate([
|
368 | core.Injectable()
|
369 | ], HttpClientTestingBackend);
|
370 | return HttpClientTestingBackend;
|
371 | }());
|
372 |
|
373 | |
374 |
|
375 |
|
376 |
|
377 |
|
378 |
|
379 |
|
380 | |
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 | var HttpClientTestingModule = (function () {
|
388 | function HttpClientTestingModule() {
|
389 | }
|
390 | HttpClientTestingModule = __decorate([
|
391 | core.NgModule({
|
392 | imports: [
|
393 | http.HttpClientModule,
|
394 | ],
|
395 | providers: [
|
396 | HttpClientTestingBackend,
|
397 | { provide: http.HttpBackend, useExisting: HttpClientTestingBackend },
|
398 | { provide: HttpTestingController, useExisting: HttpClientTestingBackend },
|
399 | ],
|
400 | })
|
401 | ], HttpClientTestingModule);
|
402 | return HttpClientTestingModule;
|
403 | }());
|
404 |
|
405 | |
406 |
|
407 |
|
408 |
|
409 |
|
410 |
|
411 |
|
412 |
|
413 | |
414 |
|
415 |
|
416 |
|
417 |
|
418 |
|
419 |
|
420 |
|
421 | |
422 |
|
423 |
|
424 |
|
425 | exports.ɵangular_packages_common_http_testing_testing_a = HttpClientTestingBackend;
|
426 | exports.HttpTestingController = HttpTestingController;
|
427 | exports.HttpClientTestingModule = HttpClientTestingModule;
|
428 | exports.TestRequest = TestRequest;
|
429 |
|
430 | Object.defineProperty(exports, '__esModule', { value: true });
|
431 |
|
432 | }));
|
433 |
|