Skip to content

Commit

Permalink
extend test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
sadym-chromium committed Mar 4, 2024
1 parent 6a0fe8e commit 6b46913
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 11 deletions.
22 changes: 20 additions & 2 deletions packages/puppeteer-core/src/bidi/Page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -571,8 +571,26 @@ export class BidiPage extends Page {
}
}

override deleteCookie(...cookies: DeleteCookiesRequest[]): Promise<void> {
return this.#browserContext.userContext.deleteCookie(...cookies);
override async deleteCookie(...cookies: DeleteCookiesRequest[]): Promise<void> {
await Promise.all(cookies.map(async deleteCookieRequest => {
const cookieUrl = deleteCookieRequest.url ?? this.url();
const normalizedUrl = URL.canParse(cookieUrl)
? new URL(cookieUrl)
: undefined;

const domain = deleteCookieRequest.domain ?? normalizedUrl?.hostname;
assert(
domain !== undefined,
`At least one of the url and domain needs to be specified`
);

const filter = {
domain: domain,
name: deleteCookieRequest.name,
...(deleteCookieRequest.path !== undefined ? { path: deleteCookieRequest.path } : {})
}
await this.#browserContext.userContext.deleteCookie(filter);
}));
}

override async removeExposedFunction(name: string): Promise<void> {
Expand Down
16 changes: 16 additions & 0 deletions packages/puppeteer-core/src/bidi/core/BrowsingContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,20 @@ export class BrowsingContext extends EventEmitter<{
this.#disposables.dispose();
super[disposeSymbol]();
}

@throwIfDisposed<BrowsingContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async deleteCookie(...cookieFilters: Bidi.Storage.CookieFilter[]): Promise<void> {
await Promise.all(cookieFilters.map(async filter => {
await this.#session.send('storage.deleteCookies', {
filter: filter,
partition: {
type: 'context',
context: this.id,
},
});
}));
}
}
8 changes: 2 additions & 6 deletions packages/puppeteer-core/src/bidi/core/UserContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,8 @@ export class UserContext extends EventEmitter<{
// SAFETY: Disposal implies this exists.
return context.#reason!;
})
async deleteCookie(...cookies: DeleteCookiesRequest[]): Promise<void> {
await Promise.all(cookies.map(async deleteCookieRequest => {
const filter = {
...deleteCookieRequest
};
async deleteCookie(...cookieFilters: Bidi.Storage.CookieFilter[]): Promise<void> {
await Promise.all(cookieFilters.map(async filter => {
await this.#session.send('storage.deleteCookies', {
filter: filter,
partition: {
Expand All @@ -216,7 +213,6 @@ export class UserContext extends EventEmitter<{
}));
}


@throwIfDisposed<UserContext>(context => {
// SAFETY: Disposal implies this exists.
return context.#reason!;
Expand Down
141 changes: 138 additions & 3 deletions test/src/cookies.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -557,8 +557,8 @@ describe('Cookie specs', () => {
});
});

describe('Page.deleteCookie', function () {
it('should work', async () => {
describe.only('Page.deleteCookie', function () {
it('should delete cookie', async () => {
const {page, server} = await getTestState();

await page.goto(server.EMPTY_PAGE);
Expand All @@ -579,10 +579,145 @@ describe('Cookie specs', () => {
expect(await page.evaluate('document.cookie')).toBe(
'cookie1=1; cookie2=2; cookie3=3'
);
await page.deleteCookie({name: 'cookie2'});
await page.deleteCookie({ name: 'cookie2' });
expect(await page.evaluate('document.cookie')).toBe(
'cookie1=1; cookie3=3'
);
});
it('should not delete cookie for different domain', async () => {
const { page, server } = await getTestState();
const COOKIE_DESTINATION_URL = 'https://example.com';
const COOKIE_NAME = 'some_cookie_name';

await page.goto(server.EMPTY_PAGE);
// Set a cookie for the current page.
await page.setCookie({
name: COOKIE_NAME,
value: 'local page cookie value',
});
expect(await page.cookies()).toHaveLength(1);

// Set a cookie for different domain.
await page.setCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
value: 'COOKIE_DESTINATION_URL cookie value',
});
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(1);

await page.deleteCookie({ name: COOKIE_NAME });

// Verify the cookie is deleted for the current page.
expect(await page.cookies()).toHaveLength(0);

// Verify the cookie is not deleted for different domain.
await expectCookieEquals(await page.cookies(COOKIE_DESTINATION_URL), [
{
name: COOKIE_NAME,
value: 'COOKIE_DESTINATION_URL cookie value',
domain: 'example.com',
path: '/',
sameParty: false,
expires: -1,
size: 51,
httpOnly: false,
secure: true,
session: true,
sourceScheme: 'Secure',
}]
);
});
it('should delete cookie for specified URL', async () => {
const { page, server } = await getTestState();
const COOKIE_DESTINATION_URL = 'https://example.com';
const COOKIE_NAME = 'some_cookie_name';

await page.goto(server.EMPTY_PAGE);
// Set a cookie for the current page.
await page.setCookie({
name: COOKIE_NAME,
value: 'some_cookie_value',
});
expect(await page.cookies()).toHaveLength(1);

// Set a cookie for specified URL.
await page.setCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
value: 'another_cookie_value',
});
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(1);

// Delete the cookie for specified URL.
await page.deleteCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
});

// Verify the cookie is deleted for specified URL.
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(0);

// Verify the cookie is not deleted for the current page.
await expectCookieEquals(await page.cookies(), [
{
name: COOKIE_NAME,
value: 'some_cookie_value',
domain: 'localhost',
path: '/',
sameParty: false,
expires: -1,
size: 33,
httpOnly: false,
secure: false,
session: true,
sourceScheme: 'NonSecure',
}]
);

});
it('should delete cookie for specified URL regardless of the current page', async () => {
// Tentative test case. Depending on the cookie partitioning implementation, this
// test case may not pass.
// This test verifies the page.deleteCookie method deletes cookies for the custom
// destination URL, even if it was set from another page.

const { page, server } = await getTestState();
const COOKIE_DESTINATION_URL = 'https://example.com';
const COOKIE_NAME = 'some_cookie_name';
const URL_1 = server.EMPTY_PAGE;
const URL_2 = server.CROSS_PROCESS_PREFIX + '/empty.html';

await page.goto(URL_1);
// Set a cookie for the COOKIE_DESTINATION from URL_1.
await page.setCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
value: 'Cookie from URL_1',
});
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(1);

await page.goto(URL_2);
// Set a cookie for the COOKIE_DESTINATION from URL_2.
await page.setCookie({
url: COOKIE_DESTINATION_URL,
name: COOKIE_NAME,
value: 'Cookie from URL_2',
});
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(1);

// Delete the cookie for the COOKIE_DESTINATION from URL_2.
await page.deleteCookie({
name: COOKIE_NAME,
url: COOKIE_DESTINATION_URL
});

// Expect the cookie for the COOKIE_DESTINATION from URL_2 is deleted.
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(0);

// Navigate back to the URL_1.
await page.goto(server.EMPTY_PAGE);
// Expect the cookie for the COOKIE_DESTINATION from URL_1 is deleted.
expect(await page.cookies(COOKIE_DESTINATION_URL)).toHaveLength(0);
});
});
});

0 comments on commit 6b46913

Please sign in to comment.