Skip to content

Chrome DevTools for coding agents

fix(network): list only earlier hops in a request's redirect chain (#3198)

nancy4675940e04d6
diff --git a/src/formatters/NetworkFormatter.ts b/src/formatters/NetworkFormatter.ts
index bd488340..c0818003 100644
--- a/src/formatters/NetworkFormatter.ts
+++ b/src/formatters/NetworkFormatter.ts
@@ -178,7 +178,13 @@ export class NetworkFormatter {
}
toJSONDetailed(): NetworkRequestDetailed {
- const redirectChain = this.#request.redirectChain();
+ // Puppeteer shares one redirect chain array between all hops of a
+ // redirect, so earlier hops also list themselves and the hops after them.
+ // Keep only the requests that redirected to this one.
+ const fullChain = this.#request.redirectChain();
+ const ownIndex = fullChain.indexOf(this.#request);
+ const redirectChain =
+ ownIndex === -1 ? fullChain : fullChain.slice(0, ownIndex);
const formattedRedirectChain = redirectChain.reverse().map(request => {
const id = this.#options.requestIdResolver
? this.#options.requestIdResolver(request)
diff --git a/tests/formatters/NetworkFormatter.test.ts b/tests/formatters/NetworkFormatter.test.ts
index d52ee5a4..3a2570e3 100644
--- a/tests/formatters/NetworkFormatter.test.ts
+++ b/tests/formatters/NetworkFormatter.test.ts
@@ -374,6 +374,44 @@ describe('NetworkFormatter', () => {
`redirect chain order differs between text and JSON`,
);
});
+ it('lists only earlier hops in the redirect chain of each hop', async () => {
+ // Puppeteer gives every hop of a redirect the same chain array.
+ const chain: HTTPRequest[] = [];
+ const first = getMockRequest({
+ url: 'http://example.com/first',
+ redirectChain: chain,
+ });
+ const second = getMockRequest({
+ url: 'http://example.com/second',
+ redirectChain: chain,
+ });
+ const final = getMockRequest({
+ url: 'http://example.com/final',
+ redirectChain: chain,
+ });
+ chain.push(first, second);
+
+ const chainOf = async (request: HTTPRequest) => {
+ const formatter = await NetworkFormatter.from(request, {
+ requestId: 1,
+ saveFile: async () => ({filename: ''}),
+ redactNetworkHeaders: false,
+ });
+ return (formatter.toJSONDetailed().redirectChain ?? []).map(
+ entry => entry.url,
+ );
+ };
+
+ assert.deepStrictEqual(await chainOf(first), []);
+ assert.deepStrictEqual(await chainOf(second), [
+ 'http://example.com/first',
+ ]);
+ assert.deepStrictEqual(await chainOf(final), [
+ 'http://example.com/second',
+ 'http://example.com/first',
+ ]);
+ });
+
it('shows saved to file message in toStringDetailed', async () => {
const request = {
method: () => 'POST',