summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_partial_response_entry_size_smart_shrink.js
blob: c2a16cc8ebdabadb07ea432e3428887d32255e84 (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
103
104
105
106
/*

  This is only a crash test.  We load a partial content, cache it.  Then we change the limit
  for single cache entry size (shrink it) so that the next request for the rest of the content
  will hit that limit and doom/remove the entry.  We change the size manually, but in reality
  it's being changed by cache smart size.

*/

"use strict";

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

ChromeUtils.defineLazyGetter(this, "URL", function () {
  return "http://localhost:" + httpServer.identity.primaryPort;
});

var httpServer = null;

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

// Have 2kb response (8 * 2 ^ 8)
var responseBody = "response";
for (var i = 0; i < 8; ++i) {
  responseBody += responseBody;
}

function contentHandler(metadata, response) {
  response.setHeader("Content-Type", "text/plain", false);
  response.setHeader("ETag", "range");
  response.setHeader("Accept-Ranges", "bytes");
  response.setHeader("Cache-Control", "max-age=360000");

  if (!metadata.hasHeader("If-Range")) {
    response.setHeader("Content-Length", responseBody.length + "");
    response.processAsync();
    let slice = responseBody.slice(0, 100);
    response.bodyOutputStream.write(slice, slice.length);
    response.finish();
  } else {
    let slice = responseBody.slice(100);
    response.setStatusLine(metadata.httpVersion, 206, "Partial Content");
    response.setHeader(
      "Content-Range",
      (responseBody.length - slice.length).toString() +
        "-" +
        (responseBody.length - 1).toString() +
        "/" +
        responseBody.length.toString()
    );

    response.setHeader("Content-Length", slice.length + "");
    response.bodyOutputStream.write(slice, slice.length);
  }
}

let enforceSoftPref;
let enforceStrictChunkedPref;

function run_test() {
  enforceSoftPref = Services.prefs.getBoolPref(
    "network.http.enforce-framing.soft"
  );
  Services.prefs.setBoolPref("network.http.enforce-framing.soft", false);

  enforceStrictChunkedPref = Services.prefs.getBoolPref(
    "network.http.enforce-framing.strict_chunked_encoding"
  );
  Services.prefs.setBoolPref(
    "network.http.enforce-framing.strict_chunked_encoding",
    false
  );

  httpServer = new HttpServer();
  httpServer.registerPathHandler("/content", contentHandler);
  httpServer.start(-1);

  var chan = make_channel(URL + "/content");
  chan.asyncOpen(new ChannelListener(firstTimeThrough, null, CL_IGNORE_CL));
  do_test_pending();
}

function firstTimeThrough() {
  // Change single cache entry limit to 1 kb.  This emulates smart size change.
  Services.prefs.setIntPref("browser.cache.disk.max_entry_size", 1);

  var chan = make_channel(URL + "/content");
  chan.asyncOpen(new ChannelListener(finish_test, null));
}

function finish_test(request, buffer) {
  Assert.equal(buffer, responseBody);
  Services.prefs.setBoolPref(
    "network.http.enforce-framing.soft",
    enforceSoftPref
  );
  Services.prefs.setBoolPref(
    "network.http.enforce-framing.strict_chunked_encoding",
    enforceStrictChunkedPref
  );
  httpServer.stop(do_test_finished);
}