1
- import { Http , BaseRequestOptions , Response , ResponseOptions , RequestMethod } from '@angular/http' ;
2
- import { MockBackend , MockConnection } from '@angular/http/testing' ;
3
-
4
- export function fakeBackendFactory ( backend : MockBackend , options : BaseRequestOptions ) {
5
- // configure fake backend
6
- backend . connections . subscribe ( ( connection : MockConnection ) => {
7
- let testUser = { username : 'test' , password : 'test' , firstName : 'Test' , lastName : 'User' } ;
8
-
9
- // wrap in timeout to simulate server api call
10
- setTimeout ( ( ) => {
11
-
12
- // fake authenticate api end point
13
- if ( connection . request . url . endsWith ( '/api/authenticate' ) && connection . request . method === RequestMethod . Post ) {
14
- // get parameters from post request
15
- let params = JSON . parse ( connection . request . getBody ( ) ) ;
16
-
17
- // check user credentials and return fake jwt token if valid
18
- if ( params . username === testUser . username && params . password === testUser . password ) {
19
- connection . mockRespond ( new Response (
20
- new ResponseOptions ( { status : 200 , body : { token : 'fake-jwt-token' } } )
21
- ) ) ;
1
+ import { Injectable } from '@angular/core' ;
2
+ import { HttpRequest , HttpResponse , HttpHandler , HttpEvent , HttpInterceptor , HTTP_INTERCEPTORS } from '@angular/common/http' ;
3
+ import { Observable } from 'rxjs/Observable' ;
4
+ import 'rxjs/add/observable/of' ;
5
+ import 'rxjs/add/observable/throw' ;
6
+ import 'rxjs/add/operator/delay' ;
7
+ import 'rxjs/add/operator/mergeMap' ;
8
+ import 'rxjs/add/operator/materialize' ;
9
+ import 'rxjs/add/operator/dematerialize' ;
10
+
11
+ @Injectable ( )
12
+ export class FakeBackendInterceptor implements HttpInterceptor {
13
+
14
+ constructor ( ) { }
15
+
16
+ intercept ( request : HttpRequest < any > , next : HttpHandler ) : Observable < HttpEvent < any > > {
17
+ let testUser = { id : 1 , username : 'test' , password : 'test' , firstName : 'Test' , lastName : 'User' } ;
18
+
19
+ // wrap in delayed observable to simulate server api call
20
+ return Observable . of ( null ) . mergeMap ( ( ) => {
21
+
22
+ // authenticate
23
+ if ( request . url . endsWith ( '/api/authenticate' ) && request . method === 'POST' ) {
24
+ if ( request . body . username === testUser . username && request . body . password === testUser . password ) {
25
+ // if login details are valid return 200 OK with a fake jwt token
26
+ return Observable . of ( new HttpResponse ( { status : 200 , body : { token : 'fake-jwt-token' } } ) ) ;
22
27
} else {
23
- connection . mockRespond ( new Response (
24
- new ResponseOptions ( { status : 200 } )
25
- ) ) ;
28
+ // else return 400 bad request
29
+ return Observable . throw ( 'Username or password is incorrect' ) ;
26
30
}
27
31
}
28
32
29
- // fake users api end point
30
- if ( connection . request . url . endsWith ( '/api/users' ) && connection . request . method === RequestMethod . Get ) {
31
- // check for fake auth token in header and return test users if valid, this security is implemented server side
32
- // in a real application
33
- if ( connection . request . headers . get ( 'Authorization' ) === 'Bearer fake-jwt-token' ) {
34
- connection . mockRespond ( new Response (
35
- new ResponseOptions ( { status : 200 , body : [ testUser ] } )
36
- ) ) ;
33
+ // get users
34
+ if ( request . url . endsWith ( '/api/users' ) && request . method === 'GET' ) {
35
+ // check for fake auth token in header and return users if valid, this security is implemented server side in a real application
36
+ if ( request . headers . get ( 'Authorization' ) === 'Bearer fake-jwt-token' ) {
37
+ return Observable . of ( new HttpResponse ( { status : 200 , body : [ testUser ] } ) ) ;
37
38
} else {
38
39
// return 401 not authorised if token is null or invalid
39
- connection . mockRespond ( new Response (
40
- new ResponseOptions ( { status : 401 } )
41
- ) ) ;
40
+ return Observable . throw ( 'Unauthorised' ) ;
42
41
}
43
42
}
44
43
45
- } , 500 ) ;
44
+ // pass through any requests not handled above
45
+ return next . handle ( request ) ;
46
+
47
+ } )
46
48
47
- } ) ;
48
-
49
- return new Http ( backend , options ) ;
49
+ // call materialize and dematerialize to ensure delay even if an error is thrown (https://github.com/Reactive-Extensions/RxJS/issues/648)
50
+ . materialize ( )
51
+ . delay ( 500 )
52
+ . dematerialize ( ) ;
53
+ }
50
54
}
51
55
52
56
export let fakeBackendProvider = {
53
57
// use fake backend in place of Http service for backend-less development
54
- provide : Http ,
55
- useFactory : fakeBackendFactory ,
56
- deps : [ MockBackend , BaseRequestOptions ]
58
+ provide : HTTP_INTERCEPTORS ,
59
+ useClass : FakeBackendInterceptor ,
60
+ multi : true
57
61
} ;
0 commit comments