dify/e2e/support/api/console-context.ts
yyh 05a25943e7
fix(e2e): align fixtures with generated contracts (#39394)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-07-22 05:09:23 +00:00

35 lines
1.1 KiB
TypeScript

import type { APIResponse } from '@playwright/test'
import { readFile } from 'node:fs/promises'
import { request } from '@playwright/test'
import * as z from 'zod'
import { authStatePath } from '../../fixtures/auth'
import { apiURL } from '../../test-env'
const zStorageState = z.object({
cookies: z.array(
z.object({
name: z.string(),
value: z.string(),
}),
),
})
export async function createConsoleApiContext() {
const state = zStorageState.parse(JSON.parse(await readFile(authStatePath, 'utf8')))
const csrfToken = state.cookies.find((cookie) => cookie.name.endsWith('csrf_token'))?.value
if (!csrfToken) throw new Error(`No CSRF token found in E2E auth state: ${authStatePath}`)
return request.newContext({
baseURL: apiURL,
extraHTTPHeaders: { 'X-CSRF-Token': csrfToken },
storageState: authStatePath,
})
}
export async function expectApiResponseOK(response: APIResponse, action: string): Promise<void> {
if (response.ok()) return
const body = await response.text().catch(() => '')
throw new Error(`${action} failed with ${response.status()} ${response.statusText()}: ${body}`)
}