summaryrefslogtreecommitdiffstats
path: root/netwerk/test/httpserver/test/test_seizepower.js
blob: 5c21a944241b07e09333bc6676f3b78a5c0d176a (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
/* vim:set ts=2 sw=2 sts=2 et: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/*
 * Tests that the seizePower API works correctly.
 */

XPCOMUtils.defineLazyGetter(this, "PORT", function () {
  return srv.identity.primaryPort;
});

var srv;

function run_test() {
  Services.prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
  srv = createServer();

  srv.registerPathHandler("/raw-data", handleRawData);
  srv.registerPathHandler("/called-too-late", handleTooLate);
  srv.registerPathHandler("/exceptions", handleExceptions);
  srv.registerPathHandler("/async-seizure", handleAsyncSeizure);
  srv.registerPathHandler("/seize-after-async", handleSeizeAfterAsync);

  srv.start(-1);

  runRawTests(tests, function () {
    Services.prefs.clearUserPref("network.proxy.allow_hijacking_localhost");
    testComplete(srv)();
  });
}

function checkException(fun, err, msg) {
  try {
    fun();
  } catch (e) {
    if (e !== err && e.result !== err) {
      do_throw(msg);
    }
    return;
  }
  do_throw(msg);
}

/** ***************
 * PATH HANDLERS *
 *****************/

function handleRawData(request, response) {
  response.seizePower();
  response.write("Raw data!");
  response.finish();
}

function handleTooLate(request, response) {
  response.write("DO NOT WANT");
  var output = response.bodyOutputStream;

  response.seizePower();

  if (response.bodyOutputStream !== output) {
    response.write("bodyOutputStream changed!");
  } else {
    response.write("too-late passed");
  }
  response.finish();
}

function handleExceptions(request, response) {
  response.seizePower();
  checkException(
    function () {
      response.setStatusLine("1.0", 500, "ISE");
    },
    Cr.NS_ERROR_NOT_AVAILABLE,
    "setStatusLine should throw not-available after seizePower"
  );
  checkException(
    function () {
      response.setHeader("X-Fail", "FAIL", false);
    },
    Cr.NS_ERROR_NOT_AVAILABLE,
    "setHeader should throw not-available after seizePower"
  );
  checkException(
    function () {
      response.processAsync();
    },
    Cr.NS_ERROR_NOT_AVAILABLE,
    "processAsync should throw not-available after seizePower"
  );
  var out = response.bodyOutputStream;
  var data = "exceptions test passed";
  out.write(data, data.length);
  response.seizePower(); // idempotency test of seizePower
  response.finish();
  response.finish(); // idempotency test of finish after seizePower
  checkException(
    function () {
      response.seizePower();
    },
    Cr.NS_ERROR_UNEXPECTED,
    "seizePower should throw unexpected after finish"
  );
}

function handleAsyncSeizure(request, response) {
  response.seizePower();
  callLater(1, function () {
    response.write("async seizure passed");
    response.bodyOutputStream.close();
    callLater(1, function () {
      response.finish();
    });
  });
}

function handleSeizeAfterAsync(request, response) {
  response.setStatusLine(request.httpVersion, 200, "async seizure pass");
  response.processAsync();
  checkException(
    function () {
      response.seizePower();
    },
    Cr.NS_ERROR_NOT_AVAILABLE,
    "seizePower should throw not-available after processAsync"
  );
  callLater(1, function () {
    response.finish();
  });
}

/** *************
 * BEGIN TESTS *
 ***************/

XPCOMUtils.defineLazyGetter(this, "tests", function () {
  return [
    new RawTest("localhost", PORT, data0, checkRawData),
    new RawTest("localhost", PORT, data1, checkTooLate),
    new RawTest("localhost", PORT, data2, checkExceptions),
    new RawTest("localhost", PORT, data3, checkAsyncSeizure),
    new RawTest("localhost", PORT, data4, checkSeizeAfterAsync),
  ];
});

// eslint-disable-next-line no-useless-concat
var data0 = "GET /raw-data HTTP/1.0\r\n" + "\r\n";
function checkRawData(data) {
  Assert.equal(data, "Raw data!");
}

// eslint-disable-next-line no-useless-concat
var data1 = "GET /called-too-late HTTP/1.0\r\n" + "\r\n";
function checkTooLate(data) {
  Assert.equal(LineIterator(data).next().value, "too-late passed");
}

// eslint-disable-next-line no-useless-concat
var data2 = "GET /exceptions HTTP/1.0\r\n" + "\r\n";
function checkExceptions(data) {
  Assert.equal("exceptions test passed", data);
}

// eslint-disable-next-line no-useless-concat
var data3 = "GET /async-seizure HTTP/1.0\r\n" + "\r\n";
function checkAsyncSeizure(data) {
  Assert.equal(data, "async seizure passed");
}

// eslint-disable-next-line no-useless-concat
var data4 = "GET /seize-after-async HTTP/1.0\r\n" + "\r\n";
function checkSeizeAfterAsync(data) {
  Assert.equal(
    LineIterator(data).next().value,
    "HTTP/1.0 200 async seizure pass"
  );
}