summaryrefslogtreecommitdiffstats
path: root/toolkit/components/downloads/test/unit/test_DownloadLegacy.js
blob: 839611ec222e88b54fe4076f493cee659ab271a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim: set ts=2 et sw=2 tw=80: */
/* Any copyright is dedicated to the Public Domain.
 * http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Tests the integration with legacy interfaces for downloads.
 */

"use strict";

// Execution of common tests

Services.prefs.setBoolPref("dom.block_download_insecure", false);

// This is used in common_test_Download.js
// eslint-disable-next-line no-unused-vars
var gUseLegacySaver = true;

var scriptFile = do_get_file("common_test_Download.js");
Services.scriptloader.loadSubScript(NetUtil.newURI(scriptFile).spec);

/**
 * Checks the referrer for restart downloads.
 * If the legacy download is stopped and restarted, the saving method
 * is changed from DownloadLegacySaver to the DownloadCopySaver.
 * The referrer header should be passed correctly.
 */
add_task(async function test_referrer_restart() {
  let sourcePath = "/test_referrer_restart.txt";
  let sourceUrl = httpUrl("test_referrer_restart.txt");

  function cleanup() {
    gHttpServer.registerPathHandler(sourcePath, null);
  }
  registerCleanupFunction(cleanup);

  registerInterruptibleHandler(
    sourcePath,
    function firstPart(aRequest, aResponse) {
      aResponse.setHeader("Content-Type", "text/plain", false);
      Assert.ok(aRequest.hasHeader("Referer"));
      Assert.equal(aRequest.getHeader("Referer"), TEST_REFERRER_URL);

      aResponse.setHeader(
        "Content-Length",
        "" + TEST_DATA_SHORT.length * 2,
        false
      );
      aResponse.write(TEST_DATA_SHORT);
    },
    function secondPart(aRequest, aResponse) {
      Assert.ok(aRequest.hasHeader("Referer"));
      Assert.equal(aRequest.getHeader("Referer"), TEST_REFERRER_URL);

      aResponse.write(TEST_DATA_SHORT);
    }
  );

  let referrerInfo = new ReferrerInfo(
    Ci.nsIReferrerInfo.UNSAFE_URL,
    true,
    NetUtil.newURI(TEST_REFERRER_URL)
  );

  async function restart_and_check_referrer(download) {
    let promiseSucceeded = download.whenSucceeded();

    // Cancel the first download attempt.
    await promiseDownloadMidway(download);
    await download.cancel();

    // The second request is allowed to complete.
    continueResponses();
    download.start().catch(() => {});

    // Wait for the download to finish by waiting on the whenSucceeded promise.
    await promiseSucceeded;

    Assert.ok(download.stopped);
    Assert.ok(download.succeeded);
    Assert.ok(!download.canceled);

    checkEqualReferrerInfos(download.source.referrerInfo, referrerInfo);
  }

  mustInterruptResponses();

  let download = await promiseStartLegacyDownload(sourceUrl, {
    referrerInfo,
  });
  await restart_and_check_referrer(download);

  mustInterruptResponses();
  download = await promiseStartLegacyDownload(sourceUrl, {
    referrerInfo,
    isPrivate: true,
  });
  await restart_and_check_referrer(download);

  cleanup();
});