|
1 // This file tests bug 250375 |
|
2 |
|
3 Cu.import("resource://testing-common/httpd.js"); |
|
4 Cu.import("resource://gre/modules/Services.jsm"); |
|
5 |
|
6 XPCOMUtils.defineLazyGetter(this, "URL", function() { |
|
7 return "http://localhost:" + httpserv.identity.primaryPort + "/"; |
|
8 }); |
|
9 |
|
10 function inChildProcess() { |
|
11 return Cc["@mozilla.org/xre/app-info;1"] |
|
12 .getService(Ci.nsIXULRuntime) |
|
13 .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT; |
|
14 } |
|
15 |
|
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 } |
|
26 |
|
27 var cookieVal = "C1=V1"; |
|
28 |
|
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 } |
|
37 |
|
38 throw Components.results.NS_ERROR_ABORT; |
|
39 }, |
|
40 |
|
41 onDataAvailable: function test_ODA() { |
|
42 throw Components.results.NS_ERROR_UNEXPECTED; |
|
43 }, |
|
44 |
|
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 }, |
|
54 |
|
55 _iteration: 1 |
|
56 }; |
|
57 |
|
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); |
|
63 |
|
64 return chan; |
|
65 } |
|
66 |
|
67 var httpserv = null; |
|
68 |
|
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); |
|
73 |
|
74 httpserv = new HttpServer(); |
|
75 httpserv.start(-1); |
|
76 |
|
77 var chan = makeChan(); |
|
78 |
|
79 chan.setRequestHeader("Cookie", cookieVal, false); |
|
80 |
|
81 chan.asyncOpen(listener, null); |
|
82 |
|
83 do_test_pending(); |
|
84 } |
|
85 |
|
86 function run_test_continued() { |
|
87 var chan = makeChan(); |
|
88 |
|
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); |
|
94 |
|
95 // We expect that the setRequestHeader overrides the |
|
96 // automatically-added one, so insert cookie2 in front |
|
97 cookieVal = cookie2 + "; " + cookieVal; |
|
98 |
|
99 listener._iteration++; |
|
100 chan.asyncOpen(listener, null); |
|
101 |
|
102 do_test_pending(); |
|
103 } |