Skip to content

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.

fix(har): do not report zero sizes for bodies that were never read (#42564)

Dmitry Gozmanca667feee5cd
diff --git a/packages/playwright-core/src/server/har/harTracer.ts b/packages/playwright-core/src/server/har/harTracer.ts
index 923ab77d2c..2fca562f56 100644
--- a/packages/playwright-core/src/server/har/harTracer.ts
+++ b/packages/playwright-core/src/server/har/harTracer.ts
@@ -265,9 +265,12 @@ export class HarTracer {
const contentType = event.headers['content-type'];
if (contentType)
content.mimeType = contentType;
- this._storeResponseContent(event.body, content, 'other');
- if (!this._options.omitSizes)
- harEntry.response.bodySize = event.body?.length ?? 0;
+ // Keep the size as -1 "not available" when we did not actually read the body.
+ if (event.body) {
+ this._storeResponseContent(event.body, content, 'other');
+ if (!this._options.omitSizes)
+ harEntry.response.bodySize = event.body.length;
+ }
if (this._started)
this._delegate.onEntryFinished(harEntry);
@@ -541,12 +544,7 @@ export class HarTracer {
this._delegate.onEntryStarted(harEntry);
}
- private _storeResponseContent(buffer: Buffer | undefined, content: har.Content, resourceType: string) {
- if (!buffer) {
- content.size = 0;
- return;
- }
-
+ private _storeResponseContent(buffer: Buffer, content: har.Content, resourceType: string) {
if (!this._options.omitSizes)
content.size = buffer.length;
diff --git a/tests/library/har.spec.ts b/tests/library/har.spec.ts
index 73d2e8bf52..46243c75ef 100644
--- a/tests/library/har.spec.ts
+++ b/tests/library/har.spec.ts
@@ -880,7 +880,8 @@ it('should include redirects from API request', async ({ contextFactory, server
const [redirect, json] = log.entries;
expect(redirect.request.url).toBe(server.PREFIX + '/redirect-me');
expect(json.request.url).toBe(server.PREFIX + '/simple.json');
-
+ expect(redirect.response.bodySize).toBe(-1);
+ expect(redirect.response.content.size).toBe(-1);
expect(redirect.timings).toBeDefined();
expect(json.timings).toBeDefined();
});