import { Http , BaseRequestOptions , Response , ResponseOptions , RequestMethod } from "@angular/http" ; import { MockBackend , MockConnection } from "@angular/http/testing" ; export let fakeBackendProvider = { provide : Http , useFactory : ( backend , options ) => { let users = JSON . parse ( localStorage . getItem ( 'users' ) ) || [ ] ; backend . connections . subscribe ( ( connection ) => { setTimeout ( ( ) => { if ( connection . request . url . endsWith ( '/api/authenticate' ) && connection . request . method === RequestMethod . Post ) { let params = JSON . parse ( connection . request . getBody ( ) ) ; let filteredUsers = users . filter ( user => { return user . username === params . username && user . password === params . password ; } ) ; if ( filteredUsers . length ) { let user = filteredUsers [ 0 ] ; connection . mockRespond ( new Response ( new ResponseOptions ( { status : 200 , body : { id : user . id , username : user . username , firstName : user . firstName , lastName : user . lastName , token : 'fake-jwt-token' } } ) ) ) ; } else { connection . mockError ( new Error ( 'Username or password is incorrect' ) ) ; } } if ( connection . request . url . endsWith ( '/api/users' ) && connection . request . method === RequestMethod . Get ) { if ( connection . request . headers . get ( 'Authorization' ) === 'Bearer fake-jwt-token' ) { connection . mockRespond ( new Response ( new ResponseOptions ( { status : 200 , body : users } ) ) ) ; } else { connection . mockRespond ( new Response ( new ResponseOptions ( { status : 401 } ) ) ) ; } } if ( connection . request . url . match ( /\/api\/users\/\d+$/ ) && connection . request . method === RequestMethod . Get ) { if ( connection . request . headers . get ( 'Authorization' ) === 'Bearer fake-jwt-token' ) { let urlParts = connection . request . url . split ( '/' ) ; let id = parseInt ( urlParts [ urlParts . length - 1 ] ) ; let matchedUsers = users . filter ( user => { return user . id === id ; } ) ; let user = matchedUsers . length ? matchedUsers [ 0 ] : null ; connection . mockRespond ( new Response ( new ResponseOptions ( { status : 200 , body : user } ) ) ) ; } else { connection . mockRespond ( new Response ( new ResponseOptions ( { status : 401 } ) ) ) ; } } if ( connection . request . url . endsWith ( '/api/users' ) && connection . request . method === RequestMethod . Post ) { let newUser = JSON . parse ( connection . request . getBody ( ) ) ; let duplicateUser = users . filter ( user => { return user . username === newUser . username ; } ) . length ; if ( duplicateUser ) { return connection . mockError ( new Error ( 'Username "' + newUser . username + '" is already taken' ) ) ; } newUser . id = users . length + 1 ; users . push ( newUser ) ; localStorage . setItem ( 'users' , JSON . stringify ( users ) ) ; connection . mockRespond ( new Response ( new ResponseOptions ( { status : 200 } ) ) ) ; } if ( connection . request . url . match ( /\/api\/users\/\d+$/ ) && connection . request . method === RequestMethod . Delete ) { if ( connection . request . headers . get ( 'Authorization' ) === 'Bearer fake-jwt-token' ) { let urlParts = connection . request . url . split ( '/' ) ; let id = parseInt ( urlParts [ urlParts . length - 1 ] ) ; for ( let i = 0 ; i < users . length ; i ++ ) { let user = users [ i ] ; if ( user . id === id ) { users . splice ( i , 1 ) ; localStorage . setItem ( 'users' , JSON . stringify ( users ) ) ; break ; } } connection . mockRespond ( new Response ( new ResponseOptions ( { status : 200 } ) ) ) ; } else { connection . mockRespond ( new Response ( new ResponseOptions ( { status : 401 } ) ) ) ; } } } , 500 ) ; } ) ; return new Http ( backend , options ) ; } , deps : [ MockBackend , BaseRequestOptions ] } ;