Skip to content

Commit

Permalink
refactor: getCookies and deleteCookies (#1538)
Browse files Browse the repository at this point in the history
* use only map

* use B.all

* remove todo

* follow response values

* Update web.js
  • Loading branch information
KazuCocoa authored Apr 18, 2023
1 parent bbfb95c commit 14e70b7
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions lib/commands/web.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,23 +209,21 @@ const commands = {
}

// get the cookies from the remote debugger, or an empty object
const cookies = await this.remote.getCookies() || {cookies: []};
const { cookies } = await this.remote.getCookies();

// the value is URI encoded, so decode it safely
const decodedCookieValues = cookies.cookies.map((cookie) => {
try {
return decodeURI(cookie.value);
} catch (error) {
this.log.debug(`Cookie ${cookie.name} was not decoded successfully. Cookie value: ${cookie.value}`);
this.log.warn(error);
return undefined;
return cookies.map((cookie) => {
if (!_.isEmpty(cookie.value)) {
try {
cookie.value = decodeURI(cookie.value);
} catch (error) {
this.log.debug(`Cookie ${cookie.name} was not decoded successfully. Cookie value: ${cookie.value}`);
this.log.warn(error);
// Keep the original value
}
}
return cookie;
});

// zip cookies with decoded value, removing undefined cookie values
return _.zip(cookies.cookies, decodedCookieValues)
.filter(([, value]) => !_.isUndefined(value))
.map(([cookie, value]) => Object.assign({}, cookie, {value}));
},
/**
* @this {XCUITestDriver}
Expand Down Expand Up @@ -265,7 +263,7 @@ const commands = {
}

const cookies = await this.getCookies();
const cookie = cookies.find((cookie) => cookie.name === cookieName);
const cookie = cookies.find(({name}) => name === cookieName);
if (!cookie) {
this.log.debug(`Cookie '${cookieName}' not found. Ignoring.`);
return;
Expand All @@ -283,9 +281,7 @@ const commands = {
}

const cookies = await this.getCookies();
for (const cookie of cookies) {
await this._deleteCookie(cookie);
}
await B.all(cookies.map((cookie) => this._deleteCookie(cookie)));
},
};

Expand Down

0 comments on commit 14e70b7

Please # to comment.