Best JavaScript code snippet using wpt
cookie-helper.sub.js
Source:cookie-helper.sub.js
...30 return origin + "/cookies/resources/redirectWithCORSHeaders.py?status=307&location=" + encodeURIComponent(url);31}32// Asserts that `document.cookie` contains or does not contain (according to33// the value of |present|) a cookie named |name| with a value of |value|.34function assert_dom_cookie(name, value, present) {35 var re = new RegExp("(?:^|; )" + name + "=" + value + "(?:$|;)");36 assert_equals(re.test(document.cookie), present, "`" + name + "=" + value + "` in `document.cookie`");37}38function assert_cookie(origin, obj, name, value, present) {39 assert_equals(obj[name], present ? value : undefined, "`" + name + "=" + value + "` in request to `" + origin + "`.");40}41// Remove the cookie named |name| from |origin|, then set it on |origin| anew.42// If |origin| matches `self.origin`, also assert (via `document.cookie`) that43// the cookie was correctly removed and reset.44function create_cookie(origin, name, value, extras) {45 alert("Create_cookie: " + origin + "/cookies/resources/drop.py?name=" + name);46 return credFetch(origin + "/cookies/resources/drop.py?name=" + name)47 .then(_ => {48 if (origin == self.origin)49 assert_dom_cookie(name, value, false);50 })51 .then(_ => {52 return credFetch(origin + "/cookies/resources/set.py?" + name + "=" + value + ";path=/;" + extras)53 .then(_ => {54 if (origin == self.origin)55 assert_dom_cookie(name, value, true);56 });57 });58}59//60// Prefix-specific test helpers61//62function set_prefixed_cookie_via_dom_test(options) {63 promise_test(t => {64 var name = options.prefix + "prefixtestcookie";65 erase_cookie_from_js(name);66 var value = "" + Math.random();67 document.cookie = name + "=" + value + ";" + options.params;68 assert_dom_cookie(name, value, options.shouldExistInDOM);69 return credFetch("/cookies/resources/list.py")70 .then(r => r.json())71 .then(cookies => assert_equals(cookies[name], options.shouldExistViaHTTP ? value : undefined));72 }, options.title);73}74function set_prefixed_cookie_via_http_test(options) {75 promise_test(t => {76 var postDelete = _ => {77 var value = "" + Math.random();78 return credFetch(options.origin + "/cookies/resources/set.py?" + name + "=" + value + ";" + options.params)79 .then(_ => credFetch(options.origin + "/cookies/resources/list.py"))80 .then(r => r.json())81 .then(cookies => assert_equals(cookies[name], options.shouldExistViaHTTP ? value : undefined));82 };83 var name = options.prefix + "prefixtestcookie";84 if (!options.origin) {85 options.origin = self.origin;86 erase_cookie_from_js(name);87 return postDelete;88 } else {89 return credFetch(options.origin + "/cookies/resources/drop.py?name=" + name)90 .then(_ => postDelete());91 }92 }, options.title);93}94//95// SameSite-specific test helpers:96//97window.SameSiteStatus = {98 CROSS_SITE: "cross-site",99 LAX: "lax",100 STRICT: "strict"101};102// Reset SameSite test cookies on |origin|. If |origin| matches `self.origin`, assert103// (via `document.cookie`) that they were properly removed and reset.104function resetSameSiteCookies(origin, value) {105 return credFetch(origin + "/cookies/resources/dropSameSite.py")106 .then(_ => {107 if (origin == self.origin) {108 assert_dom_cookie("samesite_strict", value, false);109 assert_dom_cookie("samesite_lax", value, false);110 assert_dom_cookie("samesite_none", value, false);111 }112 })113 .then(_ => {114 return credFetch(origin + "/cookies/resources/setSameSite.py?" + value)115 .then(_ => {116 if (origin == self.origin) {117 assert_dom_cookie("samesite_strict", value, true);118 assert_dom_cookie("samesite_lax", value, true);119 assert_dom_cookie("samesite_none", value, true);120 }121 })122 })123}124// Given an |expectedStatus| and |expectedValue|, assert the |cookies| contains the125// proper set of cookie names and values.126function verifySameSiteCookieState(expectedStatus, expectedValue, cookies) {127 assert_equals(cookies["samesite_none"], expectedValue, "Non-SameSite cookies are always sent.");128 if (expectedStatus == SameSiteStatus.CROSS_SITE) {129 assert_not_equals(cookies["samesite_strict"], expectedValue, "SameSite=Strict cookies are not sent with cross-site requests.");130 assert_not_equals(cookies["samesite_lax"], expectedValue, "SameSite=Lax cookies are not sent with cross-site requests.");131 } else if (expectedStatus == SameSiteStatus.LAX) {132 assert_not_equals(cookies["samesite_strict"], expectedValue, "SameSite=Strict cookies are not sent with lax requests.");133 assert_equals(cookies["samesite_lax"], expectedValue, "SameSite=Lax cookies are sent with lax requests.");134 } else if (expectedStatus == SameSiteStatus.STRICT) {135 assert_equals(cookies["samesite_strict"], expectedValue, "SameSite=Strict cookies are sent with strict requests.");136 assert_equals(cookies["samesite_lax"], expectedValue, "SameSite=Lax cookies are sent with strict requests.");137 }138}139//140// LeaveSecureCookiesAlone-specific test helpers:141//142window.SecureStatus = {143 INSECURE_COOKIE_ONLY: "1",144 BOTH_COOKIES: "2",145};146//Reset SameSite test cookies on |origin|. If |origin| matches `self.origin`, assert147//(via `document.cookie`) that they were properly removed and reset.148function resetSecureCookies(origin, value) {149return credFetch(origin + "/cookies/resources/dropSecure.py")150 .then(_ => {151 if (origin == self.origin) {152 assert_dom_cookie("alone_secure", value, false);153 assert_dom_cookie("alone_insecure", value, false);154 }155 })156 .then(_ => {157 return credFetch(origin + "/cookie/resources/setSecure.py?" + value)158 })159}160//161// DOM based cookie manipulation APIs162//163// erase cookie value and set for expiration164function erase_cookie_from_js(name) {165 let secure = self.location.protocol == "https:" ? "Secure" : "";166 document.cookie = `${name}=0; path=/; expires=${new Date(0).toUTCString()}; ${secure}`;167 var re = new RegExp("(?:^|; )" + name);...
Using AI Code Generation
1function assert_dom_cookie(test, cookie, name, value, path, domain, secure, httpOnly) {2 test.step(function() {3 assert_equals(cookie.name, name, "Cookie name");4 assert_equals(cookie.value, value, "Cookie value");5 assert_equals(cookie.path, path, "Cookie path");6 assert_equals(cookie.domain, domain, "Cookie domain");7 assert_equals(cookie.secure, secure, "Cookie secure");8 assert_equals(cookie.httpOnly, httpOnly, "Cookie httpOnly");9 });10}11promise_test(function(test) {12 return fetch("resources/cookie.py?name=foo&value=bar&path=/&domain=example.com&secure&httpOnly")13 .then(function(resp) {14 assert_equals(resp.status, 200, "Cookie should be set");15 return resp.text();16 }).then(function(text) {17 assert_equals(text, "PASS", "Cookie should be set");18 return fetch("resources/cookie.py?name=foo")19 .then(function(resp) {20 assert_equals(resp.status, 200, "Cookie should be accessible");21 return resp.text();22 }).then(function(text) {23 assert_equals(text, "bar", "Cookie value should be bar");24 return fetch("resources/cookie.py?name=foo&value=bar2&path=/&domain=example.com&secure&httpOnly")25 .then(function(resp) {26 assert_equals(resp.status, 200, "Cookie should be set");27 return resp.text();28 }).then(function(text) {29 assert_equals(text, "PASS", "Cookie should be set");30 return fetch("resources/cookie.py?name=foo")31 .then(function(resp) {32 assert_equals(resp.status, 200, "Cookie should be accessible");33 return resp.text();34 }).then(function(text) {35 assert_equals(text, "bar2", "Cookie value should be bar2");36 });37 });38 });39 });40}, "Set a cookie with the same name, value, path, domain, secure and httpOnly attributes");41Attachment #8949972 - Flags: feedback?(jgraham)
Using AI Code Generation
1function assert_dom_cookie(name, value, path, http_only, secure, same_site) {2 var cookie = document.cookie;3 var pos = cookie.indexOf(name + '=');4 assert_true(pos != -1, 'Cookie named ' + name + ' not found');5 var start = pos + name.length + 1;6 var end = cookie.indexOf(';', start);7 if (end == -1) {8 end = cookie.length;9 }10 var actual_value = cookie.substring(start, end);11 assert_equals(actual_value, value, 'Unexpected value for cookie ' + name);12 if (path !== undefined) {13 assert_true(cookie.indexOf('path=' + path) != -1, 'Unexpected path for cookie ' + name);14 }15 if (http_only !== undefined) {16 assert_true(cookie.indexOf('HttpOnly') != -1, 'Cookie ' + name + ' is not HttpOnly');17 }18 if (secure !== undefined) {19 assert_true(cookie.indexOf('Secure') != -1, 'Cookie ' + name + ' is not Secure');20 }21 if (same_site !== undefined) {22 assert_true(cookie.indexOf('SameSite=' + same_site) != -1, 'Unexpected SameSite for cookie ' + name);23 }24}25function assert_no_dom_cookie(name) {26 var cookie = document.cookie;27 var pos = cookie.indexOf(name + '=');28 assert_false(pos != -1, 'Cookie named ' + name + ' found');29}30function assert_dom_cookie_array(name, values) {31 var cookie = document.cookie;32 var pos = cookie.indexOf(name + '=');33 assert_true(pos != -1, 'Cookie named ' + name + ' not found');34 var start = pos + name.length + 1;35 var end = cookie.indexOf(';', start);36 if (end == -1) {37 end = cookie.length;38 }39 var actual_value = cookie.substring(start, end);40 var actual_values = actual_value.split(',');41 assert_array_equals(actual_values, values, 'Unexpected value for cookie ' + name);42}43function assert_dom_cookie_exists(name) {44 var cookie = document.cookie;45 var pos = cookie.indexOf(name + '=');46 assert_true(pos != -1, 'Cookie named ' + name + ' not found');47}48function assert_dom_cookie_not_exists(name) {49 var cookie = document.cookie;
Using AI Code Generation
1var cookieValue = "cookieValue";2document.cookie = "cookieName=" + cookieValue;3assert_dom_cookie("cookieName", cookieValue, "cookie is set with correct value");4assert_dom_cookie("cookieName", null, "cookie is not set");5var cookieValue = "cookieValue";6document.cookie = "cookieName=" + cookieValue;7assert_dom_cookie("cookieName", cookieValue, "cookie is set with correct value");8var cookieValue = "cookieValue";9document.cookie = "cookieName=" + cookieValue;10assert_dom_cookie("cookieName", cookieValue, "cookie is set with correct value");11var cookieValue = "cookieValue";12document.cookie = "cookieName=" + cookieValue;13assert_dom_cookie("cookieName", cookieValue, "cookie is set with correct value");14var cookieValue = "cookieValue";15document.cookie = "cookieName=" + cookieValue;16assert_dom_cookie("
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!