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),
storageState: tOptional(tObject({
cookies: tArray(tType('NetworkCookie')),
origins: tArray(tType('OriginStorage')),
cookies: tOptional(tArray(tType('NetworkCookie'))),
origins: tOptional(tArray(tType('OriginStorage'))),
})),
tracesDir: tOptional(tString),
});

View file

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

View file

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

View file

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

View file

@ -436,3 +436,14 @@ it('storage state should round-trip through file', async ({ playwright, server }
const state2 = await request2.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();
});