1
|
import { Connection, ConnectionBackend, ReadyState, Request, Response } from '@angular/http';
|
2
|
import { ReplaySubject } from 'rxjs/ReplaySubject';
|
3
|
/**
|
4
|
*
|
5
|
* Mock Connection to represent a {@link Connection} for tests.
|
6
|
*
|
7
|
* @experimental
|
8
|
*/
|
9
|
export declare class MockConnection implements Connection {
|
10
|
/**
|
11
|
* Describes the state of the connection, based on `XMLHttpRequest.readyState`, but with
|
12
|
* additional states. For example, state 5 indicates an aborted connection.
|
13
|
*/
|
14
|
readyState: ReadyState;
|
15
|
/**
|
16
|
* {@link Request} instance used to create the connection.
|
17
|
*/
|
18
|
request: Request;
|
19
|
/**
|
20
|
* {@link EventEmitter} of {@link Response}. Can be subscribed to in order to be notified when a
|
21
|
* response is available.
|
22
|
*/
|
23
|
response: ReplaySubject<Response>;
|
24
|
constructor(req: Request);
|
25
|
/**
|
26
|
* Sends a mock response to the connection. This response is the value that is emitted to the
|
27
|
* {@link EventEmitter} returned by {@link Http}.
|
28
|
*
|
29
|
* ### Example
|
30
|
*
|
31
|
* ```
|
32
|
* var connection;
|
33
|
* backend.connections.subscribe(c => connection = c);
|
34
|
* http.request('data.json').subscribe(res => console.log(res.text()));
|
35
|
* connection.mockRespond(new Response(new ResponseOptions({ body: 'fake response' }))); //logs
|
36
|
* 'fake response'
|
37
|
* ```
|
38
|
*
|
39
|
*/
|
40
|
mockRespond(res: Response): void;
|
41
|
/**
|
42
|
* Not yet implemented!
|
43
|
*
|
44
|
* Sends the provided {@link Response} to the `downloadObserver` of the `Request`
|
45
|
* associated with this connection.
|
46
|
*/
|
47
|
mockDownload(res: Response): void;
|
48
|
/**
|
49
|
* Emits the provided error object as an error to the {@link Response} {@link EventEmitter}
|
50
|
* returned
|
51
|
* from {@link Http}.
|
52
|
*
|
53
|
* ### Example
|
54
|
*
|
55
|
* ```
|
56
|
* var connection;
|
57
|
* backend.connections.subscribe(c => connection = c);
|
58
|
* http.request('data.json').subscribe(res => res, err => console.log(err)));
|
59
|
* connection.mockError(new Error('error'));
|
60
|
* ```
|
61
|
*
|
62
|
*/
|
63
|
mockError(err?: Error): void;
|
64
|
}
|
65
|
/**
|
66
|
* A mock backend for testing the {@link Http} service.
|
67
|
*
|
68
|
* This class can be injected in tests, and should be used to override providers
|
69
|
* to other backends, such as {@link XHRBackend}.
|
70
|
*
|
71
|
* ### Example
|
72
|
*
|
73
|
* ```
|
74
|
* import {Injectable, ReflectiveInjector} from '@angular/core';
|
75
|
* import {async, fakeAsync, tick} from '@angular/core/testing';
|
76
|
* import {BaseRequestOptions, ConnectionBackend, Http, RequestOptions} from '@angular/http';
|
77
|
* import {Response, ResponseOptions} from '@angular/http';
|
78
|
* import {MockBackend, MockConnection} from '@angular/http/testing';
|
79
|
*
|
80
|
* const HERO_ONE = 'HeroNrOne';
|
81
|
* const HERO_TWO = 'WillBeAlwaysTheSecond';
|
82
|
*
|
83
|
* @Injectable()
|
84
|
* class HeroService {
|
85
|
* constructor(private http: Http) {}
|
86
|
*
|
87
|
* getHeroes(): Promise<String[]> {
|
88
|
* return this.http.get('myservices.de/api/heroes')
|
89
|
* .toPromise()
|
90
|
* .then(response => response.json().data)
|
91
|
* .catch(e => this.handleError(e));
|
92
|
* }
|
93
|
*
|
94
|
* private handleError(error: any): Promise<any> {
|
95
|
* console.error('An error occurred', error);
|
96
|
* return Promise.reject(error.message || error);
|
97
|
* }
|
98
|
* }
|
99
|
*
|
100
|
* describe('MockBackend HeroService Example', () => {
|
101
|
* beforeEach(() => {
|
102
|
* this.injector = ReflectiveInjector.resolveAndCreate([
|
103
|
* {provide: ConnectionBackend, useClass: MockBackend},
|
104
|
* {provide: RequestOptions, useClass: BaseRequestOptions},
|
105
|
* Http,
|
106
|
* HeroService,
|
107
|
* ]);
|
108
|
* this.heroService = this.injector.get(HeroService);
|
109
|
* this.backend = this.injector.get(ConnectionBackend) as MockBackend;
|
110
|
* this.backend.connections.subscribe((connection: any) => this.lastConnection = connection);
|
111
|
* });
|
112
|
*
|
113
|
* it('getHeroes() should query current service url', () => {
|
114
|
* this.heroService.getHeroes();
|
115
|
* expect(this.lastConnection).toBeDefined('no http service connection at all?');
|
116
|
* expect(this.lastConnection.request.url).toMatch(/api\/heroes$/, 'url invalid');
|
117
|
* });
|
118
|
*
|
119
|
* it('getHeroes() should return some heroes', fakeAsync(() => {
|
120
|
* let result: String[];
|
121
|
* this.heroService.getHeroes().then((heroes: String[]) => result = heroes);
|
122
|
* this.lastConnection.mockRespond(new Response(new ResponseOptions({
|
123
|
* body: JSON.stringify({data: [HERO_ONE, HERO_TWO]}),
|
124
|
* })));
|
125
|
* tick();
|
126
|
* expect(result.length).toEqual(2, 'should contain given amount of heroes');
|
127
|
* expect(result[0]).toEqual(HERO_ONE, ' HERO_ONE should be the first hero');
|
128
|
* expect(result[1]).toEqual(HERO_TWO, ' HERO_TWO should be the second hero');
|
129
|
* }));
|
130
|
*
|
131
|
* it('getHeroes() while server is down', fakeAsync(() => {
|
132
|
* let result: String[];
|
133
|
* let catchedError: any;
|
134
|
* this.heroService.getHeroes()
|
135
|
* .then((heroes: String[]) => result = heroes)
|
136
|
* .catch((error: any) => catchedError = error);
|
137
|
* this.lastConnection.mockRespond(new Response(new ResponseOptions({
|
138
|
* status: 404,
|
139
|
* statusText: 'URL not Found',
|
140
|
* })));
|
141
|
* tick();
|
142
|
* expect(result).toBeUndefined();
|
143
|
* expect(catchedError).toBeDefined();
|
144
|
* }));
|
145
|
* });
|
146
|
* ```
|
147
|
*
|
148
|
* This method only exists in the mock implementation, not in real Backends.
|
149
|
*
|
150
|
* @experimental
|
151
|
*/
|
152
|
export declare class MockBackend implements ConnectionBackend {
|
153
|
/**
|
154
|
* {@link EventEmitter}
|
155
|
* of {@link MockConnection} instances that have been created by this backend. Can be subscribed
|
156
|
* to in order to respond to connections.
|
157
|
*
|
158
|
* ### Example
|
159
|
*
|
160
|
* ```
|
161
|
* import {ReflectiveInjector} from '@angular/core';
|
162
|
* import {fakeAsync, tick} from '@angular/core/testing';
|
163
|
* import {BaseRequestOptions, ConnectionBackend, Http, RequestOptions} from '@angular/http';
|
164
|
* import {Response, ResponseOptions} from '@angular/http';
|
165
|
* import {MockBackend, MockConnection} from '@angular/http/testing';
|
166
|
*
|
167
|
* it('should get a response', fakeAsync(() => {
|
168
|
* let connection:
|
169
|
* MockConnection; // this will be set when a new connection is emitted from the
|
170
|
* // backend.
|
171
|
* let text: string; // this will be set from mock response
|
172
|
* let injector = ReflectiveInjector.resolveAndCreate([
|
173
|
* {provide: ConnectionBackend, useClass: MockBackend},
|
174
|
* {provide: RequestOptions, useClass: BaseRequestOptions},
|
175
|
* Http,
|
176
|
* ]);
|
177
|
* let backend = injector.get(ConnectionBackend);
|
178
|
* let http = injector.get(Http);
|
179
|
* backend.connections.subscribe((c: MockConnection) => connection = c);
|
180
|
* http.request('something.json').toPromise().then((res: any) => text = res.text());
|
181
|
* connection.mockRespond(new Response(new ResponseOptions({body: 'Something'})));
|
182
|
* tick();
|
183
|
* expect(text).toBe('Something');
|
184
|
* }));
|
185
|
* ```
|
186
|
*
|
187
|
* This property only exists in the mock implementation, not in real Backends.
|
188
|
*/
|
189
|
connections: any;
|
190
|
/**
|
191
|
* An array representation of `connections`. This array will be updated with each connection that
|
192
|
* is created by this backend.
|
193
|
*
|
194
|
* This property only exists in the mock implementation, not in real Backends.
|
195
|
*/
|
196
|
connectionsArray: MockConnection[];
|
197
|
/**
|
198
|
* {@link EventEmitter} of {@link MockConnection} instances that haven't yet been resolved (i.e.
|
199
|
* with a `readyState`
|
200
|
* less than 4). Used internally to verify that no connections are pending via the
|
201
|
* `verifyNoPendingRequests` method.
|
202
|
*
|
203
|
* This property only exists in the mock implementation, not in real Backends.
|
204
|
*/
|
205
|
pendingConnections: any;
|
206
|
constructor();
|
207
|
/**
|
208
|
* Checks all connections, and raises an exception if any connection has not received a response.
|
209
|
*
|
210
|
* This method only exists in the mock implementation, not in real Backends.
|
211
|
*/
|
212
|
verifyNoPendingRequests(): void;
|
213
|
/**
|
214
|
* Can be used in conjunction with `verifyNoPendingRequests` to resolve any not-yet-resolve
|
215
|
* connections, if it's expected that there are connections that have not yet received a response.
|
216
|
*
|
217
|
* This method only exists in the mock implementation, not in real Backends.
|
218
|
*/
|
219
|
resolveAllConnections(): void;
|
220
|
/**
|
221
|
* Creates a new {@link MockConnection}. This is equivalent to calling `new
|
222
|
* MockConnection()`, except that it also will emit the new `Connection` to the `connections`
|
223
|
* emitter of this `MockBackend` instance. This method will usually only be used by tests
|
224
|
* against the framework itself, not by end-users.
|
225
|
*/
|
226
|
createConnection(req: Request): MockConnection;
|
227
|
}
|