From 14e70b716775207804b4e40bd5b5c82dba6eacca Mon Sep 17 00:00:00 2001 From: Kazuaki Matsuo Date: Tue, 18 Apr 2023 08:54:33 -0700 Subject: [PATCH] refactor: getCookies and deleteCookies (#1538) * use only map * use B.all * remove todo * follow response values * Update web.js --- lib/commands/web.js | 30 +++++++++++++----------------- 1 file changed, 13 insertions(+), 17 deletions(-) diff --git a/lib/commands/web.js b/lib/commands/web.js index 6ee1fb505..96d3fa81e 100644 --- a/lib/commands/web.js +++ b/lib/commands/web.js @@ -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} @@ -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; @@ -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))); }, };