export class MockHeaders { private _values : string [ ] [ ] = [ ] ; constructor ( init ? ) { if ( typeof init === 'object' ) { if ( typeof init [ Symbol . iterator ] === 'function' ) { const kvs = [ ] ; for ( const kv of init ) { if ( typeof kv [ Symbol . iterator ] === 'function' ) { kvs . push ( [ ... kv ] ) ; } } for ( const kv of kvs ) { this . append ( kv [ 0 ] , kv [ 1 ] ) ; } } else { for ( const key in init ) { this . append ( key , init [ key ] ) ; } } } } append ( key , value ) { this . _values . push ( [ key , value + '' ] ) ; } delete ( key ) { key = key . toLowerCase ( ) ; for ( let i = this . _values . length - 1 ; i >= 0 ; i -- ) { if ( this . _values [ i ] [ 0 ] . toLowerCase ( ) === key ) { this . _values . splice ( i , 1 ) ; } } } entries ( ) { const entries = [ ] ; for ( const kv of this . keys ( ) ) { entries . push ( [ kv , this . get ( kv ) ] ) ; } let index = - 1 ; return { next ( ) { index ++ ; return { value : entries [ index ] , done : ! entries [ index ] , } ; } , [ Symbol . iterator ] ( ) { return this ; } , } ; } forEach ( cb ) { for ( const kv of this . entries ( ) ) { cb ( kv [ 1 ] , kv [ 0 ] ) ; } } get ( key ) { const rtn = [ ] ; key = key . toLowerCase ( ) ; for ( const kv of this . _values ) { if ( kv [ 0 ] . toLowerCase ( ) === key ) { rtn . push ( kv [ 1 ] ) ; } } return rtn . length > 0 ? rtn . join ( ', ' ) : null ; } has ( key ) { key = key . toLowerCase ( ) ; for ( const kv of this . _values ) { if ( kv [ 0 ] . toLowerCase ( ) === key ) { return true ; } } return false ; } keys ( ) { const keys = [ ] ; for ( const kv of this . _values ) { const key = kv [ 0 ] . toLowerCase ( ) ; if ( ! keys . includes ( key ) ) { keys . push ( key ) ; } } let index = - 1 ; return { next ( ) { index ++ ; return { value : keys [ index ] , done : ! keys [ index ] , } ; } , [ Symbol . iterator ] ( ) { return this ; } , } ; } set ( key , value ) { for ( const kv of this . _values ) { if ( kv [ 0 ] . toLowerCase ( ) === key . toLowerCase ( ) ) { kv [ 1 ] = value + '' ; return ; } } this . append ( key , value ) ; } values ( ) { const values = this . _values ; let index = - 1 ; return { next ( ) { index ++ ; const done = ! values [ index ] ; return { value : done ? undefined : values [ index ] [ 1 ] , done , } ; } , [ Symbol . iterator ] ( ) { return this ; } , } ; } [ Symbol . iterator ] ( ) { return this . entries ( ) ; } }