Skip to content

Commit

Permalink
fix(cdp): call callback after session has been associated with request (
Browse files Browse the repository at this point in the history
#231)

closes #230
  • Loading branch information
derevnjuk authored Mar 7, 2023
1 parent 25b9a69 commit 44a47e5
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
9 changes: 7 additions & 2 deletions cypress/app/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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)
Expand Down
37 changes: 30 additions & 7 deletions cypress/app/views/post-data.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,44 @@
</head>
<body>
<h1>Post Data</h1>
<pre></pre>
<details>
<summary>
Text
</summary>
<pre id="text"></pre>
</details>
<details>
<summary>
Blob
</summary>
<pre id="blob"></pre>
</details>
</body>
<script>
const postData = {
document: document.body.innerHTML,
};
const pre = document.querySelector('pre')
const text = document.querySelector('#text');
const blob = document.querySelector('#blob');
const blobContent = new XMLSerializer().serializeToString(document);
fetch('/api/echo', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(postData)
body: JSON.stringify({
name: 'John Doe',
email: '[email protected]',
message: 'Hello, world!'
})
})
.then(response => response.json())
.then(data => pre.innerText = JSON.stringify(data, null, 2))
.then(data => text.innerText = JSON.stringify(data, null, 2))
.catch(error => console.error(error));
fetch('/api/echo', {
method: 'POST',
headers: { 'Content-Type': 'text/xml' },
body: new Blob([blobContent])
})
.then(response => response.text())
.then(data => blob.innerText = data)
.catch(error => console.error(error));
</script>
</html>
23 changes: 21 additions & 2 deletions cypress/e2e/record-har.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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: /^<html xmlns/
}
}
});
Expand Down
4 changes: 4 additions & 0 deletions src/cdp/DefaultNetwork.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ describe('DefaultNetwork', () => {
let sut!: DefaultNetwork;

beforeEach(() => {
jest.useFakeTimers();
sut = new DefaultNetwork(instance(clientMock), instance(loggerMock));
});

afterEach(() => {
jest.useRealTimers();
listener.mockReset();
reset<Client | Logger>(clientMock, loggerMock);
});
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 3 additions & 6 deletions src/cdp/DefaultNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
};

Expand Down

0 comments on commit 44a47e5

Please sign in to comment.