Skip to content
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
10 changes: 6 additions & 4 deletions packages/playwright-core/src/server/har/harTracer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
* limitations under the License.
*/

import { isUtf8 } from 'buffer';

import mime from 'mime';
import { base64ByteLength } from '@isomorphic/base64';
import { ManualPromise } from '@isomorphic/manualPromise';
Expand Down Expand Up @@ -268,7 +270,7 @@ export class HarTracer {
content.mimeType = contentType;
// Keep the size as -1 "not available" when we did not actually read the body.
if (event.body) {
this._storeResponseContent(event.body, content, 'other');
this._storeResponseContent(event.body, content);
if (!this._options.omitSizes)
harEntry.response.bodySize = event.body.length;
}
Expand Down Expand Up @@ -380,7 +382,7 @@ export class HarTracer {

const content = harEntry.response.content;
compressionCalculationBarrier?.setDecodedBodySize(buffer.length);
this._storeResponseContent(buffer, content, request.resourceType());
this._storeResponseContent(buffer, content);
}).catch(() => {
compressionCalculationBarrier?.setDecodedBodySize(0);
}).then(() => {
Expand Down Expand Up @@ -545,14 +547,14 @@ export class HarTracer {
this._delegate.onEntryStarted(harEntry);
}

private _storeResponseContent(buffer: Buffer, content: har.Content, resourceType: string) {
private _storeResponseContent(buffer: Buffer, content: har.Content) {
if (!this._options.omitSizes)
content.size = buffer.length;

if (this._options.content === 'embed') {
// Sometimes, we can receive a font/media file with textual mime type. Browser
// still interprets them correctly, but the 'content-type' header is obviously wrong.
if (isTextualMimeType(content.mimeType) && resourceType !== 'font') {
if (isTextualMimeType(content.mimeType) && isUtf8(buffer)) {
content.text = buffer.toString();
} else {
content.text = buffer.toString('base64');
Expand Down
18 changes: 18 additions & 0 deletions tests/library/har.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,24 @@ it('HA-021 should include content @smoke', async ({ contextFactory, server }, te
expect(log.entries[2].response.content.compression).toBe(0);
});

it('HA-113 should store binary content with a textual mime type as base64', {
annotation: { type: 'issue', description: 'https://github.com/microsoft/playwright/issues/43493' },
}, async ({ contextFactory, server, browserName }, testInfo) => {
it.fixme(browserName === 'webkit', 'WebKit decodes the body of a textual resource before it gets to us');
const body = Buffer.from([0x80, 0xc3, 0x28, 0xff, 0x41]);
server.setRoute('/binary.js', (req, res) => {
res.setHeader('Content-Type', 'application/javascript');
res.end(body);
});
const { page, getLog } = await pageWithHar(contextFactory, testInfo);
await page.goto(server.EMPTY_PAGE);
await page.evaluate(() => fetch('/binary.js').then(r => r.arrayBuffer()));
const log = await getLog();
const content = log.entries.find(entry => entry.request.url.endsWith('/binary.js'))!.response.content;
expect(content.encoding).toBe('base64');
expect(Buffer.from(content.text!, 'base64')).toEqual(body);
});

it('HA-022 should use attach mode for zip extension', async ({ contextFactory, server }, testInfo) => {
const { page, getZip } = await pageWithHar(contextFactory, testInfo, { outputPath: 'test.har.zip' });
await page.goto(server.PREFIX + '/har.html');
Expand Down
5 changes: 3 additions & 2 deletions tests/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ subsystem, add a row to the Subsystems table. Then update this file:

## Subsystems

98 subsystems, 4933 tests (5330 with loop copies).
98 subsystems, 4934 tests (5331 with loop copies).

| Code | Subsystem | Files | Tests |
|---|---|---|---|
Expand Down Expand Up @@ -78,7 +78,7 @@ subsystem, add a row to the Subsystems table. Then update this file:
| `FR` | Frame | `page/{frame-evaluate, frame-frame-element, frame-goto, frame-hierarchy}` | 36 |
| `GE` | Geolocation | `library/geolocation` | 9 |
| `GN` | Locator and selector generator | `library/{locator-generator, selector-generator}` | 104 |
| `HA` | HAR | `library/{har, browsercontext-har, har-websocket}` | 112 |
| `HA` | HAR | `library/{har, browsercontext-har, har-websocket}` | 113 |
| `HE` | Headful | `library/headful` | 16 |
| `HT` | Hit target | `library/hit-target` | 22 |
| `IF` | Input files | `page/page-set-input-files` | 21 |
Expand Down Expand Up @@ -3157,6 +3157,7 @@ subsystem, add a row to the Subsystems table. Then update this file:
| `HA-110` | `library/har-websocket.spec.ts` | should still allow routeWebSocket to fully mock the connection when capturing HAR |
| `HA-111` | `library/har-websocket.spec.ts` | should still allow routeWebSocket to modify messages when capturing HAR |
| `HA-112` | `library/har-websocket.spec.ts` | should respect PLAYWRIGHT\_HAR\_NO\_WEBSOCKET\_FRAMES |
| `HA-113` | `library/har.spec.ts` | should store binary content with a textual mime type as base64 |

### HE — Headful

Expand Down
Loading