Skip to content
Closed
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
13 changes: 13 additions & 0 deletions e2e/infra/api/apiRequests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,24 @@ const csrfCache = new WeakMap<APIRequestContext, string>();
/**
* Seed the CSRF cookie on the given request context by making a lightweight
* GET (only on the first call), then return the cached token value.
*
* When the context already carries the csrf_token cookie (e.g. loaded from
* storageState), the server won't emit a new Set-Cookie header. In that
* case we read the token directly from the context's stored cookies.
*/
async function getCsrfToken(baseUrl: string, ctx: APIRequestContext): Promise<string | undefined> {
const cached = csrfCache.get(ctx);
if (cached) return cached;

// Check if the csrf_token cookie already exists in the context (from storageState)
const state = await ctx.storageState();
const existingCookie = state.cookies.find(c => c.name === 'csrf_token');
if (existingCookie) {
csrfCache.set(ctx, existingCookie.value);
return existingCookie.value;
}

// No existing cookie — seed it with a lightweight GET request
const seedResp = await ctx.get(`${baseUrl}/auth-status`);
const setCookies = seedResp.headersArray()
.filter(h => h.name.toLowerCase() === 'set-cookie')
Expand Down