summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_resumable_truncate.js
blob: c0da631e08c0bd4164658c0190b1a052e73052cd (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
"use strict";

const { HttpServer } = ChromeUtils.importESModule(
  "resource://testing-common/httpd.sys.mjs"
);

var httpserver = null;

function make_channel(url) {
  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
}

const responseBody = "response body";

function cachedHandler(metadata, response) {
  var body = responseBody;
  if (metadata.hasHeader("Range")) {
    var matches = metadata
      .getHeader("Range")
      .match(/^\s*bytes=(\d+)?-(\d+)?\s*$/);
    var from = matches[1] === undefined ? 0 : matches[1];
    var to = matches[2] === undefined ? responseBody.length - 1 : matches[2];
    if (from >= responseBody.length) {
      response.setStatusLine(metadata.httpVersion, 416, "Start pos too high");
      response.setHeader("Content-Range", "*/" + responseBody.length, false);
      return;
    }
    body = responseBody.slice(from, to + 1);
    // always respond to successful range requests with 206
    response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
    response.setHeader(
      "Content-Range",
      from + "-" + to + "/" + responseBody.length,
      false
    );
  }

  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("ETag", "Just testing");
  response.setHeader("Accept-Ranges", "bytes");

  response.bodyOutputStream.write(body, body.length);
}

function Canceler(continueFn) {
  this.continueFn = continueFn;
}

Canceler.prototype = {
  QueryInterface: ChromeUtils.generateQI([
    "nsIStreamListener",
    "nsIRequestObserver",
  ]),

  onStartRequest() {},

  onDataAvailable(request) {
    request.QueryInterface(Ci.nsIChannel).cancel(Cr.NS_BINDING_ABORTED);
  },

  onStopRequest(request, status) {
    Assert.equal(status, Cr.NS_BINDING_ABORTED);
    this.continueFn();
  },
};

function finish_test() {
  httpserver.stop(do_test_finished);
}

function start_cache_read() {
  var chan = make_channel(
    "http://localhost:" + httpserver.identity.primaryPort + "/cached/test.gz"
  );
  chan.asyncOpen(new ChannelListener(finish_test, null));
}

function start_canceler() {
  var chan = make_channel(
    "http://localhost:" + httpserver.identity.primaryPort + "/cached/test.gz"
  );
  chan.asyncOpen(new Canceler(start_cache_read));
}

function run_test() {
  httpserver = new HttpServer();
  httpserver.registerPathHandler("/cached/test.gz", cachedHandler);
  httpserver.start(-1);

  var chan = make_channel(
    "http://localhost:" + httpserver.identity.primaryPort + "/cached/test.gz"
  );
  chan.asyncOpen(new ChannelListener(start_canceler, null));
  do_test_pending();
}