|
| 1 | +import { test, expect } from "@playwright/test"; |
| 2 | + |
| 3 | +import { |
| 4 | + createAppFixture, |
| 5 | + createFixture, |
| 6 | + js, |
| 7 | +} from "./helpers/create-fixture.js"; |
| 8 | +import type { AppFixture, Fixture } from "./helpers/create-fixture.js"; |
| 9 | +import { reactRouterConfig } from "./helpers/vite.js"; |
| 10 | +import { PlaywrightFixture } from "./helpers/playwright-fixture.js"; |
| 11 | + |
| 12 | +test.describe("CSub-Resource Integrity", () => { |
| 13 | + test.use({ javaScriptEnabled: false }); |
| 14 | + |
| 15 | + let fixture: Fixture; |
| 16 | + let appFixture: AppFixture; |
| 17 | + |
| 18 | + test.beforeAll(async () => { |
| 19 | + fixture = await createFixture({ |
| 20 | + files: { |
| 21 | + "react-router.config.ts": reactRouterConfig({ |
| 22 | + future: { unstable_subResourceIntegrity: true }, |
| 23 | + }), |
| 24 | + "app/root.tsx": js` |
| 25 | + import { Links, Meta, Outlet, Scripts } from "react-router"; |
| 26 | +
|
| 27 | + export default function Root() { |
| 28 | + return ( |
| 29 | + <html> |
| 30 | + <head> |
| 31 | + <Meta /> |
| 32 | + <Links /> |
| 33 | + </head> |
| 34 | + <body> |
| 35 | + <Outlet /> |
| 36 | + <Scripts nonce="test-nonce-123" /> |
| 37 | + </body> |
| 38 | + </html> |
| 39 | + ); |
| 40 | + } |
| 41 | + `, |
| 42 | + }, |
| 43 | + }); |
| 44 | + appFixture = await createAppFixture(fixture); |
| 45 | + }); |
| 46 | + |
| 47 | + test("includes an importmap", async ({ page }) => { |
| 48 | + let app = new PlaywrightFixture(appFixture, page); |
| 49 | + await app.goto("/"); |
| 50 | + let json = await page.locator('script[type="importmap"]').innerText(); |
| 51 | + let importMap = JSON.parse(json); |
| 52 | + expect(Object.keys(importMap.integrity).length).toBeGreaterThan(0); |
| 53 | + for (let key in importMap.integrity) { |
| 54 | + if (key.includes("manifest")) continue; |
| 55 | + let value = importMap.integrity[key]; |
| 56 | + expect(value).toMatch(/^sha384-/); |
| 57 | + |
| 58 | + let linkEl = page.locator(`link[rel="modulepreload"][href="${key}"]`); |
| 59 | + expect(await linkEl.getAttribute("href")).toBe(key); |
| 60 | + expect(await linkEl.getAttribute("integrity")).toBe(value); |
| 61 | + |
| 62 | + let scriptEl = page.locator(`script[type="module"]`); |
| 63 | + expect(await scriptEl.innerText()).toContain(key); |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + test("includes a nonce on the importmap script", async ({ page }) => { |
| 68 | + let app = new PlaywrightFixture(appFixture, page); |
| 69 | + await app.goto("/"); |
| 70 | + expect( |
| 71 | + await page.locator('script[type="importmap"]').getAttribute("nonce"), |
| 72 | + ).toBe("test-nonce-123"); |
| 73 | + }); |
| 74 | +}); |
0 commit comments