netwerk/test/unit/test_cookie_header.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/test/unit/test_cookie_header.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,103 @@
     1.4 +// This file tests bug 250375
     1.5 +
     1.6 +Cu.import("resource://testing-common/httpd.js");
     1.7 +Cu.import("resource://gre/modules/Services.jsm");
     1.8 +
     1.9 +XPCOMUtils.defineLazyGetter(this, "URL", function() {
    1.10 +  return "http://localhost:" + httpserv.identity.primaryPort + "/";
    1.11 +});
    1.12 +
    1.13 +function inChildProcess() {
    1.14 +  return Cc["@mozilla.org/xre/app-info;1"]
    1.15 +           .getService(Ci.nsIXULRuntime)
    1.16 +           .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;  
    1.17 +}
    1.18 +
    1.19 +function check_request_header(chan, name, value) {
    1.20 +  var chanValue;
    1.21 +  try {
    1.22 +    chanValue = chan.getRequestHeader(name);
    1.23 +  } catch (e) {
    1.24 +    do_throw("Expected to find header '" + name + "' but didn't find it, got exception: " + e);
    1.25 +  }
    1.26 +  dump("Value for header '" + name + "' is '" + chanValue + "'\n");
    1.27 +  do_check_eq(chanValue, value);
    1.28 +}
    1.29 +
    1.30 +var cookieVal = "C1=V1";
    1.31 +
    1.32 +var listener = {
    1.33 +  onStartRequest: function test_onStartR(request, ctx) {
    1.34 +    try {
    1.35 +      var chan = request.QueryInterface(Components.interfaces.nsIHttpChannel);
    1.36 +      check_request_header(chan, "Cookie", cookieVal);
    1.37 +    } catch (e) {
    1.38 +      do_throw("Unexpected exception: " + e);
    1.39 +    }
    1.40 +
    1.41 +    throw Components.results.NS_ERROR_ABORT;
    1.42 +  },
    1.43 +
    1.44 +  onDataAvailable: function test_ODA() {
    1.45 +    throw Components.results.NS_ERROR_UNEXPECTED;
    1.46 +  },
    1.47 +
    1.48 +  onStopRequest: function test_onStopR(request, ctx, status) {
    1.49 +    if (this._iteration == 1) {
    1.50 +      run_test_continued();
    1.51 +    } else {
    1.52 +      do_test_pending();
    1.53 +      httpserv.stop(do_test_finished);
    1.54 +    }
    1.55 +    do_test_finished();
    1.56 +  },
    1.57 +
    1.58 +  _iteration: 1
    1.59 +};
    1.60 +
    1.61 +function makeChan() {
    1.62 +  var ios = Components.classes["@mozilla.org/network/io-service;1"]
    1.63 +                      .getService(Components.interfaces.nsIIOService);
    1.64 +  var chan = ios.newChannel(URL, null, null)
    1.65 +                .QueryInterface(Components.interfaces.nsIHttpChannel);
    1.66 +
    1.67 +  return chan;
    1.68 +}
    1.69 +
    1.70 +var httpserv = null;
    1.71 +
    1.72 +function run_test() {
    1.73 +  // Allow all cookies if the pref service is available in this process.
    1.74 +  if (!inChildProcess())
    1.75 +    Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
    1.76 +
    1.77 +  httpserv = new HttpServer();
    1.78 +  httpserv.start(-1);
    1.79 +
    1.80 +  var chan = makeChan();
    1.81 +
    1.82 +  chan.setRequestHeader("Cookie", cookieVal, false);
    1.83 +
    1.84 +  chan.asyncOpen(listener, null);
    1.85 +
    1.86 +  do_test_pending();
    1.87 +}
    1.88 +
    1.89 +function run_test_continued() {
    1.90 +  var chan = makeChan();
    1.91 +
    1.92 +  var cookServ = Components.classes["@mozilla.org/cookieService;1"]
    1.93 +                           .getService(Components.interfaces.nsICookieService);
    1.94 +  var cookie2 = "C2=V2";
    1.95 +  cookServ.setCookieString(chan.URI, null, cookie2, chan);
    1.96 +  chan.setRequestHeader("Cookie", cookieVal, false);
    1.97 +
    1.98 +  // We expect that the setRequestHeader overrides the
    1.99 +  // automatically-added one, so insert cookie2 in front
   1.100 +  cookieVal = cookie2 + "; " + cookieVal;
   1.101 +
   1.102 +  listener._iteration++;
   1.103 +  chan.asyncOpen(listener, null);
   1.104 +
   1.105 +  do_test_pending();
   1.106 +}

mercurial