Skip to content

Commit

Permalink
Don't rely on a 3rd party url (or internet access) in the new cross-o…
Browse files Browse the repository at this point in the history
…rigin test
  • Loading branch information
eoghanmurray committed May 23, 2024
1 parent f8cbf9c commit d2df9bb
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ exports[`integration tests [html file]: picture.html 1`] = `
<img src=\\"http://localhost:3030/assets/img/characters/robot.png\\" />
</picture>
<img src=\\"http://localhost:3030/images/robot.png\\" alt=\\"This is a robot\\" />
<img src=\\"https://avatars.githubusercontent.com/u/43396833?s=20&amp;v=4\\" alt=\\"CORS restricted but has access-control-allow-origin: *\\" />
</body></html>"
`;

Expand Down
1 change: 0 additions & 1 deletion packages/rrweb-snapshot/test/html/picture.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@
<img src="assets/img/characters/robot.png" />
</picture>
<img src="/images/robot.png" alt="This is a robot" />
<img src="https://avatars.githubusercontent.com/u/43396833?s=20&v=4" alt="CORS restricted but has access-control-allow-origin: *" />
</body>
</html>
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 37 additions & 4 deletions packages/rrweb-snapshot/test/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import * as puppeteer from 'puppeteer';
import * as rollup from 'rollup';
import * as typescript from 'rollup-plugin-typescript2';
import * as assert from 'assert';
import { waitForRAF } from './utils';
import { waitForRAF, getServerURL } from './utils';
import { setTimeout } from 'node:timers/promises';

const _typescript = typescript as unknown as () => rollup.Plugin;
Expand Down Expand Up @@ -210,7 +210,7 @@ iframe.contentDocument.querySelector('center').clientHeight
inlineImages: true,
inlineStylesheet: false
})`);
await waitForRAF(page); // need a small wait, as after the crossOrigin="anonymous" change, the snapshot triggers a reload of the image (which mutates the snapshot when loaded)
// don't wait, as we want to ensure that the same-origin image can be inlined immediately
const bodyChildren = (await page.evaluate(`
snapshot.childNodes[0].childNodes[1].childNodes.filter((cn) => cn.type === 2);
`)) as any[];
Expand All @@ -224,11 +224,44 @@ iframe.contentDocument.querySelector('center').clientHeight
},
}),
);
expect(bodyChildren[2]).toEqual(
});

it('correctly saves cross-origin images offline', async () => {
const page: puppeteer.Page = await browser.newPage();

await page.goto('about:blank', {
waitUntil: 'load',
});
await page.setContent(
`
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<img src="${getServerURL(
server,
)}/images/rrweb-favicon-20x20.png" alt="CORS restricted but has access-control-allow-origin: *" />
</body>
</html>
`,
{
waitUntil: 'load',
},
);

await page.waitForSelector('img', { timeout: 1000 });
await page.evaluate(`${code}var snapshot = rrweb.snapshot(document, {
dataURLOptions: { type: "image/webp", quality: 0.8 },
inlineImages: true,
inlineStylesheet: false
})`);
await waitForRAF(page); // need a small wait, as after the crossOrigin="anonymous" change, the snapshot triggers a reload of the image (after which, the snapshot is mutated)
const bodyChildren = (await page.evaluate(`
snapshot.childNodes[0].childNodes[1].childNodes.filter((cn) => cn.type === 2);
`)) as any[];
expect(bodyChildren[0]).toEqual(
expect.objectContaining({
tagName: 'img',
attributes: {
src: 'https://avatars.githubusercontent.com/u/43396833?s=20&v=4',
src: getServerURL(server) + '/images/rrweb-favicon-20x20.png',
alt: 'CORS restricted but has access-control-allow-origin: *',
rr_dataURL: expect.stringMatching(/^data:image\/webp;base64,/),
},
Expand Down
10 changes: 10 additions & 0 deletions packages/rrweb-snapshot/test/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as puppeteer from 'puppeteer';
import * as http from 'http';

export async function waitForRAF(page: puppeteer.Page) {
return await page.evaluate(() => {
Expand All @@ -9,3 +10,12 @@ export async function waitForRAF(page: puppeteer.Page) {
});
});
}

export function getServerURL(server: http.Server): string {
const address = server.address();
if (address && typeof address !== 'string') {
return `http://localhost:${address.port}`;
} else {
return `${address}`;
}
}

0 comments on commit d2df9bb

Please sign in to comment.