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