|
1 Cu.import("resource://testing-common/httpd.js"); |
|
2 |
|
3 const sentCookieVal = "foo=bar"; |
|
4 const responseBody = "response body"; |
|
5 |
|
6 XPCOMUtils.defineLazyGetter(this, "baseURL", function() { |
|
7 return "http://localhost:" + httpServer.identity.primaryPort; |
|
8 }); |
|
9 |
|
10 const preRedirectPath = "/528292/pre-redirect"; |
|
11 |
|
12 XPCOMUtils.defineLazyGetter(this, "preRedirectURL", function() { |
|
13 return baseURL + preRedirectPath; |
|
14 }); |
|
15 |
|
16 const postRedirectPath = "/528292/post-redirect"; |
|
17 |
|
18 XPCOMUtils.defineLazyGetter(this, "postRedirectURL", function() { |
|
19 return baseURL + postRedirectPath; |
|
20 }); |
|
21 |
|
22 var httpServer = null; |
|
23 var receivedCookieVal = null; |
|
24 |
|
25 function preRedirectHandler(metadata, response) |
|
26 { |
|
27 response.setStatusLine(metadata.httpVersion, 302, "Found"); |
|
28 response.setHeader("Location", postRedirectURL, false); |
|
29 return; |
|
30 } |
|
31 |
|
32 function postRedirectHandler(metadata, response) |
|
33 { |
|
34 receivedCookieVal = metadata.getHeader("Cookie"); |
|
35 response.setHeader("Content-Type", "text/plain"); |
|
36 response.bodyOutputStream.write(responseBody, responseBody.length); |
|
37 } |
|
38 |
|
39 function run_test() |
|
40 { |
|
41 // Start the HTTP server. |
|
42 httpServer = new HttpServer(); |
|
43 httpServer.registerPathHandler(preRedirectPath, preRedirectHandler); |
|
44 httpServer.registerPathHandler(postRedirectPath, postRedirectHandler); |
|
45 httpServer.start(-1); |
|
46 |
|
47 // Disable third-party cookies in general. |
|
48 Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch). |
|
49 setIntPref("network.cookie.cookieBehavior", 1); |
|
50 |
|
51 var ioService = Cc["@mozilla.org/network/io-service;1"]. |
|
52 getService(Ci.nsIIOService); |
|
53 |
|
54 // Set up a channel with forceAllowThirdPartyCookie set to true. We'll use |
|
55 // the channel both to set a cookie (since nsICookieService::setCookieString |
|
56 // requires such a channel in order to successfully set a cookie) and then |
|
57 // to load the pre-redirect URI. |
|
58 var chan = ioService.newChannel(preRedirectURL, "", null). |
|
59 QueryInterface(Ci.nsIHttpChannel). |
|
60 QueryInterface(Ci.nsIHttpChannelInternal); |
|
61 chan.forceAllowThirdPartyCookie = true; |
|
62 |
|
63 // Set a cookie on one of the URIs. It doesn't matter which one, since |
|
64 // they're both from the same host, which is enough for the cookie service |
|
65 // to send the cookie with both requests. |
|
66 var postRedirectURI = ioService.newURI(postRedirectURL, "", null); |
|
67 Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService). |
|
68 setCookieString(postRedirectURI, null, sentCookieVal, chan); |
|
69 |
|
70 // Load the pre-redirect URI. |
|
71 chan.asyncOpen(new ChannelListener(finish_test, null), null); |
|
72 do_test_pending(); |
|
73 } |
|
74 |
|
75 function finish_test(event) |
|
76 { |
|
77 do_check_eq(receivedCookieVal, sentCookieVal); |
|
78 httpServer.stop(do_test_finished); |
|
79 } |