Skip to content

Instantly share code, notes, and snippets.

@idubrov

idubrov/login.js Secret

Last active December 31, 2020 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save idubrov/d8b06800bed48ebb41465a219a7908a1 to your computer and use it in GitHub Desktop.
Save idubrov/d8b06800bed48ebb41465a219a7908a1 to your computer and use it in GitHub Desktop.
cy.task("GoogleSocialLogin", socialLoginOptions).then(({ cookies }) => {
const googleCookies = cookies.filter(
(cookie) => cookie.domain === ".google.com" || cookie.domain === "accounts.google.com" || cookie.domain === ".youtube.com"
);
cy.intercept(
{
url: /^http:\/\/localhost:3002\/cypress-cookies\//,
},
(req) => {
const pos = req.url.lastIndexOf("/");
const index = req.url.substring(pos + 1);
const domain = googleCookies[index].domain;
const url = domain.startsWith(".") ? domain.substring(1) : domain;
req.reply(200, `<!DOCTYPE html><html><script>window.location = 'https://cypress-cookies.${domain}/${index}';</script></html>`, {
"Content-Type": "text/html",
});
}
);
cy.intercept(
{
url: /^https:\/\/cypress-cookies\.*/,
},
(req) => {
const pos = req.url.lastIndexOf("/");
const index = parseInt(req.url.substring(pos + 1), 10);
req.reply(200, "<!DOCTYPE html><html><script>window.location = 'http://localhost:3002/cypress-cookies-end/';</script></html>", {
"Content-Type": "text/html",
"Set-Cookie": serializeCookie(googleCookies[index]),
});
}
);
cy.intercept(
{
url: /^http:\/\/localhost:3002\/cypress-cookies-end/,
},
(req) => {
req.reply(200, "<!DOCTYPE html><html></html>", {
"Content-Type": "text/html",
})
}
);
// Set all the cookies, one by one.
// FIXME: is there a way to set multiple cookies at once? Cypress only allows setting one header at a time.
for (let index = 0; index < googleCookies.length; index += 1) {
cy.visit(`http://localhost:3002/cypress-cookies/${index}`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment