michael@0: // This file tests bug 250375 michael@0: michael@0: Cu.import("resource://testing-common/httpd.js"); michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: XPCOMUtils.defineLazyGetter(this, "URL", function() { michael@0: return "http://localhost:" + httpserv.identity.primaryPort + "/"; michael@0: }); michael@0: michael@0: function inChildProcess() { michael@0: return Cc["@mozilla.org/xre/app-info;1"] michael@0: .getService(Ci.nsIXULRuntime) michael@0: .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; michael@0: } michael@0: michael@0: function check_request_header(chan, name, value) { michael@0: var chanValue; michael@0: try { michael@0: chanValue = chan.getRequestHeader(name); michael@0: } catch (e) { michael@0: do_throw("Expected to find header '" + name + "' but didn't find it, got exception: " + e); michael@0: } michael@0: dump("Value for header '" + name + "' is '" + chanValue + "'\n"); michael@0: do_check_eq(chanValue, value); michael@0: } michael@0: michael@0: var cookieVal = "C1=V1"; michael@0: michael@0: var listener = { michael@0: onStartRequest: function test_onStartR(request, ctx) { michael@0: try { michael@0: var chan = request.QueryInterface(Components.interfaces.nsIHttpChannel); michael@0: check_request_header(chan, "Cookie", cookieVal); michael@0: } catch (e) { michael@0: do_throw("Unexpected exception: " + e); michael@0: } michael@0: michael@0: throw Components.results.NS_ERROR_ABORT; michael@0: }, michael@0: michael@0: onDataAvailable: function test_ODA() { michael@0: throw Components.results.NS_ERROR_UNEXPECTED; michael@0: }, michael@0: michael@0: onStopRequest: function test_onStopR(request, ctx, status) { michael@0: if (this._iteration == 1) { michael@0: run_test_continued(); michael@0: } else { michael@0: do_test_pending(); michael@0: httpserv.stop(do_test_finished); michael@0: } michael@0: do_test_finished(); michael@0: }, michael@0: michael@0: _iteration: 1 michael@0: }; michael@0: michael@0: function makeChan() { michael@0: var ios = Components.classes["@mozilla.org/network/io-service;1"] michael@0: .getService(Components.interfaces.nsIIOService); michael@0: var chan = ios.newChannel(URL, null, null) michael@0: .QueryInterface(Components.interfaces.nsIHttpChannel); michael@0: michael@0: return chan; michael@0: } michael@0: michael@0: var httpserv = null; michael@0: michael@0: function run_test() { michael@0: // Allow all cookies if the pref service is available in this process. michael@0: if (!inChildProcess()) michael@0: Services.prefs.setIntPref("network.cookie.cookieBehavior", 0); michael@0: michael@0: httpserv = new HttpServer(); michael@0: httpserv.start(-1); michael@0: michael@0: var chan = makeChan(); michael@0: michael@0: chan.setRequestHeader("Cookie", cookieVal, false); michael@0: michael@0: chan.asyncOpen(listener, null); michael@0: michael@0: do_test_pending(); michael@0: } michael@0: michael@0: function run_test_continued() { michael@0: var chan = makeChan(); michael@0: michael@0: var cookServ = Components.classes["@mozilla.org/cookieService;1"] michael@0: .getService(Components.interfaces.nsICookieService); michael@0: var cookie2 = "C2=V2"; michael@0: cookServ.setCookieString(chan.URI, null, cookie2, chan); michael@0: chan.setRequestHeader("Cookie", cookieVal, false); michael@0: michael@0: // We expect that the setRequestHeader overrides the michael@0: // automatically-added one, so insert cookie2 in front michael@0: cookieVal = cookie2 + "; " + cookieVal; michael@0: michael@0: listener._iteration++; michael@0: chan.asyncOpen(listener, null); michael@0: michael@0: do_test_pending(); michael@0: }