HttpClient is an evolution of the existing Angular HTTP API, which exists alongside of it in a separate package, @angular/common/http. This structure ensures that existing codebases can slowly migrate to the new API. The new API improves significantly on the ergonomics and features of the legacy API. A partial list of new features includes: * Typed, synchronous response body access, including support for JSON body types * JSON is an assumed default and no longer needs to be explicitly parsed * Interceptors allow middleware logic to be inserted into the pipeline * Immutable request/response objects * Progress events for both request upload and response download * Post-request verification & flush based testing framework
		
			
				
	
	
		
			44 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			44 lines
		
	
	
		
			1.0 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
| /**
 | |
|  * @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
 | |
|  */
 | |
| 
 | |
| export class MockScriptElement {
 | |
|   constructor() {}
 | |
| 
 | |
|   listeners: {
 | |
|     load?: (event: Event) => void,
 | |
|     error?: (err: Error) => void,
 | |
|   } = {};
 | |
| 
 | |
|   addEventListener(event: 'load'|'error', handler: Function): void {
 | |
|     this.listeners[event] = handler as any;
 | |
|   }
 | |
| 
 | |
|   removeEventListener(event: 'load'|'error'): void { delete this.listeners[event]; }
 | |
| }
 | |
| 
 | |
| export class MockDocument {
 | |
|   mock: MockScriptElement|null;
 | |
| 
 | |
|   createElement(tag: 'script'): HTMLScriptElement {
 | |
|     return new MockScriptElement() as any as HTMLScriptElement;
 | |
|   }
 | |
| 
 | |
|   get body(): any { return this; }
 | |
| 
 | |
|   appendChild(node: any): void { this.mock = node; }
 | |
| 
 | |
|   removeNode(node: any): void {
 | |
|     if (this.mock === node) {
 | |
|       this.mock = null;
 | |
|     }
 | |
|   }
 | |
| 
 | |
|   mockLoad(): void { this.mock !.listeners.load !(null as any); }
 | |
| 
 | |
|   mockError(err: Error) { this.mock !.listeners.error !(err); }
 | |
| } |