summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_reentrancy.js
blob: 3bd7f54f79d6bd8ce657641ffe72027d436c1018 (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
107
108
109
"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 = new HttpServer();
var testpath = "/simple";
var httpbody = "<?xml version='1.0' ?><root>0123456789</root>";

function syncXHR() {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", URL + testpath, false);
  xhr.send(null);
}

const MAX_TESTS = 2;

var listener = {
  _done_onStart: false,
  _done_onData: false,
  _test: 0,

  QueryInterface: ChromeUtils.generateQI([
    "nsIStreamListener",
    "nsIRequestObserver",
  ]),

  onStartRequest(request) {
    switch (this._test) {
      case 0:
        request.suspend();
        syncXHR();
        request.resume();
        break;
      case 1:
        request.suspend();
        syncXHR();
        executeSoon(function () {
          request.resume();
        });
        break;
      case 2:
        executeSoon(function () {
          request.suspend();
        });
        executeSoon(function () {
          request.resume();
        });
        syncXHR();
        break;
    }

    this._done_onStart = true;
  },

  onDataAvailable(request, stream, offset, count) {
    Assert.ok(this._done_onStart);
    read_stream(stream, count);
    this._done_onData = true;
  },

  onStopRequest(request, status) {
    Assert.ok(this._done_onData);
    this._reset();
    if (this._test <= MAX_TESTS) {
      next_test();
    } else {
      httpserver.stop(do_test_finished);
    }
  },

  _reset() {
    this._done_onStart = false;
    this._done_onData = false;
    this._test++;
  },
};

function makeChan(url) {
  return NetUtil.newChannel({
    uri: url,
    loadUsingSystemPrincipal: true,
  }).QueryInterface(Ci.nsIHttpChannel);
}

function next_test() {
  var chan = makeChan(URL + testpath);
  chan.QueryInterface(Ci.nsIRequest);
  chan.asyncOpen(listener);
}

function run_test() {
  httpserver.registerPathHandler(testpath, serverHandler);
  httpserver.start(-1);

  next_test();

  do_test_pending();
}

function serverHandler(metadata, response) {
  response.setHeader("Content-Type", "text/xml", false);
  response.bodyOutputStream.write(httpbody, httpbody.length);
}