Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_bug1279246.js
blob8f904eb7eb951c343cc8ca475a8218d62289e090
1 "use strict";
3 const { HttpServer } = ChromeUtils.importESModule(
4 "resource://testing-common/httpd.sys.mjs"
5 );
7 var httpserver = new HttpServer();
8 var pass = 0;
9 var responseBody = [0x0b, 0x02, 0x80, 0x74, 0x65, 0x73, 0x74, 0x0a, 0x03];
10 var responseLen = 5;
11 var testUrl = "/test/brotli";
13 function setupChannel() {
14 return NetUtil.newChannel({
15 uri: "http://localhost:" + httpserver.identity.primaryPort + testUrl,
16 loadUsingSystemPrincipal: true,
17 });
20 function Listener() {}
22 Listener.prototype = {
23 _buffer: null,
25 QueryInterface: ChromeUtils.generateQI([
26 "nsIStreamListener",
27 "nsIRequestObserver",
28 ]),
30 onStartRequest(request) {
31 Assert.equal(request.status, Cr.NS_OK);
32 this._buffer = "";
35 onDataAvailable(request, stream, offset, cnt) {
36 if (pass == 0) {
37 this._buffer = this._buffer.concat(read_stream(stream, cnt));
38 } else {
39 request.QueryInterface(Ci.nsICachingChannel);
40 if (!request.isFromCache()) {
41 do_throw("Response is not from the cache");
44 request.cancel(Cr.NS_ERROR_ABORT);
48 onStopRequest() {
49 if (pass == 0) {
50 Assert.equal(this._buffer.length, responseLen);
51 pass++;
53 var channel = setupChannel();
54 channel.loadFlags = Ci.nsIRequest.VALIDATE_NEVER;
55 channel.asyncOpen(new Listener());
56 } else {
57 httpserver.stop(do_test_finished);
58 prefs.setCharPref("network.http.accept-encoding", cePref);
63 var prefs;
64 var cePref;
65 function run_test() {
66 do_get_profile();
68 prefs = Services.prefs;
69 cePref = prefs.getCharPref("network.http.accept-encoding");
70 prefs.setCharPref("network.http.accept-encoding", "gzip, deflate, br");
72 // Disable rcwn to make cache behavior deterministic.
73 prefs.setBoolPref("network.http.rcwn.enabled", false);
75 httpserver.registerPathHandler(testUrl, handler);
76 httpserver.start(-1);
78 var channel = setupChannel();
79 channel.asyncOpen(new Listener());
81 do_test_pending();
84 function handler(metadata, response) {
85 Assert.equal(pass, 0); // the second response must be server from the cache
87 response.setStatusLine(metadata.httpVersion, 200, "OK");
88 response.setHeader("Content-Type", "text/plain", false);
89 response.setHeader("Content-Encoding", "br", false);
90 response.setHeader("Content-Length", "" + responseBody.length, false);
92 var bos = Cc["@mozilla.org/binaryoutputstream;1"].createInstance(
93 Ci.nsIBinaryOutputStream
95 bos.setOutputStream(response.bodyOutputStream);
97 response.processAsync();
98 bos.writeByteArray(responseBody);
99 response.finish();