import nock from 'nock' ; import { basicHeader } from '@sap-cloud-sdk/connectivity/internal' ; import { ServiceCredentials } from '@sap-cloud-sdk/connectivity' ; export function mockClientCredentialsGrantCall ( uri , response , responseCode , serviceCredentials ) { return nock ( uri , { reqheaders : xsuaaRequestHeaders ( ) } ) . post ( '/oauth/token' , { grant_type : 'client_credentials' , client_id : serviceCredentials . clientid , client_secret : serviceCredentials . clientsecret , response_type : 'token' } ) . reply ( responseCode , response ) ; } export function mockClientCredentialsGrantWithCertCall ( uri , response , responseCode , serviceCredentials ) { return nock ( uri , { reqheaders : xsuaaRequestHeaders ( ) } ) . post ( '/oauth/token' , { grant_type : 'client_credentials' , client_id : serviceCredentials . clientid , response_type : 'token' } ) . reply ( responseCode , function ( ) { const agentOptions = ( this . req as any ) . options . agent . options ; expect ( agentOptions . cert ) . toEqual ( serviceCredentials . certificate ) ; expect ( agentOptions . key ) . toEqual ( serviceCredentials . key ) ; return response ; } ) ; } export function mockUserTokenGrantCall ( uri , response , responseCode , accessToken , clientId ) { return nock ( uri , { reqheaders : xsuaaRequestHeaders ( { authorization : `Bearer ${ accessToken }` } ) } ) . post ( '/oauth/token' , { client_id : clientId , grant_type : 'user_token' , response_type : 'token' } ) . reply ( responseCode , response ) ; } export function mockRefreshTokenGrantCall ( uri , response , responseCode , refreshToken , clientId , clientSecret ) { return nock ( uri , { reqheaders : xsuaaRequestHeaders ( { authorization : basicHeader ( clientId , clientSecret ) } ) } ) . post ( '/oauth/token' , { grant_type : 'refresh_token' , refresh_token : refreshToken } ) . reply ( responseCode , response ) ; } function xsuaaRequestHeaders ( additionalHeaders = { } ) { return { 'Content-Type' : 'application/x-www-form-urlencoded' , Accept : 'application/json' , ... additionalHeaders } ; }