From 44a47e5e01cbade4f010d6f6644ac01a1db245a1 Mon Sep 17 00:00:00 2001 From: Artem Derevnjuk Date: Tue, 7 Mar 2023 18:09:56 +0400 Subject: [PATCH] fix(cdp): call callback after session has been associated with request (#231) closes #230 --- cypress/app/index.ts | 9 ++++++-- cypress/app/views/post-data.hbs | 37 ++++++++++++++++++++++++++------- cypress/e2e/record-har.cy.ts | 23 ++++++++++++++++++-- src/cdp/DefaultNetwork.spec.ts | 4 ++++ src/cdp/DefaultNetwork.ts | 9 +++----- 5 files changed, 65 insertions(+), 17 deletions(-) diff --git a/cypress/app/index.ts b/cypress/app/index.ts index eca10f0..a1affeb 100644 --- a/cypress/app/index.ts +++ b/cypress/app/index.ts @@ -1,4 +1,4 @@ -import express, { Request, Response, json } from 'express'; +import express, { Request, Response, json, raw } from 'express'; import minimist from 'minimist'; import WebSocket, { Server } from 'ws'; import { join } from 'path'; @@ -87,7 +87,12 @@ app.get('/api/products', (_: Request, res: Response) => ] }) ); -app.get('/api/echo', (req: Request, res: Response) => res.json(req.body)); +app.post( + '/api/echo', + raw({ type: ['text/xml', 'application/json'] }), + (req: Request, res: Response) => + res.send(Buffer.isBuffer(req.body) ? req.body.toString() : req.body) +); app.get('/api/keys', (_: Request, res: Response) => res.json({ keys: Array(1000) diff --git a/cypress/app/views/post-data.hbs b/cypress/app/views/post-data.hbs index f6a7698..5801cb1 100644 --- a/cypress/app/views/post-data.hbs +++ b/cypress/app/views/post-data.hbs @@ -9,21 +9,44 @@

Post Data

-

+    
+ + Text + +

+    
+
+ + Blob + +

+    
diff --git a/cypress/e2e/record-har.cy.ts b/cypress/e2e/record-har.cy.ts index 9b90ab1..b6c2d39 100644 --- a/cypress/e2e/record-har.cy.ts +++ b/cypress/e2e/record-har.cy.ts @@ -361,7 +361,7 @@ describe('Record HAR', () => { }); }); - it('records a request body', () => { + it('records a text request body', () => { cy.recordHar({ content: false }); cy.get('a[href$=post-data]').click(); @@ -374,7 +374,26 @@ describe('Record HAR', () => { request: { url: /api\/echo$/, postData: { - text: /^\{"document":"/ + text: /^\{"name":"/ + } + } + }); + }); + + it('records a blob request body', () => { + cy.recordHar({ content: false }); + + cy.get('a[href$=post-data]').click(); + + cy.saveHar({ waitForIdle: true }); + + cy.findHar() + .its('log.entries') + .should('contain.something.like', { + request: { + url: /api\/echo$/, + postData: { + text: /^ { let sut!: DefaultNetwork; beforeEach(() => { + jest.useFakeTimers(); sut = new DefaultNetwork(instance(clientMock), instance(loggerMock)); }); afterEach(() => { + jest.useRealTimers(); listener.mockReset(); reset(clientMock, loggerMock); }); @@ -74,6 +76,8 @@ describe('DefaultNetwork', () => { when(clientMock.on('event', anyFunction())).thenCall((_, callback) => { callback(unexpectedEvent); callback(expectedEvent); + // ADHOC: spawn `setImmediate` immediately + jest.runAllTimers(); }); // act await sut.attachToTargets(listener); diff --git a/src/cdp/DefaultNetwork.ts b/src/cdp/DefaultNetwork.ts index 06294a2..03acdcd 100644 --- a/src/cdp/DefaultNetwork.ts +++ b/src/cdp/DefaultNetwork.ts @@ -104,12 +104,9 @@ export class DefaultNetwork implements Network { } } - private networkEventListener = async (eventMessage: EventMessage) => { - if ( - this.matchNetworkEvents(eventMessage) && - typeof this.listener === 'function' - ) { - this.listener(eventMessage); + private networkEventListener = (eventMessage: EventMessage) => { + if (this.matchNetworkEvents(eventMessage)) { + setImmediate(() => this.listener?.(eventMessage)); } };