Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 // This file tests bug 250375
3 Cu.import("resource://testing-common/httpd.js");
4 Cu.import("resource://gre/modules/Services.jsm");
6 XPCOMUtils.defineLazyGetter(this, "URL", function() {
7 return "http://localhost:" + httpserv.identity.primaryPort + "/";
8 });
10 function inChildProcess() {
11 return Cc["@mozilla.org/xre/app-info;1"]
12 .getService(Ci.nsIXULRuntime)
13 .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT;
14 }
16 function check_request_header(chan, name, value) {
17 var chanValue;
18 try {
19 chanValue = chan.getRequestHeader(name);
20 } catch (e) {
21 do_throw("Expected to find header '" + name + "' but didn't find it, got exception: " + e);
22 }
23 dump("Value for header '" + name + "' is '" + chanValue + "'\n");
24 do_check_eq(chanValue, value);
25 }
27 var cookieVal = "C1=V1";
29 var listener = {
30 onStartRequest: function test_onStartR(request, ctx) {
31 try {
32 var chan = request.QueryInterface(Components.interfaces.nsIHttpChannel);
33 check_request_header(chan, "Cookie", cookieVal);
34 } catch (e) {
35 do_throw("Unexpected exception: " + e);
36 }
38 throw Components.results.NS_ERROR_ABORT;
39 },
41 onDataAvailable: function test_ODA() {
42 throw Components.results.NS_ERROR_UNEXPECTED;
43 },
45 onStopRequest: function test_onStopR(request, ctx, status) {
46 if (this._iteration == 1) {
47 run_test_continued();
48 } else {
49 do_test_pending();
50 httpserv.stop(do_test_finished);
51 }
52 do_test_finished();
53 },
55 _iteration: 1
56 };
58 function makeChan() {
59 var ios = Components.classes["@mozilla.org/network/io-service;1"]
60 .getService(Components.interfaces.nsIIOService);
61 var chan = ios.newChannel(URL, null, null)
62 .QueryInterface(Components.interfaces.nsIHttpChannel);
64 return chan;
65 }
67 var httpserv = null;
69 function run_test() {
70 // Allow all cookies if the pref service is available in this process.
71 if (!inChildProcess())
72 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
74 httpserv = new HttpServer();
75 httpserv.start(-1);
77 var chan = makeChan();
79 chan.setRequestHeader("Cookie", cookieVal, false);
81 chan.asyncOpen(listener, null);
83 do_test_pending();
84 }
86 function run_test_continued() {
87 var chan = makeChan();
89 var cookServ = Components.classes["@mozilla.org/cookieService;1"]
90 .getService(Components.interfaces.nsICookieService);
91 var cookie2 = "C2=V2";
92 cookServ.setCookieString(chan.URI, null, cookie2, chan);
93 chan.setRequestHeader("Cookie", cookieVal, false);
95 // We expect that the setRequestHeader overrides the
96 // automatically-added one, so insert cookie2 in front
97 cookieVal = cookie2 + "; " + cookieVal;
99 listener._iteration++;
100 chan.asyncOpen(listener, null);
102 do_test_pending();
103 }