fix(fetch): restore from empty storage state (#27025)

Fixes #26833
This commit is contained in:
Yury Semikhatsky 2023-09-12 13:11:18 -07:00 committed by GitHub
parent 78309b9900
commit 700b310150
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 20 additions and 9 deletions

View file

@ -342,8 +342,8 @@ scheme.PlaywrightNewRequestParams = tObject({
})), })),
timeout: tOptional(tNumber), timeout: tOptional(tNumber),
storageState: tOptional(tObject({ storageState: tOptional(tObject({
cookies: tArray(tType('NetworkCookie')), cookies: tOptional(tArray(tType('NetworkCookie'))),
origins: tArray(tType('OriginStorage')), origins: tOptional(tArray(tType('OriginStorage'))),
})), })),
tracesDir: tOptional(tString), tracesDir: tOptional(tString),
}); });

View file

@ -523,7 +523,7 @@ export class GlobalAPIRequestContext extends APIRequestContext {
} }
if (options.storageState) { if (options.storageState) {
this._origins = options.storageState.origins; this._origins = options.storageState.origins;
this._cookieStore.addCookies(options.storageState.cookies); this._cookieStore.addCookies(options.storageState.cookies || []);
} }
this._options = { this._options = {
baseURL: options.baseURL, baseURL: options.baseURL,

View file

@ -587,8 +587,8 @@ export type PlaywrightNewRequestParams = {
}, },
timeout?: number, timeout?: number,
storageState?: { storageState?: {
cookies: NetworkCookie[], cookies?: NetworkCookie[],
origins: OriginStorage[], origins?: OriginStorage[],
}, },
tracesDir?: string, tracesDir?: string,
}; };
@ -610,8 +610,8 @@ export type PlaywrightNewRequestOptions = {
}, },
timeout?: number, timeout?: number,
storageState?: { storageState?: {
cookies: NetworkCookie[], cookies?: NetworkCookie[],
origins: OriginStorage[], origins?: OriginStorage[],
}, },
tracesDir?: string, tracesDir?: string,
}; };

View file

@ -683,10 +683,10 @@ Playwright:
type: object? type: object?
properties: properties:
cookies: cookies:
type: array type: array?
items: NetworkCookie items: NetworkCookie
origins: origins:
type: array type: array?
items: OriginStorage items: OriginStorage
tracesDir: string? tracesDir: string?

View file

@ -436,3 +436,14 @@ it('storage state should round-trip through file', async ({ playwright, server }
const state2 = await request2.storageState(); const state2 = await request2.storageState();
expect(state2).toEqual(storageState); expect(state2).toEqual(storageState);
}); });
it('should work with empty storage state', async ({ playwright, server }, testInfo) => {
const storageState = testInfo.outputPath('storage-state.json');
await fs.promises.writeFile(storageState, '{}');
const request1 = await playwright.request.newContext({ storageState });
const state1 = await request1.storageState();
expect(state1).toEqual({ cookies: [], origins: [] });
await expect(await request1.get(server.EMPTY_PAGE)).toBeOK();
await request1.dispose();
});