import { XhrFactory } from '@angular/common' ; import { HttpHeaders } from '@angular/common/http' ; export class MockXhrFactory implements XhrFactory { mock : MockXMLHttpRequest ; build ( ) { return ( this . mock = new MockXMLHttpRequest ( ) ) as any ; } } export class MockXMLHttpRequestUpload { constructor ( private mock ) { } addEventListener ( event , handler ) { this . mock . addEventListener ( event === 'progress' ? 'uploadProgress' : event , handler ) ; } removeEventListener ( event , handler ) { this . mock . removeEventListener ( event === 'progress' ? 'uploadProgress' : event ) ; } } export class MockXMLHttpRequest { body : any ; method ! : string ; url ! : string ; mockHeaders : { [ key ] : string ; } = { } ; mockAborted : boolean = false ; withCredentials : boolean = false ; responseType : string = 'text' ; response : any | undefined = undefined ; responseText : string | undefined = undefined ; responseURL : string | null = null ; status : number = 0 ; statusText : string = '' ; mockResponseHeaders : string = '' ; readyState : number ; onreadystatechange = ( ) => { } ; listeners : { error ? : ( event : ErrorEvent ) => void ; load ? : ( ) => void ; progress ? : ( event : ProgressEvent ) => void ; uploadProgress ? : ( event : ProgressEvent ) => void ; } = { } ; upload = new MockXMLHttpRequestUpload ( this ) ; open ( method , url ) { this . method = method ; this . url = url ; } send ( body ) { this . body = body ; } addEventListener ( event , handler ) { this . listeners [ event ] = handler as any ; } removeEventListener ( event ) { delete this . listeners [ event ] ; } setRequestHeader ( name , value ) { this . mockHeaders [ name ] = value ; } getAllResponseHeaders ( ) { return this . mockResponseHeaders ; } getResponseHeader ( header ) { return new HttpHeaders ( this . mockResponseHeaders ) . get ( header ) ; } mockFlush ( status , statusText , body ? ) { if ( typeof body === 'string' ) { this . responseText = body ; } else { this . response = body ; } this . status = status ; this . statusText = statusText ; this . mockLoadEvent ( ) ; } mockOnReadyStateChange ( readyState = XMLHttpRequest . DONE ) { this . readyState = XMLHttpRequest . DONE ; this . onreadystatechange ( ) ; } mockDownloadProgressEvent ( loaded , total ? ) { if ( this . listeners . progress ) { this . listeners . progress ( { lengthComputable : total !== undefined , loaded , total } as any ) ; } } mockUploadProgressEvent ( loaded , total ? ) { if ( this . listeners . uploadProgress ) { this . listeners . uploadProgress ( { lengthComputable : total !== undefined , loaded , total } as any ) ; } } mockLoadEvent ( ) { if ( this . listeners . load ) { this . listeners . load ( ) ; } } mockErrorEvent ( error ) { if ( this . listeners . error ) { this . listeners . error ( error ) ; } } abort ( ) { this . mockAborted = true ; } }