@@ -29,6 +29,7 @@ import { authenticator, canApplyAuthenticator } from './authenticator';
29
29
import { requestLogger } from './requestLogger' ;
30
30
import { requestCorrelator } from './requestCorrelator' ;
31
31
import { makeRequestPlugin } from './makeRequestPlugin' ;
32
+ import { authInterceptor , requestCorrelationInterceptor , requestLoggingInterceptor } from './interceptors' ;
32
33
33
34
interface ExtendedCoreOptions extends request . CoreOptions {
34
35
simple ?: boolean ;
@@ -790,6 +791,38 @@ describe('authenticator', () => {
790
791
} ;
791
792
await correlationService . withIdAsync ( work , 'test-123' ) ;
792
793
} ) ;
794
+
795
+ test ( 'interceptors' , async ( ) => {
796
+ // setup
797
+ const { session, states, logger} = setup ( ) ;
798
+
799
+ const auth = authInterceptor ( session ) ;
800
+ const logging = requestLoggingInterceptor ( logger ) ;
801
+ const correlator = requestCorrelationInterceptor ( correlationService ) ;
802
+
803
+ const options : ExtendedCoreOptions = {
804
+ resolveWithFullResponse : true ,
805
+ headers : {
806
+ 'x-correlation-id' : 'custom CID' ,
807
+ } ,
808
+ } ;
809
+ await auth ( options ) ;
810
+ await logging ( options ) ;
811
+ await correlator ( options ) ;
812
+ // act
813
+ const res = await httpGet ( 'http://localhost:4000/api/secured' , options ) ;
814
+ // expect
815
+ expect ( res . statusCode ) . toBe ( 200 ) ;
816
+ expect ( res . request . headers . authorization ) . toBe ( 'Bearer token1' ) ;
817
+ expect ( res . req . getHeader ( 'x-correlation-id' ) ) . toBe ( 'custom CID' ) ;
818
+ expect ( requestCounter ) . toBe ( 1 ) ;
819
+ expect ( states ) . toEqual ( [
820
+ OidcSessionState . acquiringToken ,
821
+ OidcSessionState . tokenAcquired ,
822
+ ] ) ;
823
+ expect ( logger . last ( ) . logType ) . toBe ( 'debug' ) ;
824
+ expect ( logger . last ( ) . messageObj . url ) . toBe ( 'http://localhost:4000/api/secured' ) ;
825
+ } ) ;
793
826
794
827
function setup ( options : ISetupOptions = { } ) {
795
828
setupOptions = options ;
0 commit comments