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