1
|
/**
|
2
|
* @license Angular v4.4.6
|
3
|
* (c) 2010-2017 Google, Inc. https://angular.io/
|
4
|
* License: MIT
|
5
|
*/
|
6
|
(function (global, factory) {
|
7
|
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('@angular/core'), require('@angular/http'), require('rxjs/ReplaySubject'), require('rxjs/Subject'), require('rxjs/operator/take')) :
|
8
|
typeof define === 'function' && define.amd ? define(['exports', '@angular/core', '@angular/http', 'rxjs/ReplaySubject', 'rxjs/Subject', 'rxjs/operator/take'], factory) :
|
9
|
(factory((global.ng = global.ng || {}, global.ng.http = global.ng.http || {}, global.ng.http.testing = global.ng.http.testing || {}),global.ng.core,global.ng.http,global.Rx,global.Rx,global.Rx.Observable.prototype));
|
10
|
}(this, (function (exports,_angular_core,_angular_http,rxjs_ReplaySubject,rxjs_Subject,rxjs_operator_take) { 'use strict';
|
11
|
|
12
|
/**
|
13
|
* @license Angular v4.4.6
|
14
|
* (c) 2010-2017 Google, Inc. https://angular.io/
|
15
|
* License: MIT
|
16
|
*/
|
17
|
/**
|
18
|
* @license
|
19
|
* Copyright Google Inc. All Rights Reserved.
|
20
|
*
|
21
|
* Use of this source code is governed by an MIT-style license that can be
|
22
|
* found in the LICENSE file at https://angular.io/license
|
23
|
*/
|
24
|
/**
|
25
|
*
|
26
|
* Mock Connection to represent a {\@link Connection} for tests.
|
27
|
*
|
28
|
* \@experimental
|
29
|
*/
|
30
|
var MockConnection = (function () {
|
31
|
/**
|
32
|
* @param {?} req
|
33
|
*/
|
34
|
function MockConnection(req) {
|
35
|
this.response = rxjs_operator_take.take.call(new rxjs_ReplaySubject.ReplaySubject(1), 1);
|
36
|
this.readyState = _angular_http.ReadyState.Open;
|
37
|
this.request = req;
|
38
|
}
|
39
|
/**
|
40
|
* Sends a mock response to the connection. This response is the value that is emitted to the
|
41
|
* {\@link EventEmitter} returned by {\@link Http}.
|
42
|
*
|
43
|
* ### Example
|
44
|
*
|
45
|
* ```
|
46
|
* var connection;
|
47
|
* backend.connections.subscribe(c => connection = c);
|
48
|
* http.request('data.json').subscribe(res => console.log(res.text()));
|
49
|
* connection.mockRespond(new Response(new ResponseOptions({ body: 'fake response' }))); //logs
|
50
|
* 'fake response'
|
51
|
* ```
|
52
|
*
|
53
|
* @param {?} res
|
54
|
* @return {?}
|
55
|
*/
|
56
|
MockConnection.prototype.mockRespond = function (res) {
|
57
|
if (this.readyState === _angular_http.ReadyState.Done || this.readyState === _angular_http.ReadyState.Cancelled) {
|
58
|
throw new Error('Connection has already been resolved');
|
59
|
}
|
60
|
this.readyState = _angular_http.ReadyState.Done;
|
61
|
this.response.next(res);
|
62
|
this.response.complete();
|
63
|
};
|
64
|
/**
|
65
|
* Not yet implemented!
|
66
|
*
|
67
|
* Sends the provided {\@link Response} to the `downloadObserver` of the `Request`
|
68
|
* associated with this connection.
|
69
|
* @param {?} res
|
70
|
* @return {?}
|
71
|
*/
|
72
|
MockConnection.prototype.mockDownload = function (res) {
|
73
|
// this.request.downloadObserver.onNext(res);
|
74
|
// if (res.bytesLoaded === res.totalBytes) {
|
75
|
// this.request.downloadObserver.onCompleted();
|
76
|
// }
|
77
|
};
|
78
|
/**
|
79
|
* Emits the provided error object as an error to the {\@link Response} {\@link EventEmitter}
|
80
|
* returned
|
81
|
* from {\@link Http}.
|
82
|
*
|
83
|
* ### Example
|
84
|
*
|
85
|
* ```
|
86
|
* var connection;
|
87
|
* backend.connections.subscribe(c => connection = c);
|
88
|
* http.request('data.json').subscribe(res => res, err => console.log(err)));
|
89
|
* connection.mockError(new Error('error'));
|
90
|
* ```
|
91
|
*
|
92
|
* @param {?=} err
|
93
|
* @return {?}
|
94
|
*/
|
95
|
MockConnection.prototype.mockError = function (err) {
|
96
|
// Matches ResourceLoader semantics
|
97
|
this.readyState = _angular_http.ReadyState.Done;
|
98
|
this.response.error(err);
|
99
|
};
|
100
|
return MockConnection;
|
101
|
}());
|
102
|
/**
|
103
|
* A mock backend for testing the {\@link Http} service.
|
104
|
*
|
105
|
* This class can be injected in tests, and should be used to override providers
|
106
|
* to other backends, such as {\@link XHRBackend}.
|
107
|
*
|
108
|
* ### Example
|
109
|
*
|
110
|
* ```
|
111
|
* import {Injectable, ReflectiveInjector} from '\@angular/core';
|
112
|
* import {async, fakeAsync, tick} from '\@angular/core/testing';
|
113
|
* import {BaseRequestOptions, ConnectionBackend, Http, RequestOptions} from '\@angular/http';
|
114
|
* import {Response, ResponseOptions} from '\@angular/http';
|
115
|
* import {MockBackend, MockConnection} from '\@angular/http/testing';
|
116
|
*
|
117
|
* const HERO_ONE = 'HeroNrOne';
|
118
|
* const HERO_TWO = 'WillBeAlwaysTheSecond';
|
119
|
*
|
120
|
* \@Injectable()
|
121
|
* class HeroService {
|
122
|
* constructor(private http: Http) {}
|
123
|
*
|
124
|
* getHeroes(): Promise<String[]> {
|
125
|
* return this.http.get('myservices.de/api/heroes')
|
126
|
* .toPromise()
|
127
|
* .then(response => response.json().data)
|
128
|
* .catch(e => this.handleError(e));
|
129
|
* }
|
130
|
*
|
131
|
* private handleError(error: any): Promise<any> {
|
132
|
* console.error('An error occurred', error);
|
133
|
* return Promise.reject(error.message || error);
|
134
|
* }
|
135
|
* }
|
136
|
*
|
137
|
* describe('MockBackend HeroService Example', () => {
|
138
|
* beforeEach(() => {
|
139
|
* this.injector = ReflectiveInjector.resolveAndCreate([
|
140
|
* {provide: ConnectionBackend, useClass: MockBackend},
|
141
|
* {provide: RequestOptions, useClass: BaseRequestOptions},
|
142
|
* Http,
|
143
|
* HeroService,
|
144
|
* ]);
|
145
|
* this.heroService = this.injector.get(HeroService);
|
146
|
* this.backend = this.injector.get(ConnectionBackend) as MockBackend;
|
147
|
* this.backend.connections.subscribe((connection: any) => this.lastConnection = connection);
|
148
|
* });
|
149
|
*
|
150
|
* it('getHeroes() should query current service url', () => {
|
151
|
* this.heroService.getHeroes();
|
152
|
* expect(this.lastConnection).toBeDefined('no http service connection at all?');
|
153
|
* expect(this.lastConnection.request.url).toMatch(/api\/heroes$/, 'url invalid');
|
154
|
* });
|
155
|
*
|
156
|
* it('getHeroes() should return some heroes', fakeAsync(() => {
|
157
|
* let result: String[];
|
158
|
* this.heroService.getHeroes().then((heroes: String[]) => result = heroes);
|
159
|
* this.lastConnection.mockRespond(new Response(new ResponseOptions({
|
160
|
* body: JSON.stringify({data: [HERO_ONE, HERO_TWO]}),
|
161
|
* })));
|
162
|
* tick();
|
163
|
* expect(result.length).toEqual(2, 'should contain given amount of heroes');
|
164
|
* expect(result[0]).toEqual(HERO_ONE, ' HERO_ONE should be the first hero');
|
165
|
* expect(result[1]).toEqual(HERO_TWO, ' HERO_TWO should be the second hero');
|
166
|
* }));
|
167
|
*
|
168
|
* it('getHeroes() while server is down', fakeAsync(() => {
|
169
|
* let result: String[];
|
170
|
* let catchedError: any;
|
171
|
* this.heroService.getHeroes()
|
172
|
* .then((heroes: String[]) => result = heroes)
|
173
|
* .catch((error: any) => catchedError = error);
|
174
|
* this.lastConnection.mockRespond(new Response(new ResponseOptions({
|
175
|
* status: 404,
|
176
|
* statusText: 'URL not Found',
|
177
|
* })));
|
178
|
* tick();
|
179
|
* expect(result).toBeUndefined();
|
180
|
* expect(catchedError).toBeDefined();
|
181
|
* }));
|
182
|
* });
|
183
|
* ```
|
184
|
*
|
185
|
* This method only exists in the mock implementation, not in real Backends.
|
186
|
*
|
187
|
* \@experimental
|
188
|
*/
|
189
|
var MockBackend = (function () {
|
190
|
function MockBackend() {
|
191
|
var _this = this;
|
192
|
this.connectionsArray = [];
|
193
|
this.connections = new rxjs_Subject.Subject();
|
194
|
this.connections.subscribe(function (connection) { return _this.connectionsArray.push(connection); });
|
195
|
this.pendingConnections = new rxjs_Subject.Subject();
|
196
|
}
|
197
|
/**
|
198
|
* Checks all connections, and raises an exception if any connection has not received a response.
|
199
|
*
|
200
|
* This method only exists in the mock implementation, not in real Backends.
|
201
|
* @return {?}
|
202
|
*/
|
203
|
MockBackend.prototype.verifyNoPendingRequests = function () {
|
204
|
var /** @type {?} */ pending = 0;
|
205
|
this.pendingConnections.subscribe(function (c) { return pending++; });
|
206
|
if (pending > 0)
|
207
|
throw new Error(pending + " pending connections to be resolved");
|
208
|
};
|
209
|
/**
|
210
|
* Can be used in conjunction with `verifyNoPendingRequests` to resolve any not-yet-resolve
|
211
|
* connections, if it's expected that there are connections that have not yet received a response.
|
212
|
*
|
213
|
* This method only exists in the mock implementation, not in real Backends.
|
214
|
* @return {?}
|
215
|
*/
|
216
|
MockBackend.prototype.resolveAllConnections = function () { this.connections.subscribe(function (c) { return c.readyState = 4; }); };
|
217
|
/**
|
218
|
* Creates a new {\@link MockConnection}. This is equivalent to calling `new
|
219
|
* MockConnection()`, except that it also will emit the new `Connection` to the `connections`
|
220
|
* emitter of this `MockBackend` instance. This method will usually only be used by tests
|
221
|
* against the framework itself, not by end-users.
|
222
|
* @param {?} req
|
223
|
* @return {?}
|
224
|
*/
|
225
|
MockBackend.prototype.createConnection = function (req) {
|
226
|
if (!req || !(req instanceof _angular_http.Request)) {
|
227
|
throw new Error("createConnection requires an instance of Request, got " + req);
|
228
|
}
|
229
|
var /** @type {?} */ connection = new MockConnection(req);
|
230
|
this.connections.next(connection);
|
231
|
return connection;
|
232
|
};
|
233
|
return MockBackend;
|
234
|
}());
|
235
|
MockBackend.decorators = [
|
236
|
{ type: _angular_core.Injectable },
|
237
|
];
|
238
|
/**
|
239
|
* @nocollapse
|
240
|
*/
|
241
|
MockBackend.ctorParameters = function () { return []; };
|
242
|
|
243
|
exports.MockConnection = MockConnection;
|
244
|
exports.MockBackend = MockBackend;
|
245
|
|
246
|
Object.defineProperty(exports, '__esModule', { value: true });
|
247
|
|
248
|
})));
|
249
|
//# sourceMappingURL=http-testing.umd.js.map
|