import * as SUT from '../../playwright-fluent' ; import { stringifyRequest , RequestInfo , getHarResponseFor , getHarDataFrom , getHarResponseContentAs , harHeadersToHttpHeaders , } from '../../../utils' ; import { FakeServer } from 'simple-fake-server' ; import * as path from 'path' ; import { readFileSync } from 'fs' ; describe ( 'Playwright Fluent - onRequestTo(url).respondWith()' , ( ) : void => { let p ; let fakeServer = undefined ; beforeAll ( ( ) => { fakeServer = new FakeServer ( 1242 ) ; fakeServer . start ( ) ; } ) ; afterAll ( ( ) => { if ( fakeServer ) { fakeServer . stop ( ) ; } } ) ; beforeEach ( ( ) : void => { p = new SUT . PlaywrightFluent ( ) ; } ) ; afterEach ( async ( ) : Promise < void > => { await p . close ( ) ; } ) ; test ( 'should intercept only POST requests to a rest API' , async ( ) : Promise < void > => { const htmlContent = readFileSync ( `${ path . join ( __dirname , 'on-request-to-respond-with.post.test.html' ) }` ) ; fakeServer && fakeServer . http . get ( ) . to ( '/app' ) . willReturn ( htmlContent . toString ( ) , 200 ) ; interface CustomResponseBody { prop1 : string ; prop2 ? : string ; } const responseBody = { prop1 : 'foobar' , } ; const responseHeaders = { 'foo-header' : 'bar' , } ; fakeServer && fakeServer . http . post ( ) . to ( '/foobar' ) . willReturn ( responseBody , 200 , responseHeaders ) ; const mockResponseBody = { prop1 : 'mocked-prop1' , prop2 : 'mocked-prop2' } ; await p . withBrowser ( 'chromium' ) . withOptions ( { headless : true } ) . withCursor ( ) . recordRequestsTo ( '/foobar' ) . onRequestTo ( '/foobar' , { method : 'POST' } ) . respondWith < CustomResponseBody > ( { status : 200 , headers : responseHeaders , body : mockResponseBody , } ) . navigateTo ( 'http://localhost:1242/app' ) ; await p . waitForStabilityOf ( async ( ) => p . getRecordedRequestsTo ( '/foobar' ) . length , { stabilityInMilliseconds : 1000 , } ) ; const foobarRequests = p . getRecordedRequestsTo ( '/foobar' ) ; expect ( Array . isArray ( foobarRequests ) ) . toBe ( true ) ; expect ( foobarRequests . length ) . toBe ( 2 ) ; const stringifiedSentRequest = await stringifyRequest ( foobarRequests [ 1 ] ) ; const sentRequest = JSON . parse ( stringifiedSentRequest ) as RequestInfo ; expect ( sentRequest . url ) . toContain ( '?foo=bar' ) ; expect ( sentRequest . response ! . status ) . toBe ( 200 ) ; expect ( sentRequest . response ! . headers [ 'foo-header' ] ) . toBe ( responseHeaders [ 'foo-header' ] ) ; expect ( sentRequest . response ! . payload ) . toMatchObject ( mockResponseBody ) ; } ) ; test ( 'should intercept only POST requests to a rest API - functional version' , async ( ) : Promise < void > => { const htmlContent = readFileSync ( `${ path . join ( __dirname , 'on-request-to-respond-with.post.test.html' ) }` ) ; fakeServer && fakeServer . http . get ( ) . to ( '/app' ) . willReturn ( htmlContent . toString ( ) , 200 ) ; interface CustomResponseBody { prop1 : string ; prop2 ? : string ; } const responseBody = { prop1 : 'foobar' , } ; const responseHeaders = { 'foo-header' : 'bar' , } ; fakeServer && fakeServer . http . post ( ) . to ( '/foobar' ) . willReturn ( responseBody , 200 , responseHeaders ) ; const mockResponseBody = { prop1 : 'mocked-prop1' , prop2 : 'mocked-prop2' } ; await p . withBrowser ( 'chromium' ) . withOptions ( { headless : true } ) . withCursor ( ) . recordRequestsTo ( '/foobar' ) . onRequestTo ( '/foobar' , { method : 'POST' } ) . respondWith < CustomResponseBody > ( ( request ) => { const url = request . url ( ) ; console . log ( url ) ; return { status : 200 , headers : responseHeaders , body : mockResponseBody , } ; } ) . navigateTo ( 'http://localhost:1242/app' ) ; await p . waitForStabilityOf ( async ( ) => p . getRecordedRequestsTo ( '/foobar' ) . length , { stabilityInMilliseconds : 1000 , } ) ; const foobarRequests = p . getRecordedRequestsTo ( '/foobar' ) ; expect ( Array . isArray ( foobarRequests ) ) . toBe ( true ) ; expect ( foobarRequests . length ) . toBe ( 2 ) ; const stringifiedSentRequest = await stringifyRequest ( foobarRequests [ 1 ] ) ; const sentRequest = JSON . parse ( stringifiedSentRequest ) as RequestInfo ; expect ( sentRequest . url ) . toContain ( '?foo=bar' ) ; expect ( sentRequest . response ! . status ) . toBe ( 200 ) ; expect ( sentRequest . response ! . headers [ 'foo-header' ] ) . toBe ( responseHeaders [ 'foo-header' ] ) ; expect ( sentRequest . response ! . payload ) . toMatchObject ( mockResponseBody ) ; } ) ; test ( 'should mock reponse with data coming from a HAR file' , async ( ) : Promise < void > => { const htmlContent = readFileSync ( `${ path . join ( __dirname , 'on-request-to-respond-with.post.test.html' ) }` ) ; const harFile = path . join ( __dirname , 'on-request-to-respond-with.har' ) ; const harData = getHarDataFrom ( harFile ) ; fakeServer && fakeServer . http . get ( ) . to ( '/app' ) . willReturn ( htmlContent . toString ( ) , 200 ) ; interface CustomResponseBody { prop1 : string ; prop2 ? : string ; } const responseBody = { prop1 : 'foobar' , } ; const responseHeaders = { 'foo-header' : 'bar' , } ; const mockResponseBody = { prop1 : 'mocked-prop1' , prop2 : 'mocked-prop2' } ; fakeServer && fakeServer . http . post ( ) . to ( '/foobar' ) . willReturn ( responseBody , 200 , responseHeaders ) ; fakeServer && fakeServer . http . get ( ) . to ( '/foobar' ) . willReturn ( responseBody , 200 , responseHeaders ) ; await p . withBrowser ( 'chromium' ) . withOptions ( { headless : true } ) . withCursor ( ) . recordRequestsTo ( '/foobar' ) . onRequestTo ( '/foobar' , { method : 'POST' } ) . respondWith < CustomResponseBody > ( ( request ) => { const harResponse = getHarResponseFor ( request , harData ) ; const response = getHarResponseContentAs < CustomResponseBody > ( harResponse ) ; return { status : harResponse ?. status , headers : harHeadersToHttpHeaders ( harResponse ?. headers ) , body : response , } ; } ) . navigateTo ( 'http://localhost:1242/app' ) ; await p . waitForStabilityOf ( async ( ) => p . getRecordedRequestsTo ( '/foobar' ) . length , { stabilityInMilliseconds : 1000 , } ) ; const foobarRequests = p . getRecordedRequestsTo ( '/foobar' ) ; expect ( Array . isArray ( foobarRequests ) ) . toBe ( true ) ; expect ( foobarRequests . length ) . toBe ( 2 ) ; const stringifiedSentRequest = await stringifyRequest ( foobarRequests [ 1 ] ) ; const sentRequest = JSON . parse ( stringifiedSentRequest ) as RequestInfo ; expect ( sentRequest . url ) . toContain ( '?foo=bar' ) ; expect ( sentRequest . response ! . status ) . toBe ( 200 ) ; expect ( sentRequest . response ! . headers [ 'foo-header' ] ) . toBe ( responseHeaders [ 'foo-header' ] ) ; expect ( sentRequest . response ! . payload ) . toMatchObject ( mockResponseBody ) ; } ) ; } ) ;