diff --git a/.github/workflows/playwright-tests.yml b/.github/workflows/playwright-tests.yml index 19f515351..e3ae1404f 100644 --- a/.github/workflows/playwright-tests.yml +++ b/.github/workflows/playwright-tests.yml @@ -44,6 +44,8 @@ jobs: run: pnpm --filter @sam-monorepo/playwright test env: BASE_URL: ${{ vars.BASE_URL }} + PLAYWRIGHT_CUSTOM_HEADER_NAME: ${{ vars.PLAYWRIGHT_CUSTOM_HEADER_NAME }} + PLAYWRIGHT_CUSTOM_HEADER_VALUE: ${{ secrets.PLAYWRIGHT_CUSTOM_HEADER_VALUE }} - name: Upload test report uses: actions/upload-artifact@v4.6.2 diff --git a/pnpm-monorepo/apps/playwright/.env.example b/pnpm-monorepo/apps/playwright/.env.example index 717a4c762..4ea66a323 100644 --- a/pnpm-monorepo/apps/playwright/.env.example +++ b/pnpm-monorepo/apps/playwright/.env.example @@ -1 +1,3 @@ BASE_URL= +PLAYWRIGHT_CUSTOM_HEADER_NAME= +PLAYWRIGHT_CUSTOM_HEADER_VALUE= diff --git a/pnpm-monorepo/apps/playwright/playwright.config.ts b/pnpm-monorepo/apps/playwright/playwright.config.ts index 20cb39f7f..df64f1e77 100644 --- a/pnpm-monorepo/apps/playwright/playwright.config.ts +++ b/pnpm-monorepo/apps/playwright/playwright.config.ts @@ -1,6 +1,19 @@ import { defineConfig } from "@playwright/test"; import "dotenv/config"; +// Build custom HTTP headers object if both name and value are provided +const extraHTTPHeaders: Record = {}; +const headerName = process.env.PLAYWRIGHT_CUSTOM_HEADER_NAME; +const headerValue = process.env.PLAYWRIGHT_CUSTOM_HEADER_VALUE; + +if (headerName && headerValue) { + // Basic validation: HTTP header names should be alphanumeric with hyphens/underscores + if (!/^[a-zA-Z0-9_-]+$/.test(headerName)) { + throw new Error(`Invalid HTTP header name: ${headerName}. Header names must contain only alphanumeric characters, hyphens, and underscores.`); + } + extraHTTPHeaders[headerName] = headerValue; +} + export default defineConfig({ testDir: "./tests", fullyParallel: true, @@ -11,5 +24,6 @@ export default defineConfig({ use: { baseURL: process.env.BASE_URL, trace: "on-first-retry", + ...(Object.keys(extraHTTPHeaders).length > 0 && { extraHTTPHeaders }), }, });