1
1
import { TestBed } from '@angular/core/testing' ;
2
- import { HTTP_INTERCEPTORS , HttpRequest , HttpHandler , HttpResponse , HttpEvent , HttpHeaders , provideHttpClient , withInterceptorsFromDi } from '@angular/common/http' ;
2
+ import { HttpRequest , HttpHandler , HttpResponse , HttpEvent , HttpHeaders , provideHttpClient , withInterceptorsFromDi } from '@angular/common/http' ;
3
3
import { provideHttpClientTesting } from '@angular/common/http/testing' ;
4
4
import { of , Observable , throwError } from 'rxjs' ;
5
5
import { delay } from 'rxjs/operators' ;
6
- import { NgHttpCachingService , NG_HTTP_CACHING_CONFIG , NgHttpCachingHeaders } from './ng-http-caching.service' ;
6
+ import { NgHttpCachingService , NgHttpCachingHeaders } from './ng-http-caching.service' ;
7
7
import { NgHttpCachingInterceptorService } from './ng-http-caching-interceptor.service' ;
8
- import { NgHttpCachingModule } from './ng-http-caching.module ' ;
8
+ import { provideNgHttpCaching } from './ng-http-caching-provider ' ;
9
9
10
10
const DELAY = 50 ;
11
11
12
- class MockHandler extends HttpHandler {
13
-
12
+ class BaseHandler extends HttpHandler {
13
+ constructor ( private response : HttpResponse < any > , private delay ?: number ) {
14
+ super ( )
15
+ }
14
16
// eslint-disable-next-line @typescript-eslint/no-unused-vars
15
- handle ( req : HttpRequest < any > ) : Observable < HttpEvent < any > > {
16
- return of ( new HttpResponse ( { status : 200 , body : { date : new Date ( ) . toJSON ( ) } } ) ) . pipe ( delay ( DELAY ) ) ;
17
+ handle ( _req : HttpRequest < any > ) : Observable < HttpEvent < any > > {
18
+ if ( typeof this . delay === 'number' && this . delay > 0 ) {
19
+ return of ( this . response ) . pipe ( delay ( this . delay ) ) ;
20
+ }
21
+ return of ( this . response ) ;
17
22
}
18
23
}
19
24
20
- class EchoMockHandler extends HttpHandler {
21
- handle ( req : HttpRequest < any > ) : Observable < HttpEvent < any > > {
22
- return of ( new HttpResponse ( { status : 200 , body : req } ) ) . pipe ( delay ( DELAY ) ) ;
25
+ class MockHandler extends BaseHandler {
26
+ constructor ( ) {
27
+ super ( new HttpResponse ( { status : 200 , body : { date : new Date ( ) . toJSON ( ) } } ) , DELAY )
23
28
}
24
29
}
25
30
26
- class ErrorMockHandler extends HttpHandler {
27
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
31
+ class EchoMockHandler extends HttpHandler {
28
32
handle ( req : HttpRequest < any > ) : Observable < HttpEvent < any > > {
29
- return throwError ( ( ) => 'This is an error!' ) . pipe ( delay ( DELAY ) ) ;
33
+ return of ( new HttpResponse ( { status : 200 , body : req } ) ) . pipe ( delay ( DELAY ) ) ;
30
34
}
31
35
}
32
36
33
- class NullMockHandler extends HttpHandler {
34
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
35
- handle ( req : HttpRequest < any > ) : Observable < HttpEvent < any > > {
36
- return of ( null as any ) ;
37
+ class NullMockHandler extends BaseHandler {
38
+ constructor ( ) {
39
+ super ( null as any , DELAY )
37
40
}
38
41
}
39
42
40
- class BaseHandler extends HttpHandler {
41
- constructor ( private response : HttpResponse < any > ) {
42
- super ( )
43
- }
43
+ class ErrorMockHandler extends HttpHandler {
44
44
// eslint-disable-next-line @typescript-eslint/no-unused-vars
45
45
handle ( req : HttpRequest < any > ) : Observable < HttpEvent < any > > {
46
- return of ( this . response ) ;
46
+ return throwError ( ( ) => 'This is an error!' ) . pipe ( delay ( DELAY ) ) ;
47
47
}
48
48
}
49
49
50
+
51
+
50
52
function sleep ( time : number ) : Promise < any > {
51
53
return new Promise ( ( resolve ) => setTimeout ( resolve , time ) ) ;
52
54
}
@@ -57,14 +59,8 @@ describe('NgHttpCachingInterceptorService', () => {
57
59
58
60
beforeEach ( ( ) => {
59
61
TestBed . configureTestingModule ( {
60
- imports : [ NgHttpCachingModule ] ,
61
62
providers : [
62
- { provide : NG_HTTP_CACHING_CONFIG , useValue : { } } ,
63
- {
64
- provide : HTTP_INTERCEPTORS ,
65
- useClass : NgHttpCachingInterceptorService ,
66
- multi : true ,
67
- } ,
63
+ provideNgHttpCaching ( ) ,
68
64
provideHttpClient ( withInterceptorsFromDi ( ) ) ,
69
65
provideHttpClientTesting ( ) ,
70
66
]
@@ -141,10 +137,10 @@ describe('NgHttpCachingInterceptorService', () => {
141
137
142
138
expect ( headers ) . toBeTruthy ( ) ;
143
139
144
- expect ( body . headers . has ( NgHttpCachingHeaders . ALLOW_CACHE ) ) . toBeFalse ( ) ;
145
- expect ( body . headers . has ( NgHttpCachingHeaders . DISALLOW_CACHE ) ) . toBeFalse ( ) ;
146
- expect ( body . headers . has ( NgHttpCachingHeaders . LIFETIME ) ) . toBeFalse ( ) ;
147
- expect ( body . headers . has ( 'CHECK' ) ) . toBeTrue ( ) ;
140
+ expect ( headers . has ( NgHttpCachingHeaders . ALLOW_CACHE ) ) . toBeFalse ( ) ;
141
+ expect ( headers . has ( NgHttpCachingHeaders . DISALLOW_CACHE ) ) . toBeFalse ( ) ;
142
+ expect ( headers . has ( NgHttpCachingHeaders . LIFETIME ) ) . toBeFalse ( ) ;
143
+ expect ( headers . has ( 'CHECK' ) ) . toBeTrue ( ) ;
148
144
149
145
done ( ) ;
150
146
} ) ;
@@ -266,8 +262,8 @@ describe('NgHttpCachingInterceptorService: cache headers', () => {
266
262
267
263
beforeEach ( ( ) => {
268
264
TestBed . configureTestingModule ( {
269
- imports : [ NgHttpCachingModule . forRoot ( { checkResponseHeaders : true } ) ] ,
270
265
providers : [
266
+ provideNgHttpCaching ( { checkResponseHeaders : true } ) ,
271
267
provideHttpClient ( withInterceptorsFromDi ( ) ) ,
272
268
provideHttpClientTesting ( ) ,
273
269
]
0 commit comments