| 
									
										
										
										
											2017-03-22 17:13:24 -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
 | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-11-15 11:35:08 -08:00
										 |  |  | import {HttpHeaders} from '@angular/common/http/src/headers'; | 
					
						
							|  |  |  | import {XhrFactory} from '@angular/common/http/src/xhr'; | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  | export class MockXhrFactory implements XhrFactory { | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |   mock !: MockXMLHttpRequest; | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  | 
 | 
					
						
							|  |  |  |   build(): XMLHttpRequest { return (this.mock = new MockXMLHttpRequest()) as any; } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class MockXMLHttpRequestUpload { | 
					
						
							|  |  |  |   constructor(private mock: MockXMLHttpRequest) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   addEventListener(event: 'progress', handler: Function) { | 
					
						
							|  |  |  |     this.mock.addEventListener('uploadProgress', handler); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   removeEventListener(event: 'progress', handler: Function) { | 
					
						
							|  |  |  |     this.mock.removeEventListener('uploadProgress'); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | export class MockXMLHttpRequest { | 
					
						
							|  |  |  |   // Set by method calls.
 | 
					
						
							|  |  |  |   body: any; | 
					
						
							| 
									
										
										
										
											2018-06-18 16:38:33 -07:00
										 |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |   method !: string; | 
					
						
							|  |  |  |   // TODO(issue/24571): remove '!'.
 | 
					
						
							|  |  |  |   url !: string; | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |   mockHeaders: {[key: string]: string} = {}; | 
					
						
							|  |  |  |   mockAborted: boolean = false; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Directly settable interface.
 | 
					
						
							|  |  |  |   withCredentials: boolean = false; | 
					
						
							|  |  |  |   responseType: string = 'text'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Mocked response interface.
 | 
					
						
							|  |  |  |   response: any|undefined = undefined; | 
					
						
							|  |  |  |   responseText: string|undefined = undefined; | 
					
						
							|  |  |  |   responseURL: string|null = null; | 
					
						
							|  |  |  |   status: number = 0; | 
					
						
							|  |  |  |   statusText: string = ''; | 
					
						
							|  |  |  |   mockResponseHeaders: string = ''; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   listeners: { | 
					
						
							|  |  |  |     error?: (event: ErrorEvent) => void, | 
					
						
							|  |  |  |     load?: () => void, | 
					
						
							|  |  |  |     progress?: (event: ProgressEvent) => void, | 
					
						
							|  |  |  |     uploadProgress?: (event: ProgressEvent) => void, | 
					
						
							|  |  |  |   } = {}; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   upload = new MockXMLHttpRequestUpload(this); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   open(method: string, url: string): void { | 
					
						
							|  |  |  |     this.method = method; | 
					
						
							|  |  |  |     this.url = url; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   send(body: any): void { this.body = body; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   addEventListener(event: 'error'|'load'|'progress'|'uploadProgress', handler: Function): void { | 
					
						
							|  |  |  |     this.listeners[event] = handler as any; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   removeEventListener(event: 'error'|'load'|'progress'|'uploadProgress'): void { | 
					
						
							|  |  |  |     delete this.listeners[event]; | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   setRequestHeader(name: string, value: string): void { this.mockHeaders[name] = value; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getAllResponseHeaders(): string { return this.mockResponseHeaders; } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   getResponseHeader(header: string): string|null { | 
					
						
							|  |  |  |     return new HttpHeaders(this.mockResponseHeaders).get(header); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-01 11:46:26 -07:00
										 |  |  |   mockFlush(status: number, statusText: string, body?: string) { | 
					
						
							|  |  |  |     if (typeof body === 'string') { | 
					
						
							| 
									
										
										
										
											2017-03-22 17:13:24 -07:00
										 |  |  |       this.responseText = body; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |       this.response = body; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     this.status = status; | 
					
						
							|  |  |  |     this.statusText = statusText; | 
					
						
							|  |  |  |     this.mockLoadEvent(); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mockDownloadProgressEvent(loaded: number, total?: number): void { | 
					
						
							|  |  |  |     if (this.listeners.progress) { | 
					
						
							|  |  |  |       this.listeners.progress({ lengthComputable: total !== undefined, loaded, total } as any); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mockUploadProgressEvent(loaded: number, total?: number) { | 
					
						
							|  |  |  |     if (this.listeners.uploadProgress) { | 
					
						
							|  |  |  |       this.listeners.uploadProgress( | 
					
						
							|  |  |  |           { lengthComputable: total !== undefined, loaded, total, } as any); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mockLoadEvent(): void { | 
					
						
							|  |  |  |     if (this.listeners.load) { | 
					
						
							|  |  |  |       this.listeners.load(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   mockErrorEvent(error: any): void { | 
					
						
							|  |  |  |     if (this.listeners.error) { | 
					
						
							|  |  |  |       this.listeners.error(error); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   abort() { this.mockAborted = true; } | 
					
						
							| 
									
										
										
										
											2018-11-15 11:35:08 -08:00
										 |  |  | } |