Fri, 16 Jan 2015 18:13:44 +0100
Integrate suggestion from review to improve consistency with existing code.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 // Public request gets times=0 cookie, completes
6 // Private request gets times=1 cookie, canceled
7 // Private resumed request sends times=1 cookie, completes
9 function run_test() {
10 if (oldDownloadManagerDisabled()) {
11 return;
12 }
14 // Allow all cookies.
15 Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
17 do_test_pending();
18 let httpserv = new HttpServer();
20 let times = 0;
21 httpserv.registerPathHandler("/head_download_manager.js", function (meta, response) {
22 response.setHeader("Content-Type", "text/plain", false);
23 response.setStatusLine("1.1", !meta.hasHeader('range') ? 200 : 206);
25 // Set a cookie if none is sent with the request. Public and private requests
26 // should therefore receive different cookies, so we can tell if the resumed
27 // request is actually treated as private or not.
28 if (!meta.hasHeader('Cookie')) {
29 do_check_true(times == 0 || times == 1);
30 response.setHeader('Set-Cookie', 'times=' + times++);
31 } else {
32 do_check_eq(times, 2);
33 do_check_eq(meta.getHeader('Cookie'), 'times=1');
34 }
35 let full = "";
36 let body = "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"; //60
37 for (var i = 0; i < 1000; i++) {
38 full += body;
39 }
40 response.write(full);
41 });
42 httpserv.start(-1);
44 let state = 0;
46 let listener = {
47 onDownloadStateChange: function(aState, aDownload) {
48 switch (aDownload.state) {
49 case downloadUtils.downloadManager.DOWNLOAD_DOWNLOADING:
50 // We only care about the private download
51 if (state != 1)
52 break;
54 state++;
55 do_check_true(aDownload.resumable);
57 aDownload.pause();
58 do_check_eq(aDownload.state, downloadUtils.downloadManager.DOWNLOAD_PAUSED);
60 do_execute_soon(function() {
61 aDownload.resume();
62 });
63 break;
65 case downloadUtils.downloadManager.DOWNLOAD_FINISHED:
66 if (state == 0) {
67 do_execute_soon(function() {
68 // Perform an identical request but in private mode.
69 // It should receive a different cookie than the
70 // public request.
72 state++;
74 addDownload(httpserv, {
75 isPrivate: true,
76 sourceURI: downloadCSource,
77 downloadName: downloadCName + "!!!",
78 runBeforeStart: function (aDownload) {
79 // Check that dl is retrievable
80 do_check_eq(downloadUtils.downloadManager.activePrivateDownloadCount, 1);
81 }
82 });
83 });
84 } else if (state == 2) {
85 // We're done here.
86 do_execute_soon(function() {
87 httpserv.stop(do_test_finished);
88 });
89 }
90 break;
92 default:
93 break;
94 }
95 },
96 onStateChange: function(a, b, c, d, e) { },
97 onProgressChange: function(a, b, c, d, e, f, g) { },
98 onSecurityChange: function(a, b, c, d) { }
99 };
101 downloadUtils.downloadManager.addPrivacyAwareListener(listener);
103 const downloadCSource = "http://localhost:" +
104 httpserv.identity.primaryPort +
105 "/head_download_manager.js";
106 const downloadCName = "download-C";
108 // First a public download that completes without interruption.
109 let dl = addDownload(httpserv, {
110 isPrivate: false,
111 sourceURI: downloadCSource,
112 downloadName: downloadCName,
113 runBeforeStart: function (aDownload) {
114 // Check that dl is retrievable
115 do_check_eq(downloadUtils.downloadManager.activeDownloadCount, 1);
116 }
117 });
118 }