Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(network): prevent sending redundant commands #229

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion cypress/app/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ app.get('/', (_: Request, res: Response) =>
{ id: 'multi-targets', name: 'Multi targets' },
{ id: 'server-sent-events', name: 'Server-sent events' },
{ id: 'websocket', name: 'WebSocket' },
{ id: 'large-content', name: 'Large content' }
{ id: 'large-content', name: 'Large content' },
{ id: 'post-data', name: 'Post data' }
]
})
);
Expand Down Expand Up @@ -86,6 +87,7 @@ app.get('/api/products', (_: Request, res: Response) =>
]
})
);
app.get('/api/echo', (req: Request, res: Response) => res.json(req.body));
app.get('/api/keys', (_: Request, res: Response) =>
res.json({
keys: Array(1000)
Expand Down
29 changes: 29 additions & 0 deletions cypress/app/views/post-data.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<html>
<head>
<meta charset='utf-8' />
<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1' />
<meta name='viewport' content='width=device-width' />
<link rel="icon" href="data:;base64,iVBORw0KGgo=">

<title>Post Data</title>
</head>
<body>
<h1>Post Data</h1>
<pre></pre>
</body>
<script>
const postData = {
document: document.body.innerHTML,
};
const pre = document.querySelector('pre')

fetch('/api/echo', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(postData)
})
.then(response => response.json())
.then(data => pre.innerText = JSON.stringify(data, null, 2))
.catch(error => console.error(error));
</script>
</html>
19 changes: 19 additions & 0 deletions cypress/e2e/record-har.cy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,4 +360,23 @@ describe('Record HAR', () => {
}
});
});

it('records a 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: /^\{"document":"/
}
}
});
});
});
24 changes: 24 additions & 0 deletions src/cdp/DefaultNetwork.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
anything,
deepEqual,
instance,
match,
mock,
reset,
verify,
Expand Down Expand Up @@ -369,6 +370,29 @@ describe('DefaultNetwork', () => {
)
).once();
});

it.each([
{ input: 'Network.requestWillBeSent' },
{ input: 'Network.webSocketCreated' }
])(
'should associate a session with a request on $input',
async ({ input }) => {
// arrange
const requestId = '1';
const sessionId = '2';
when(clientMock.on(input, anyFunction())).thenCall((_, callback) =>
callback({ requestId }, sessionId)
);
// act
await sut.attachToTargets(listener);
// assert
verify(
loggerMock.debug(
match(`Session ${sessionId} associated with request: ${requestId}`)
)
).once();
}
);
});

describe('detachFromTargets', () => {
Expand Down
18 changes: 10 additions & 8 deletions src/cdp/DefaultNetwork.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ export class DefaultNetwork implements Network {

this.cdp.on('event', this.networkEventListener);
this.cdp.on('Target.attachedToTarget', this.attachedToTargetListener);

this.cdp.on('Network.requestWillBeSent', this.sessionListener);
this.cdp.on('Network.webSocketCreated', this.sessionListener);
await this.ignoreCertificateError();
await this.trackSessions();
await this.recursivelyAttachToTargets({ type: 'browser' });
}

Expand All @@ -59,6 +59,7 @@ export class DefaultNetwork implements Network {
]);
} catch (e) {
// ADHOC: handle any unforeseen issues while detaching from targets.
this.logger.debug(`Unexpected error while detaching from targets: ${e}`);
}

this.sessions.clear();
Expand Down Expand Up @@ -88,11 +89,6 @@ export class DefaultNetwork implements Network {
);
}

private async trackSessions(): Promise<void> {
this.cdp.on('Network.requestWillBeSent', this.sessionListener);
this.cdp.on('Network.webSocketCreated', this.sessionListener);
}

private async ignoreCertificateError(): Promise<void> {
this.cdp.on('Security.certificateError', this.certificateErrorListener);
try {
Expand Down Expand Up @@ -164,7 +160,12 @@ export class DefaultNetwork implements Network {
| Protocol.Network.RequestWillBeSentEvent
| Protocol.Network.WebSocketCreatedEvent,
sessionId?: string
) => this.sessions.set(requestId, sessionId);
) => {
this.sessions.set(requestId, sessionId);
this.logger.debug(
`Session ${sessionId ?? 'N/A'} associated with request: ${requestId}`
);
};

private attachedToTargetListener = async ({
sessionId,
Expand All @@ -186,6 +187,7 @@ export class DefaultNetwork implements Network {
}
} catch (e) {
this.logger.err(UNABLE_TO_ATTACH_TO_TARGET);
this.logger.err(e);
throw e;
}
};
Expand Down
75 changes: 73 additions & 2 deletions src/network/NetworkObserver.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,9 +200,7 @@ describe('NetworkObserver', () => {
// assert
expect(result).toBe(false);
});
});

describe('subscribe', () => {
it('should attach to network events', async () => {
// act
await sut.subscribe(callback);
Expand Down Expand Up @@ -512,6 +510,79 @@ describe('NetworkObserver', () => {
);
});

it('should load the request body', async () => {
// arrange
when(networkMock.getRequestBody('1')).thenResolve({
postData: 'test'
});
when(networkMock.attachToTargets(anyFunction())).thenCall(async cb => {
await cb({
...requestWillBeSentEvent,
params: {
...requestWillBeSentEvent.params,
request: {
...requestWillBeSentEvent.params.request,
method: 'POST',
hasPostData: true
}
}
});
await cb(responseReceivedEvent);
await cb(loadingFinishedEvent);
});
// act
await sut.subscribe(callback);
// assert
verify(networkMock.getRequestBody('1')).once();
});

it('should utilize the preloaded request body', async () => {
// arrange
when(networkMock.attachToTargets(anyFunction())).thenCall(async cb => {
await cb({
...requestWillBeSentEvent,
params: {
...requestWillBeSentEvent.params,
request: {
...requestWillBeSentEvent.params.request,
method: 'POST',
postData: 'test',
hasPostData: true
}
}
});
await cb(responseReceivedEvent);
await cb(loadingFinishedEvent);
});
// act
await sut.subscribe(callback);
// assert
verify(networkMock.getRequestBody('1')).never();
});

it('should prevent loading the request body when the request method forbids the body', async () => {
// arrange
when(networkMock.attachToTargets(anyFunction())).thenCall(async cb => {
await cb({
...requestWillBeSentEvent,
params: {
...requestWillBeSentEvent.params,
request: {
...requestWillBeSentEvent.params.request,
method: 'OPTIONS',
hasPostData: false
}
}
});
await cb(responseReceivedEvent);
await cb(loadingFinishedEvent);
});
// act
await sut.subscribe(callback);
// assert
verify(networkMock.getRequestBody('1')).never();
});

it('should filter a request out when it does not match a filter criteria', async () => {
// arrange
when(requestFilterMock.wouldApply(anything())).thenReturn(true);
Expand Down
10 changes: 6 additions & 4 deletions src/network/NetworkObserver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -457,17 +457,19 @@ export class NetworkObserver implements Observer<NetworkRequest> {
request.headers
);
chromeRequest.setRequestFormData(
this.getRequestPostData(chromeRequest, request)
request.hasPostData
? this.getRequestPostData(chromeRequest, request)
: Promise.resolve(undefined)
);
chromeRequest.initialPriority = request.initialPriority;
}

private getRequestPostData(
request: NetworkRequest,
rawRequest: Protocol.Network.Request
): string | Promise<string | undefined> {
return rawRequest.hasPostData && rawRequest.postData
? rawRequest.postData
): Promise<string | undefined> {
return rawRequest.postData !== undefined
? Promise.resolve(rawRequest.postData)
: this.network
.getRequestBody(request.requestId)
.then(
Expand Down