| 
									
										
										
										
											2016-06-23 09:47:54 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @license | 
					
						
							|  |  |  |  * Copyright Google Inc. All Rights Reserved. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Use of this source code is governed by an MIT-style license that can be | 
					
						
							|  |  |  |  * found in the LICENSE file at https://angular.io/license
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  | import {Injectable} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  | import {Connection, ConnectionBackend, ReadyState, Request, Response} from '@angular/http'; | 
					
						
							| 
									
										
										
										
											2016-04-13 11:25:45 -07:00
										 |  |  | import {ReplaySubject} from 'rxjs/ReplaySubject'; | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  | import {Subject} from 'rxjs/Subject'; | 
					
						
							| 
									
										
										
										
											2015-12-04 09:09:57 -08:00
										 |  |  | import {take} from 'rxjs/operator/take'; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-10 15:55:18 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |  * Mock Connection to represent a {@link Connection} for tests. | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2016-06-27 12:27:23 -07:00
										 |  |  |  * @experimental | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-08-26 11:41:41 -07:00
										 |  |  | export class MockConnection implements Connection { | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |   // TODO Name `readyState` should change to be more generic, and states could be made to be more
 | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |   // descriptive than ResourceLoader states.
 | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * Describes the state of the connection, based on `XMLHttpRequest.readyState`, but with | 
					
						
							|  |  |  |    * additional states. For example, state 5 indicates an aborted connection. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |   readyState: ReadyState; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * {@link Request} instance used to create the connection. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   request: Request; | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |    * {@link EventEmitter} of {@link Response}. Can be subscribed to in order to be notified when a | 
					
						
							|  |  |  |    * response is available. | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |   response: ReplaySubject<Response>; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   constructor(req: Request) { | 
					
						
							| 
									
										
										
										
											2016-04-10 13:24:41 -07:00
										 |  |  |     this.response = <any>take.call(new ReplaySubject(1), 1); | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |     this.readyState = ReadyState.Open; | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     this.request = req; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * Sends a mock response to the connection. This response is the value that is emitted to the | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |    * {@link EventEmitter} returned by {@link Http}. | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * | 
					
						
							| 
									
										
										
										
											2015-10-19 15:37:32 +01:00
										 |  |  |    * ### Example | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * | 
					
						
							|  |  |  |    * ```
 | 
					
						
							|  |  |  |    * var connection; | 
					
						
							| 
									
										
										
										
											2015-10-07 07:41:27 -03:00
										 |  |  |    * backend.connections.subscribe(c => connection = c); | 
					
						
							|  |  |  |    * http.request('data.json').subscribe(res => console.log(res.text())); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |    * connection.mockRespond(new Response(new ResponseOptions({ body: 'fake response' }))); //logs
 | 
					
						
							|  |  |  |    * 'fake response' | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * ```
 | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   mockRespond(res: Response) { | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |     if (this.readyState === ReadyState.Done || this.readyState === ReadyState.Cancelled) { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |       throw new Error('Connection has already been resolved'); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |     this.readyState = ReadyState.Done; | 
					
						
							| 
									
										
										
										
											2015-10-01 16:04:20 -07:00
										 |  |  |     this.response.next(res); | 
					
						
							|  |  |  |     this.response.complete(); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Not yet implemented! | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * Sends the provided {@link Response} to the `downloadObserver` of the `Request` | 
					
						
							|  |  |  |    * associated with this connection. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   mockDownload(res: Response) { | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |     // this.request.downloadObserver.onNext(res);
 | 
					
						
							|  |  |  |     // if (res.bytesLoaded === res.totalBytes) {
 | 
					
						
							|  |  |  |     //   this.request.downloadObserver.onCompleted();
 | 
					
						
							|  |  |  |     // }
 | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |   // TODO(jeffbcross): consider using Response type
 | 
					
						
							|  |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |    * Emits the provided error object as an error to the {@link Response} {@link EventEmitter} | 
					
						
							|  |  |  |    * returned | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * from {@link Http}. | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |    * | 
					
						
							| 
									
										
										
										
											2016-05-27 13:10:53 -04:00
										 |  |  |    * ### Example | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * ```
 | 
					
						
							|  |  |  |    * var connection; | 
					
						
							|  |  |  |    * backend.connections.subscribe(c => connection = c); | 
					
						
							|  |  |  |    * http.request('data.json').subscribe(res => res, err => console.log(err))); | 
					
						
							|  |  |  |    * connection.mockError(new Error('error')); | 
					
						
							|  |  |  |    * ```
 | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |    * | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-07-07 20:03:00 -07:00
										 |  |  |   mockError(err?: Error) { | 
					
						
							| 
									
										
										
										
											2016-08-17 09:24:44 -07:00
										 |  |  |     // Matches ResourceLoader semantics
 | 
					
						
							| 
									
										
										
										
											2015-12-03 22:44:14 +01:00
										 |  |  |     this.readyState = ReadyState.Done; | 
					
						
							| 
									
										
										
										
											2015-10-01 16:04:20 -07:00
										 |  |  |     this.response.error(err); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  | /** | 
					
						
							|  |  |  |  * A mock backend for testing the {@link Http} service. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-10-10 22:11:13 -07:00
										 |  |  |  * This class can be injected in tests, and should be used to override providers | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |  * to other backends, such as {@link XHRBackend}. | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2015-10-19 15:37:32 +01:00
										 |  |  |  * ### Example | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * ```
 | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  |  * import {BaseRequestOptions, Http} from '@angular/http'; | 
					
						
							|  |  |  |  * import {MockBackend} from '@angular/http/testing'; | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |  * it('should get some data', inject([AsyncTestCompleter], (async) => { | 
					
						
							|  |  |  |  *   var connection; | 
					
						
							|  |  |  |  *   var injector = Injector.resolveAndCreate([ | 
					
						
							|  |  |  |  *     MockBackend, | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |  *     {provide: Http, useFactory: (backend, options) => { | 
					
						
							| 
									
										
										
										
											2015-12-20 20:28:52 -06:00
										 |  |  |  *       return new Http(backend, options); | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |  *     }, deps: [MockBackend, BaseRequestOptions]}]); | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |  *   var http = injector.get(Http); | 
					
						
							|  |  |  |  *   var backend = injector.get(MockBackend); | 
					
						
							|  |  |  |  *   //Assign any newly-created connection to local variable
 | 
					
						
							| 
									
										
										
										
											2015-10-07 07:41:27 -03:00
										 |  |  |  *   backend.connections.subscribe(c => connection = c); | 
					
						
							|  |  |  |  *   http.request('data.json').subscribe((res) => { | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |  *     expect(res.text()).toBe('awesome'); | 
					
						
							|  |  |  |  *     async.done(); | 
					
						
							|  |  |  |  *   }); | 
					
						
							|  |  |  |  *   connection.mockRespond(new Response('awesome')); | 
					
						
							|  |  |  |  * })); | 
					
						
							|  |  |  |  * ```
 | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This method only exists in the mock implementation, not in real Backends. | 
					
						
							| 
									
										
										
										
											2016-06-27 12:27:23 -07:00
										 |  |  |  * | 
					
						
							|  |  |  |  * @experimental | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | @Injectable() | 
					
						
							| 
									
										
										
										
											2015-08-26 11:41:41 -07:00
										 |  |  | export class MockBackend implements ConnectionBackend { | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |    * {@link EventEmitter} | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * of {@link MockConnection} instances that have been created by this backend. Can be subscribed | 
					
						
							|  |  |  |    * to in order to respond to connections. | 
					
						
							|  |  |  |    * | 
					
						
							| 
									
										
										
										
											2015-10-19 15:37:32 +01:00
										 |  |  |    * ### Example | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * | 
					
						
							|  |  |  |    * ```
 | 
					
						
							| 
									
										
										
										
											2016-03-02 15:58:17 -05:00
										 |  |  |    * import {Http, BaseRequestOptions, Response} from '@angular/http'; | 
					
						
							| 
									
										
										
										
											2016-04-28 17:50:03 -07:00
										 |  |  |    * import {MockBackend} from '@angular/http/testing'; | 
					
						
							| 
									
										
										
										
											2016-03-02 15:58:17 -05:00
										 |  |  |    * import {Injector, provide} from '@angular/core'; | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * | 
					
						
							|  |  |  |    * it('should get a response', () => { | 
					
						
							|  |  |  |    *   var connection; //this will be set when a new connection is emitted from the backend.
 | 
					
						
							|  |  |  |    *   var text; //this will be set from mock response
 | 
					
						
							|  |  |  |    *   var injector = Injector.resolveAndCreate([ | 
					
						
							|  |  |  |    *     MockBackend, | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |    *     {provide: Http, useFactory: (backend, options) => { | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    *       return new Http(backend, options); | 
					
						
							| 
									
										
										
										
											2016-06-02 17:30:40 -07:00
										 |  |  |    *     }, deps: [MockBackend, BaseRequestOptions]}]); | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    *   var backend = injector.get(MockBackend); | 
					
						
							|  |  |  |    *   var http = injector.get(Http); | 
					
						
							| 
									
										
										
										
											2015-10-07 07:41:27 -03:00
										 |  |  |    *   backend.connections.subscribe(c => connection = c); | 
					
						
							|  |  |  |    *   http.request('something.json').subscribe(res => { | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    *     text = res.text(); | 
					
						
							|  |  |  |    *   }); | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |    *   connection.mockRespond(new Response({body: 'Something'})); | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    *   expect(text).toBe('Something'); | 
					
						
							|  |  |  |    * }); | 
					
						
							|  |  |  |    * ```
 | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * This property only exists in the mock implementation, not in real Backends. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-10-01 16:04:20 -07:00
										 |  |  |   connections: any;  //<MockConnection>
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   /** | 
					
						
							|  |  |  |    * An array representation of `connections`. This array will be updated with each connection that | 
					
						
							|  |  |  |    * is created by this backend. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * This property only exists in the mock implementation, not in real Backends. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-08-28 11:29:19 -07:00
										 |  |  |   connectionsArray: MockConnection[]; | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |   /** | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |    * {@link EventEmitter} of {@link MockConnection} instances that haven't yet been resolved (i.e. | 
					
						
							|  |  |  |    * with a `readyState` | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * less than 4). Used internally to verify that no connections are pending via the | 
					
						
							|  |  |  |    * `verifyNoPendingRequests` method. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * This property only exists in the mock implementation, not in real Backends. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-10-01 16:04:20 -07:00
										 |  |  |   pendingConnections: any;  // Subject<MockConnection>
 | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   constructor() { | 
					
						
							|  |  |  |     this.connectionsArray = []; | 
					
						
							| 
									
										
										
										
											2015-10-01 16:04:20 -07:00
										 |  |  |     this.connections = new Subject(); | 
					
						
							| 
									
										
										
										
											2016-06-08 16:38:52 -07:00
										 |  |  |     this.connections.subscribe( | 
					
						
							|  |  |  |         (connection: MockConnection) => this.connectionsArray.push(connection)); | 
					
						
							| 
									
										
										
										
											2015-10-01 16:04:20 -07:00
										 |  |  |     this.pendingConnections = new Subject(); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Checks all connections, and raises an exception if any connection has not received a response. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * This method only exists in the mock implementation, not in real Backends. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   verifyNoPendingRequests() { | 
					
						
							|  |  |  |     let pending = 0; | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |     this.pendingConnections.subscribe((c: MockConnection) => pending++); | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |     if (pending > 0) throw new Error(`${pending} pending connections to be resolved`); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Can be used in conjunction with `verifyNoPendingRequests` to resolve any not-yet-resolve | 
					
						
							|  |  |  |    * connections, if it's expected that there are connections that have not yet received a response. | 
					
						
							|  |  |  |    * | 
					
						
							|  |  |  |    * This method only exists in the mock implementation, not in real Backends. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |   resolveAllConnections() { this.connections.subscribe((c: MockConnection) => c.readyState = 4); } | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |   /** | 
					
						
							|  |  |  |    * Creates a new {@link MockConnection}. This is equivalent to calling `new
 | 
					
						
							|  |  |  |    * MockConnection()`, except that it also will emit the new `Connection` to the `connections`
 | 
					
						
							| 
									
										
										
										
											2015-06-24 00:27:07 -07:00
										 |  |  |    * emitter of this `MockBackend` instance. This method will usually only be used by tests | 
					
						
							| 
									
										
										
										
											2015-06-09 15:18:57 -07:00
										 |  |  |    * against the framework itself, not by end-users. | 
					
						
							|  |  |  |    */ | 
					
						
							| 
									
										
										
										
											2016-02-01 17:05:50 -08:00
										 |  |  |   createConnection(req: Request): MockConnection { | 
					
						
							| 
									
										
										
										
											2016-08-30 18:07:40 -07:00
										 |  |  |     if (!req || !(req instanceof Request)) { | 
					
						
							| 
									
										
										
										
											2016-08-25 00:50:16 -07:00
										 |  |  |       throw new Error(`createConnection requires an instance of Request, got ${req}`); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2016-11-12 14:08:58 +01:00
										 |  |  |     const connection = new MockConnection(req); | 
					
						
							| 
									
										
										
										
											2015-10-01 16:04:20 -07:00
										 |  |  |     this.connections.next(connection); | 
					
						
							| 
									
										
										
										
											2015-04-28 23:07:55 -07:00
										 |  |  |     return connection; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } |