import { HttpHeaders , HttpRequestMethod } from '../../../utils' ; import { Page , Request } from 'playwright' ; export interface MockedResponse < T > { status : number ; headers : HttpHeaders ; contentType : string ; body : T ; } function serializeBody < T > ( body ) { if ( typeof body === 'string' ) { return body as string ; } try { return JSON . stringify ( body , null , 2 ) ; } catch ( error ) { const buffer = body as unknown as Buffer ; return buffer ; } } function buildPlaywrightResponseWith < T > ( mockedResponse ) { return { headers : buildHeadersFrom ( mockedResponse ) , contentType : buildContentTypeFrom ( mockedResponse ) , status : mockedResponse . status || 200 , body : serializeBody ( mockedResponse . body ) , } ; } function buildHeadersFrom < T > ( mockedResponse ) { const headers = { ... mockedResponse . headers , } ; if ( ! hasAccessControlAllowCredentialsHeader ( headers ) ) { headers [ 'Access-Control-Allow-Credentials' ] = 'true' ; } if ( ! hasAccessControlAllowOriginHeader ( headers ) ) { headers [ 'Access-Control-Allow-Origin' ] = '*' ; } return headers ; } function hasAccessControlAllowCredentialsHeader ( headers ) { if ( headers [ 'access-control-allow-credentials' ] ) { return true ; } if ( headers [ 'Access-Control-Allow-Credentials' ] ) { return true ; } return false ; } function hasAccessControlAllowOriginHeader ( headers ) { if ( headers [ 'access-control-allow-origin' ] ) { return true ; } if ( headers [ 'Access-Control-Allow-Origin' ] ) { return true ; } return false ; } function buildContentTypeFrom < T > ( mockedResponse ) { if ( mockedResponse . contentType ) { return mockedResponse . contentType ; } if ( typeof mockedResponse . body === 'string' ) { return 'text/plain' ; } return 'application/json' ; } export interface RequestInterceptionFilterOptions { type { HttpRequestMethod } memberof method ? : HttpRequestMethod ; memberof bypassPredicate ? : ( request : Request ) => boolean ; } export async function onRequestToRespondWith < T > ( url , options , response , page ) { if ( ! page ) { throw new Error ( `Cannot intercept requests to '${ url }' because no browser has been launched` ) ; } await page . route ( ( uri ) => { return uri . toString ( ) . includes ( url ) ; } , ( route , request ) => { const requestMethod = request . method ( ) ; if ( options && typeof options . method === 'string' && options . method !== requestMethod ) { route . continue ( ) ; return ; } if ( options && typeof options . bypassPredicate === 'function' && options . bypassPredicate ( request ) ) { route . continue ( ) ; return ; } const mockedResponse = typeof response === 'function' ? response ( request ) : response ; const playwrightResponse = buildPlaywrightResponseWith ( mockedResponse ) ; route . fulfill ( playwrightResponse ) ; } ) ; }