extensions/cookie/test/file_testloadflags.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

michael@0 1 var gExpectedCookies;
michael@0 2 var gExpectedHeaders;
michael@0 3 var gExpectedLoads;
michael@0 4
michael@0 5 var gObs;
michael@0 6 var gPopup;
michael@0 7
michael@0 8 var gHeaders = 0;
michael@0 9 var gLoads = 0;
michael@0 10
michael@0 11 // setupTest() is run from 'onload='.
michael@0 12 function setupTest(uri, domain, cookies, loads, headers) {
michael@0 13 ok(true, "setupTest uri: " + uri + " domain: " + domain + " cookies: " + cookies +
michael@0 14 " loads: " + loads + " headers: " + headers);
michael@0 15
michael@0 16 SimpleTest.waitForExplicitFinish();
michael@0 17
michael@0 18 SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
michael@0 19 .getService(SpecialPowers.Ci.nsIPrefBranch)
michael@0 20 .setIntPref("network.cookie.cookieBehavior", 1);
michael@0 21
michael@0 22 var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"]
michael@0 23 .getService(SpecialPowers.Ci.nsICookieManager2);
michael@0 24
michael@0 25 ok(true, "we are going to remove these cookies");
michael@0 26 var count = 0;
michael@0 27 var list = cs.enumerator;
michael@0 28 while (list.hasMoreElements()) {
michael@0 29 var cookie = list.getNext().QueryInterface(SpecialPowers.Ci.nsICookie);
michael@0 30 ok(true, "cookie: " + cookie);
michael@0 31 ok(true, "cookie host " + cookie.host + " path " + cookie.path + " name " + cookie.name +
michael@0 32 " value " + cookie.value + " isSecure " + cookie.isSecure + " expires " + cookie.expires);
michael@0 33 ++count;
michael@0 34 }
michael@0 35 ok(true, count + " cookies");
michael@0 36
michael@0 37 cs.removeAll();
michael@0 38 cs.add(domain, "", "oh", "hai", false, false, true, Math.pow(2, 62));
michael@0 39 is(cs.countCookiesFromHost(domain), 1, "number of cookies for domain " + domain);
michael@0 40
michael@0 41 gExpectedCookies = cookies;
michael@0 42 gExpectedLoads = loads;
michael@0 43 gExpectedHeaders = headers;
michael@0 44
michael@0 45 gObs = new obs();
michael@0 46 // Listen for MessageEvents.
michael@0 47 window.addEventListener("message", messageReceiver, false);
michael@0 48
michael@0 49 // load a window which contains an iframe; each will attempt to set
michael@0 50 // cookies from their respective domains.
michael@0 51 gPopup = window.open(uri, 'hai', 'width=100,height=100');
michael@0 52 }
michael@0 53
michael@0 54 function finishTest()
michael@0 55 {
michael@0 56 gObs.remove();
michael@0 57
michael@0 58 SpecialPowers.Cc["@mozilla.org/preferences-service;1"]
michael@0 59 .getService(SpecialPowers.Ci.nsIPrefBranch)
michael@0 60 .clearUserPref("network.cookie.cookieBehavior");
michael@0 61
michael@0 62 SimpleTest.finish();
michael@0 63 }
michael@0 64
michael@0 65 // Count headers.
michael@0 66 function obs () {
michael@0 67 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
michael@0 68
michael@0 69 ok(true, "adding observer");
michael@0 70
michael@0 71 this.window = window;
michael@0 72 this.os = SpecialPowers.Cc["@mozilla.org/observer-service;1"]
michael@0 73 .getService(SpecialPowers.Ci.nsIObserverService);
michael@0 74 this.os.addObserver(this, "http-on-modify-request", false);
michael@0 75 }
michael@0 76
michael@0 77 obs.prototype = {
michael@0 78 observe: function obs_observe (theSubject, theTopic, theData)
michael@0 79 {
michael@0 80 ok(true, "theSubject " + theSubject);
michael@0 81 ok(true, "theTopic " + theTopic);
michael@0 82 ok(true, "theData " + theData);
michael@0 83
michael@0 84 var channel = theSubject.QueryInterface(
michael@0 85 this.window.SpecialPowers.Ci.nsIHttpChannel);
michael@0 86 ok(true, "channel " + channel);
michael@0 87 try {
michael@0 88 ok(true, "channel.URI " + channel.URI);
michael@0 89 ok(true, "channel.URI.spec " + channel.URI.spec);
michael@0 90 channel.visitRequestHeaders({
michael@0 91 visitHeader: function(aHeader, aValue) {
michael@0 92 ok(true, aHeader + ": " + aValue);
michael@0 93 }});
michael@0 94 } catch (err) {
michael@0 95 ok(false, "catch error " + err);
michael@0 96 }
michael@0 97
michael@0 98 // Ignore notifications we don't care about (like favicons)
michael@0 99 if (channel.URI.spec.indexOf(
michael@0 100 "http://example.org/tests/extensions/cookie/test/") == -1) {
michael@0 101 ok(true, "ignoring this one");
michael@0 102 return;
michael@0 103 }
michael@0 104
michael@0 105 this.window.isnot(channel.getRequestHeader("Cookie").indexOf("oh=hai"), -1,
michael@0 106 "cookie 'oh=hai' is in header for " + channel.URI.spec);
michael@0 107 ++gHeaders;
michael@0 108 },
michael@0 109
michael@0 110 remove: function obs_remove()
michael@0 111 {
michael@0 112 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
michael@0 113
michael@0 114 ok(true, "removing observer");
michael@0 115
michael@0 116 this.os.removeObserver(this, "http-on-modify-request");
michael@0 117 this.os = null;
michael@0 118 this.window = null;
michael@0 119 }
michael@0 120 }
michael@0 121
michael@0 122 /** Receives MessageEvents to this window. */
michael@0 123 // Count and check loads.
michael@0 124 function messageReceiver(evt)
michael@0 125 {
michael@0 126 ok(evt.data == "f_lf_i msg data img" || evt.data == "f_lf_i msg data page",
michael@0 127 "message data received from popup");
michael@0 128 if (evt.data == "f_lf_i msg data img") {
michael@0 129 ok(true, "message data received from popup for image");
michael@0 130 }
michael@0 131 if (evt.data == "f_lf_i msg data page") {
michael@0 132 ok(true, "message data received from popup for page");
michael@0 133 }
michael@0 134 if (evt.data != "f_lf_i msg data img" && evt.data != "f_lf_i msg data page") {
michael@0 135 ok(true, "got this message but don't know what it is " + evt.data);
michael@0 136 gPopup.close();
michael@0 137 window.removeEventListener("message", messageReceiver, false);
michael@0 138
michael@0 139 finishTest();
michael@0 140 return;
michael@0 141 }
michael@0 142
michael@0 143 // only run the test when all our children are done loading & setting cookies
michael@0 144 if (++gLoads == gExpectedLoads) {
michael@0 145 gPopup.close();
michael@0 146 window.removeEventListener("message", messageReceiver, false);
michael@0 147
michael@0 148 runTest();
michael@0 149 }
michael@0 150 }
michael@0 151
michael@0 152 // runTest() is run by messageReceiver().
michael@0 153 // Check headers, and count and check cookies.
michael@0 154 function runTest() {
michael@0 155 // set a cookie from a domain of "localhost"
michael@0 156 document.cookie = "o=noes";
michael@0 157
michael@0 158 is(gHeaders, gExpectedHeaders, "number of observed request headers");
michael@0 159
michael@0 160 var cs = SpecialPowers.Cc["@mozilla.org/cookiemanager;1"]
michael@0 161 .getService(SpecialPowers.Ci.nsICookieManager);
michael@0 162 var count = 0;
michael@0 163 var list = cs.enumerator;
michael@0 164 while (list.hasMoreElements()) {
michael@0 165 var cookie = list.getNext().QueryInterface(SpecialPowers.Ci.nsICookie);
michael@0 166 ok(true, "cookie: " + cookie);
michael@0 167 ok(true, "cookie host " + cookie.host + " path " + cookie.path + " name " + cookie.name +
michael@0 168 " value " + cookie.value + " isSecure " + cookie.isSecure + " expires " + cookie.expires);
michael@0 169 ++count;
michael@0 170 }
michael@0 171 is(count, gExpectedCookies, "total number of cookies");
michael@0 172 cs.removeAll();
michael@0 173
michael@0 174 finishTest();
michael@0 175 }

mercurial