PageRenderTime 16ms CodeModel.GetById 1ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/cookie/test/file_testloadflags.js

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