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: capture redirected requests in waterfall #180

Merged
merged 2 commits into from
Jan 12, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion __tests__/plugins/network.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('network', () => {
await server.close();
});

it('should capture network info', async () => {
it('capture network info', async () => {
const driver = await Gatherer.setupDriver();
const network = new NetworkManager();
await network.start(driver.client);
Expand All @@ -51,4 +51,25 @@ describe('network', () => {
});
await Gatherer.dispose(driver);
});

it('produce distinct events for redirects', async () => {
const driver = await Gatherer.setupDriver();
const network = new NetworkManager();
await network.start(driver.client);
/**
* Set up two level of redirects
*/
server.setRedirect('/route1', '/route2');
server.setRedirect('/route2', '/route3');
server.route('/route3', (req, res) => {
res.end('route3');
});
await driver.page.goto(server.PREFIX + '/route1');
const netinfo = await network.stop();
expect(netinfo.length).toEqual(3);
expect(netinfo[0].status).toBe(302);
expect(netinfo[1].status).toBe(302);
expect(netinfo[2].status).toBe(200);
await Gatherer.dispose(driver);
});
});
38 changes: 38 additions & 0 deletions src/plugins/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,44 @@ export class NetworkManager {
const { requestId, request, timestamp, type, loaderId } = event;
const { url, method } = request;
const isNavigationRequest = requestId == loaderId && type === 'Document';
const record = this.waterfallMap.get(requestId);
/**
* On redirects, another `requestWillBeSent` event will be fired for the
* same requestId. We calculate the timings for the redirect request using
* the `redirectedResponse` from the redirected request.
*/
if (record) {
if (event.redirectResponse) {
const response = event.redirectResponse;
const data = Object.assign(event, {
type: event.type,
response,
encodedDataLength: response.encodedDataLength,
});
this._onResponseReceived(data);
this._onLoadingFinished(data);
} else {
/**
* Edge case, we handle it to proceed with next navigation
*/
this._onLoadingFailed(
Object.assign(event, {
type: event.type,
errorText: 'redirectResponse data is not available',
})
);
}
const redirectedRecord = this.waterfallMap.get(requestId);
vigneshshanmugam marked this conversation as resolved.
Show resolved Hide resolved
/**
* Rewrite the map with new redirect id to not reset
* the redirect request with original request
*/
this.waterfallMap.delete(requestId);
this.waterfallMap.set(
`redirect:${timestamp}:${requestId}`,
redirectedRecord
);
}

this.waterfallMap.set(requestId, {
step: this._currentStep,
Expand Down
11 changes: 10 additions & 1 deletion utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { join } from 'path';
import { AddressInfo } from 'net';

export class Server {
PREFIX: string;
TEST_PAGE: string;
_server: http.Server;
_routes = new Map<string, RequestListener>();
Expand All @@ -45,7 +46,15 @@ export class Server {
this._server = http.createServer(this._onRequest.bind(this));
this._server.listen(0);
const { port } = this._server.address() as AddressInfo;
this.TEST_PAGE = `http://localhost:${port}/index.html`;
this.PREFIX = `http://localhost:${port}`;
this.TEST_PAGE = `${this.PREFIX}/index.html`;
}

setRedirect(from, to) {
this.route(from, (req, res) => {
res.writeHead(302, { location: to });
res.end();
});
}

route(path, handler: RequestListener) {
Expand Down