Skip to content
Open
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
20 changes: 17 additions & 3 deletions packages/playwright-core/src/server/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,19 @@ export abstract class APIRequestContext extends SdkObject {
// Brotli and deflate decompressors throw if the input stream is empty.
const emptyStreamTransform = new SafeEmptyStreamTransform(notifyBodyFinished);
body = pipeline(response, emptyStreamTransform, transform, e => {
if (e)
reject(new Error(`failed to decompress '${encoding}' encoding: ${e.message}`));
if (e) {
if (isNetworkConnectionError(e))
reject(e);
else
reject(new Error(`failed to decompress '${encoding}' encoding: ${e.message}`));
}
});
body.on('error', e => {
if (isNetworkConnectionError(e))
reject(e);
else
reject(new Error(`failed to decompress '${encoding}' encoding: ${e}`));
});
body.on('error', e => reject(new Error(`failed to decompress '${encoding}' encoding: ${e}`)));
} else {
body.on('error', reject);
}
Expand Down Expand Up @@ -804,6 +813,11 @@ function removeHeader(headers: { [name: string]: string }, name: string) {
delete headers[existing[0]];
}

function isNetworkConnectionError(e: any): boolean {
const code = e?.code;
return code === 'ECONNRESET' || code === 'EPIPE' || code === 'ECONNABORTED';
}

function setBasicAuthorizationHeader(headers: { [name: string]: string }, credentials: HTTPCredentials) {
const { username, password } = credentials;
const encoded = Buffer.from(`${username || ''}:${password || ''}`).toString('base64');
Expand Down
25 changes: 25 additions & 0 deletions tests/library/browsercontext-fetch.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1401,3 +1401,28 @@ it('should retry on ECONNRESET', {
expect(await response.text()).toBe('Hello!');
expect(requestCount).toBe(4);
});

it('should retry ECONNRESET on compressed response', async ({ context, server }) => {
let requestCount = 0;
server.setRoute('/test-gzip', (req, res) => {
if (requestCount++ < 2) {
req.socket.destroy();
return;
}
res.writeHead(200, {
'Content-Encoding': 'gzip',
'Content-Type': 'text/plain',
});
const gzipStream = zlib.createGzip();
pipeline(gzipStream, res, err => {
if (err)
console.log(`Server error: ${err}`);
});
gzipStream.write('compressed-retry-ok');
gzipStream.end();
});
const response = await context.request.get(server.PREFIX + '/test-gzip', { maxRetries: 3 });
expect(response.status()).toBe(200);
expect(await response.text()).toBe('compressed-retry-ok');
expect(requestCount).toBe(3);
});