Best JavaScript code snippet using wpt
cookie-test.js
Source:cookie-test.js
1// getDefaultPathCookies is a helper method to get and delete cookies on the2// "default path" (which for these tests will be at `/cookies/resources`),3// determined by the path portion of the request-uri.4async function getDefaultPathCookies(path = '/cookies/resources') {5 return new Promise((resolve, reject) => {6 try {7 const iframe = document.createElement('iframe');8 iframe.style = 'display: none';9 iframe.src = `${path}/echo-cookie.html`;10 iframe.addEventListener('load', (e) => {11 const win = e.target.contentWindow;12 const iframeCookies = win.getCookies();13 win.expireCookie('test', path);14 resolve(iframeCookies);15 }, {once: true});16 document.documentElement.appendChild(iframe);17 } catch (e) {18 reject(e);19 }20 });21}22// getRedirectedCookies is a helper method to get and delete cookies that23// were set from a Location header redirect.24async function getRedirectedCookies(location, cookie) {25 return new Promise((resolve, reject) => {26 try {27 const iframe = document.createElement('iframe');28 iframe.style = 'display: none';29 iframe.src = location;30 iframe.addEventListener('load', (e) => {31 const win = e.target.contentWindow;32 let iframeCookie;33 // go ask for the cookie34 win.postMessage('getCookies', '*');35 // once we get it, send a message to delete on the other36 // side, then resolve the cookie back to httpRedirectCookieTest37 window.addEventListener('message', (e) => {38 if (typeof e.data == 'object' && 'cookies' in e.data) {39 iframeCookie = e.data.cookies;40 e.source.postMessage({'expireCookie': cookie}, '*');41 }42 // wait on the iframe to tell us it deleted the cookies before43 // resolving, to avoid any state race conditions.44 if (e.data == 'expired') {45 resolve(iframeCookie);46 }47 });48 }, {once: true});49 document.documentElement.appendChild(iframe);50 } catch (e) {51 reject(e);52 }53 });54}55// httpCookieTest sets a `cookie` (via HTTP), then asserts it was or was not set56// via `expectedValue` (via the DOM). Then cleans it up (via HTTP). Most tests57// do not set a Path attribute, so `defaultPath` defaults to true.58//59// `cookie` may be a single cookie string, or an array of cookie strings, where60// the order of the array items represents the order of the Set-Cookie headers61// sent by the server.62function httpCookieTest(cookie, expectedValue, name, defaultPath = true) {63 return promise_test(async (t) => {64 let encodedCookie = encodeURIComponent(JSON.stringify(cookie));65 await fetch(`/cookies/resources/cookie.py?set=${encodedCookie}`);66 let cookies = document.cookie;67 if (defaultPath) {68 // for the tests where a Path is set from the request-uri69 // path, we need to go look for cookies in an iframe at that70 // default path.71 cookies = await getDefaultPathCookies();72 }73 if (Boolean(expectedValue)) {74 assert_equals(cookies, expectedValue, 'The cookie was set as expected.');75 } else {76 assert_equals(cookies, expectedValue, 'The cookie was rejected.');77 }78 await fetch(`/cookies/resources/cookie.py?drop=${encodedCookie}`);79 }, name);80}81// This is a variation on httpCookieTest, where a redirect happens via82// the Location header and we check to see if cookies are sent via83// getRedirectedCookies84function httpRedirectCookieTest(cookie, expectedValue, name, location) {85 return promise_test(async (t) => {86 const encodedCookie = encodeURIComponent(JSON.stringify(cookie));87 const encodedLocation = encodeURIComponent(location);88 const setParams = `?set=${encodedCookie}&location=${encodedLocation}`;89 await fetch(`/cookies/resources/cookie.py${setParams}`);90 // for the tests where a redirect happens, we need to head91 // to that URI to get the cookies (and then delete them there)92 const cookies = await getRedirectedCookies(location, cookie);93 if (Boolean(expectedValue)) {94 assert_equals(cookies, expectedValue, 'The cookie was set as expected.');95 } else {96 assert_equals(cookies, expectedValue, 'The cookie was rejected.');97 }98 await fetch(`/cookies/resources/cookie.py?drop=${encodedCookie}`);99 }, name);100}101// Sets a `cookie` via the DOM, checks it against `expectedValue` via the DOM,102// then cleans it up via the DOM. This is needed in cases where going through103// HTTP headers may modify the cookie line (e.g. by stripping control104// characters).105//106// Note: this function has a dependency on testdriver.js. Any test files calling107// it should include testdriver.js and testdriver-vendor.js108function domCookieTest(cookie, expectedValue, name) {109 return promise_test(async (t) => {110 await test_driver.delete_all_cookies();111 t.add_cleanup(test_driver.delete_all_cookies);112 document.cookie = cookie;113 let cookies = document.cookie;114 assert_equals(cookies, expectedValue, Boolean(expectedValue) ?115 'The cookie was set as expected.' :116 'The cookie was rejected.');117 }, name);118}119// Returns an array of control characters along with their ASCII codes. Control120// characters are defined by RFC 5234 to be %x00-1F / %x7F.121function getCtlCharacters() {122 const ctlCodes = [...Array(0x20).keys()]123 .concat([0x7F]);124 return ctlCodes.map(i => ({ code: i, chr: String.fromCharCode(i) }))125}126// Returns a cookie string with name set to "t" * nameLength and value127// set to "1" * valueLength. Passing in 0 for either allows for creating128// a name- or value-less cookie.129//130// Note: Cookie length checking should ignore the "=".131function cookieStringWithNameAndValueLengths(nameLength, valueLength) {132 return `${"t".repeat(nameLength)}=${"1".repeat(valueLength)}`;...
Using AI Code Generation
1var wpt = require('webpagetest');2var test = wpt('www.webpagetest.org');3 videoParams: {4 },5}, function(err, data) {6 if (err) return console.error(err);7 test.ctlCodes(data.data.testId, function(err, data) {8 if (err) return console.error(err);9 console.log(data);10 });11});12### getLocations(callback)13var wpt = require('webpagetest');14var test = wpt('www.webpagetest.org');15test.getLocations(function(err, data) {16 if (err) return console.error(err);17 console.log(data);18});19### getLocations(callback)20var wpt = require('webpagetest');21var test = wpt('www.webpagetest.org');22test.getLocations(function(err, data) {23 if (err) return console.error(err);24 console.log(data);25});26### getTesters(callback)27var wpt = require('webpagetest');28var test = wpt('www.webpagetest.org');29test.getTesters(function(err, data) {30 if (err) return console.error(err);31 console.log(data);32});33### getTestStatus(testId, callback)34var wpt = require('webpagetest');35var test = wpt('www.web
Using AI Code Generation
1const Wpt = require('wpt-api');2const wpt = new Wpt('API_KEY');3wpt.ctlCodes((err, data) => {4 if (err) console.log(err);5 console.log(data);6});7const Wpt = require('wpt-api');8const wpt = new Wpt('API_KEY');9wpt.getLocations((err, data) => {10 if (err) console.log(err);11 console.log(data);12});13const Wpt = require('wpt-api');14const wpt = new Wpt('API_KEY');15wpt.getTesters((err, data) => {16 if (err) console.log(err);17 console.log(data);18});19const Wpt = require('wpt-api');20const wpt = new Wpt('API_KEY');21wpt.getTesters((err, data) => {22 if (err) console.log(err);23 console.log(data);24});25const Wpt = require('wpt-api');26const wpt = new Wpt('API_KEY');27wpt.getTest('test_id', (err, data) => {28 if (err) console.log(err);29 console.log(data);30});31const Wpt = require('wpt-api');32const wpt = new Wpt('API_KEY');33wpt.getTestResults('test_id', (err, data) => {34 if (err) console.log(err);35 console.log(data);36});37const Wpt = require('wpt-api');38const wpt = new Wpt('API_KEY');39wpt.getTestStatus('test_id', (err,
Using AI Code Generation
1var wpt = require('wpt');2var util = require('util');3var fs = require('fs');4var path = require('path');5var async = require('async');6var request = require('request');7var config = require('../config');8var wpt = new WebPageTest('www.webpagetest.org', config.wpt.apiKey);9var opts = {10};11 if (err) return console.log(err);12 var testId = data.data.testId;13 console.log('Test ID: ' + testId);14 wpt.getTestResults(testId, function(err, data) {15 if (err) return console.log(err);16 console.log('Test Results:');17 console.log(util.inspect(data.data, { depth: null }));18 });19});20var wpt = require('wpt');21var util = require('util');22var fs = require('fs');23var path = require('path');24var async = require('async');25var request = require('request');26var config = require('../config');27var wpt = new WebPageTest('www.webpagetest.org', config.wpt.apiKey);28var opts = {29};30 if (err) return console.log(err);31 var testId = data.data.testId;32 console.log('Test ID: ' + testId);33 wpt.getTestResults(testId,
Using AI Code Generation
1var wpt = require('wpt');2var ctl = new wpt.ctlCodes();3var test = ctl.ctlCodes('test');4console.log(test);5var ctl = function() {6 this.ctlCodes = function(str) {7 return str;8 }9}10module.exports = {11}12{13 "scripts": {14 },15 "dependencies": {16 }17}18var wpt = require('wpt');19var ctl = new wpt.ctlCodes();20var test = ctl.ctlCodes('test');21console.log(test);22var ctl = function() {23 this.ctlCodes = function(str) {24 return str;25 }26}27module.exports = {28}29{30 "scripts": {31 },32 "dependencies": {33 }34}35var wpt = require('wpt');36var ctl = new wpt.ctlCodes();37var test = ctl.ctlCodes('test');38console.log(test);39var ctl = function() {40 this.ctlCodes = function(str) {41 return str;42 }43}44module.exports = {45}46{
Using AI Code Generation
1var wpt = require('wpt');2var wpt = new WebPageTest('www.webpagetest.org', 'A.9b9f9f0eae0f1ab1c8c8d2d2c0c0c0c0');3 if (err) return console.error(err);4 console.log('Test status: ' + data.statusText);5 console.log('Test ID: ' + data.data.testId);6 console.log('Test results available at: ' + data.data.summaryCSV);7 console.log('View your test at: ' + data.data.userUrl);8});9var wpt = require('wpt');10var wpt = new WebPageTest('www.webpagetest.org', 'A.9b9f9f0eae0f1ab1c8c8d2d2c0c0c0c0');11 if (err) return console.error(err);12 console.log('Test status: ' + data.statusText);13 console.log('Test ID: ' + data.data.testId);14 console.log('Test results available at: ' + data.data.summaryCSV);15 console.log('View your test at: ' + data.data.userUrl);16});17var wpt = require('wpt');18var wpt = new WebPageTest('www.webpagetest.org', 'A.9b9f9f0eae0f1ab1c8c8d2d2c0c0c0c0');19 if (err) return console.error(err);20 console.log('Test status: ' + data
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!!