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