browser_storage_cookies-duplicate-names.js (3973B)
1 /* Any copyright is dedicated to the Public Domain. 2 http://creativecommons.org/publicdomain/zero/1.0/ */ 3 4 "use strict"; 5 6 // Test that the storage panel is able to display multiple cookies with the same 7 // name (and different paths). 8 9 Services.scriptloader.loadSubScript( 10 "chrome://mochitests/content/browser/devtools/server/tests/browser/storage-helpers.js", 11 this 12 ); 13 14 const l10n = new Localization(["devtools/client/storage.ftl"], true); 15 const sessionString = l10n.formatValueSync("storage-expires-session"); 16 17 const TESTDATA = { 18 "http://test1.example.org": [ 19 { 20 name: "name", 21 value: "value1", 22 expires: 0, 23 path: "/", 24 host: "test1.example.org", 25 hostOnly: true, 26 isSecure: false, 27 }, 28 { 29 name: "name", 30 value: "value2", 31 expires: 0, 32 path: "/path2/", 33 host: "test1.example.org", 34 hostOnly: true, 35 isSecure: false, 36 }, 37 { 38 name: "name", 39 value: "value3", 40 expires: 0, 41 path: "/path3/", 42 host: "test1.example.org", 43 hostOnly: true, 44 isSecure: false, 45 }, 46 ], 47 }; 48 49 add_task(async function () { 50 const { commands } = await openTabAndSetupStorage( 51 MAIN_DOMAIN + "storage-cookies-same-name.html" 52 ); 53 54 const { resourceCommand } = commands; 55 const { TYPES } = resourceCommand; 56 const data = {}; 57 await resourceCommand.watchResources( 58 [TYPES.COOKIE, TYPES.LOCAL_STORAGE, TYPES.SESSION_STORAGE], 59 { 60 async onAvailable(resources) { 61 for (const resource of resources) { 62 const { resourceType } = resource; 63 if (!data[resourceType]) { 64 data[resourceType] = { hosts: {}, dataByHost: {} }; 65 } 66 67 for (const host in resource.hosts) { 68 if (!data[resourceType].hosts[host]) { 69 data[resourceType].hosts[host] = []; 70 } 71 // For indexed DB, we have some values, the database names. Other are empty arrays. 72 const hostValues = resource.hosts[host]; 73 data[resourceType].hosts[host].push(...hostValues); 74 data[resourceType].dataByHost[host] = 75 await resource.getStoreObjects(host, null, { sessionString }); 76 } 77 } 78 }, 79 } 80 ); 81 82 ok(data.cookies, "Cookies storage actor is present"); 83 84 await testCookies(data.cookies); 85 await clearStorage(); 86 87 // Forcing GC/CC to get rid of docshells and windows created by this test. 88 forceCollections(); 89 await commands.destroy(); 90 forceCollections(); 91 }); 92 93 function testCookies({ hosts, dataByHost }) { 94 const numHosts = Object.keys(hosts).length; 95 is(numHosts, 1, "Correct number of host entries for cookies"); 96 return testCookiesObjects(0, hosts, dataByHost); 97 } 98 99 var testCookiesObjects = async function (index, hosts, dataByHost) { 100 const host = Object.keys(hosts)[index]; 101 const data = dataByHost[host]; 102 is( 103 data.total, 104 TESTDATA[host].length, 105 "Number of cookies in host " + host + " matches" 106 ); 107 for (const item of data.data) { 108 let found = false; 109 for (const toMatch of TESTDATA[host]) { 110 if ( 111 item.name === toMatch.name && 112 item.host === toMatch.host && 113 item.path === toMatch.path 114 ) { 115 found = true; 116 ok(true, "Found cookie " + item.name + " in response"); 117 is(item.value.str, toMatch.value, "The value matches."); 118 is(item.expires, toMatch.expires, "The expiry time matches."); 119 is(item.path, toMatch.path, "The path matches."); 120 is(item.host, toMatch.host, "The host matches."); 121 is(item.isSecure, toMatch.isSecure, "The isSecure value matches."); 122 is(item.hostOnly, toMatch.hostOnly, "The hostOnly value matches."); 123 break; 124 } 125 } 126 ok(found, "cookie " + item.name + " should exist in response"); 127 } 128 129 ok(!!TESTDATA[host], "Host is present in the list : " + host); 130 if (index == Object.keys(hosts).length - 1) { 131 return; 132 } 133 await testCookiesObjects(++index, hosts, dataByHost); 134 };