test_seizepower.js
Go to the documentation of this file.
1 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et: */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is httpd.js code.
17  *
18  * The Initial Developer of the Original Code is
19  * the Mozilla Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 2009
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  * Jeff Walden <jwalden+code@mit.edu> (original author)
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
40 /*
41  * Tests that the seizePower API works correctly.
42  */
43 
44 const PORT = 4444;
45 
46 var srv;
47 
48 function run_test()
49 {
50  srv = createServer();
51 
52  srv.registerPathHandler("/raw-data", handleRawData);
53  srv.registerPathHandler("/called-too-late", handleTooLate);
54  srv.registerPathHandler("/exceptions", handleExceptions);
55  srv.registerPathHandler("/async-seizure", handleAsyncSeizure);
56  srv.registerPathHandler("/seize-after-async", handleSeizeAfterAsync);
57  srv.registerPathHandler("/thrown-exception", handleThrownException);
58  srv.registerPathHandler("/asap-later-write", handleASAPLaterWrite);
59  srv.registerPathHandler("/asap-later-finish", handleASAPLaterFinish);
60 
61  srv.start(PORT);
62 
64 }
65 
66 
67 function checkException(fun, err, msg)
68 {
69  try
70  {
71  fun();
72  }
73  catch (e)
74  {
75  if (e !== err && e.result !== err)
76  do_throw(msg);
77  return;
78  }
79  do_throw(msg);
80 }
81 
82 function callASAPLater(fun)
83 {
84  gThreadManager.currentThread.dispatch({
85  run: function()
86  {
87  fun();
88  }
89  }, Ci.nsIThread.DISPATCH_NORMAL);
90 }
91 
92 
93 /*****************
94  * PATH HANDLERS *
95  *****************/
96 
97 function handleRawData(request, response)
98 {
99  response.seizePower();
100  response.write("Raw data!");
101  response.finish();
102 }
103 
104 function handleTooLate(request, response)
105 {
106  response.write("DO NOT WANT");
107  var output = response.bodyOutputStream;
108 
109  response.seizePower();
110 
111  if (response.bodyOutputStream !== output)
112  response.write("bodyOutputStream changed!");
113  else
114  response.write("too-late passed");
115  response.finish();
116 }
117 
118 function handleExceptions(request, response)
119 {
120  response.seizePower();
121  checkException(function() { response.setStatusLine("1.0", 500, "ISE"); },
122  Cr.NS_ERROR_NOT_AVAILABLE,
123  "setStatusLine should throw not-available after seizePower");
124  checkException(function() { response.setHeader("X-Fail", "FAIL", false); },
125  Cr.NS_ERROR_NOT_AVAILABLE,
126  "setHeader should throw not-available after seizePower");
127  checkException(function() { response.processAsync(); },
128  Cr.NS_ERROR_NOT_AVAILABLE,
129  "processAsync should throw not-available after seizePower");
130  var out = response.bodyOutputStream;
131  var data = "exceptions test passed";
132  out.write(data, data.length);
133  response.seizePower(); // idempotency test of seizePower
134  response.finish();
135  response.finish(); // idempotency test of finish after seizePower
136  checkException(function() { response.seizePower(); },
137  Cr.NS_ERROR_UNEXPECTED,
138  "seizePower should throw unexpected after finish");
139 }
140 
141 function handleAsyncSeizure(request, response)
142 {
143  response.seizePower();
144  callLater(1, function()
145  {
146  response.write("async seizure passed");
147  response.bodyOutputStream.close();
148  callLater(1, function()
149  {
150  response.finish();
151  });
152  });
153 }
154 
155 function handleSeizeAfterAsync(request, response)
156 {
157  response.setStatusLine(request.httpVersion, 200, "async seizure pass");
158  response.processAsync();
159  checkException(function() { response.seizePower(); },
160  Cr.NS_ERROR_NOT_AVAILABLE,
161  "seizePower should throw not-available after processAsync");
162  callLater(1, function()
163  {
164  response.finish();
165  });
166 }
167 
168 function handleThrownException(request, response)
169 {
170  if (request.queryString === "writeBefore")
171  response.write("ignore this");
172  else if (request.queryString === "writeBeforeEmpty")
173  response.write("");
174  else if (request.queryString !== "")
175  throw "query string FAIL";
176  response.seizePower();
177  response.write("preparing to throw...");
178  throw "badness 10000";
179 }
180 
181 function handleASAPLaterWrite(request, response)
182 {
183  response.seizePower();
184  response.write("should only ");
185  response.write("see this");
186 
187  callASAPLater(function()
188  {
189  response.write("...and not this");
190  callASAPLater(function()
191  {
192  response.write("...or this");
193  response.finish();
194  });
195  });
196 
197  throw "opening pitch of the ballgame";
198 }
199 
200 function handleASAPLaterFinish(request, response)
201 {
202  response.seizePower();
203  response.write("should only see this");
204 
205  callASAPLater(function()
206  {
207  response.finish();
208  });
209 
210  throw "out the bum!";
211 }
212 
213 
214 /***************
215  * BEGIN TESTS *
216  ***************/
217 
218 var test, data;
219 var tests = [];
220 
221 data = "GET /raw-data HTTP/1.0\r\n" +
222  "\r\n";
224 {
225  do_check_eq(data, "Raw data!");
226 }
227 test = new RawTest("localhost", PORT, data, checkRawData),
228 tests.push(test);
229 
230 data = "GET /called-too-late HTTP/1.0\r\n" +
231  "\r\n";
233 {
234  do_check_eq(LineIterator(data).next(), "too-late passed");
235 }
236 test = new RawTest("localhost", PORT, data, checkTooLate),
237 tests.push(test);
238 
239 data = "GET /exceptions HTTP/1.0\r\n" +
240  "\r\n";
242 {
243  do_check_eq("exceptions test passed", data);
244 }
245 test = new RawTest("localhost", PORT, data, checkExceptions),
246 tests.push(test);
247 
248 data = "GET /async-seizure HTTP/1.0\r\n" +
249  "\r\n";
251 {
252  do_check_eq(data, "async seizure passed");
253 }
254 test = new RawTest("localhost", PORT, data, checkAsyncSeizure),
255 tests.push(test);
256 
257 data = "GET /seize-after-async HTTP/1.0\r\n" +
258  "\r\n";
260 {
261  do_check_eq(LineIterator(data).next(), "HTTP/1.0 200 async seizure pass");
262 }
263 test = new RawTest("localhost", PORT, data, checkSeizeAfterAsync),
264 tests.push(test);
265 
266 data = "GET /thrown-exception?writeBefore HTTP/1.0\r\n" +
267  "\r\n";
269 {
270  do_check_eq(data, "preparing to throw...");
271 }
273 tests.push(test);
274 
275 data = "GET /thrown-exception?writeBeforeEmpty HTTP/1.0\r\n" +
276  "\r\n";
278 {
279  do_check_eq(data, "preparing to throw...");
280 }
282 tests.push(test);
283 
284 data = "GET /thrown-exception HTTP/1.0\r\n" +
285  "\r\n";
287 {
288  do_check_eq(data, "preparing to throw...");
289 }
290 test = new RawTest("localhost", PORT, data, checkThrownException),
291 tests.push(test);
292 
293 data = "GET /asap-later-write HTTP/1.0\r\n" +
294  "\r\n";
296 {
297  do_check_eq(data, "should only see this");
298 }
299 test = new RawTest("localhost", PORT, data, checkASAPLaterWrite),
300 tests.push(test);
301 
302 data = "GET /asap-later-finish HTTP/1.0\r\n" +
303  "\r\n";
305 {
306  do_check_eq(data, "should only see this");
307 }
308 test = new RawTest("localhost", PORT, data, checkASAPLaterFinish),
309 tests.push(test);
var data
do_check_eq(typeof PlacesUtils,"object")
function handleASAPLaterFinish(request, response)
var srv
var tests
function checkSeizeAfterAsync(data)
function handleASAPLaterWrite(request, response)
const PORT
function checkThrownExceptionWriteBeforeEmpty(data)
function checkThrownException(data)
function checkRawData(data)
function checkThrownExceptionWriteBefore(data)
function handleAsyncSeizure(request, response)
function handleThrownException(request, response)
function checkAsyncSeizure(data)
function runRawTests(testArray, done)
Definition: head_utils.js:501
function checkExceptions(data)
function createServer()
Definition: head_utils.js:53
var gThreadManager
Definition: httpd.js:188
function run_test()
function handleSeizeAfterAsync(request, response)
function RawTest(host, port, data, responseCheck)
Definition: head_utils.js:476
function checkASAPLaterFinish(data)
function callASAPLater(fun)
function testComplete(srv)
Definition: head_utils.js:292
const Cr
var test
const Ci
function checkException(fun, err, msg)
function checkASAPLaterWrite(data)
function msg
function handleRawData(request, response)
function callLater(msecs, callback)
Definition: head_utils.js:237
function handleExceptions(request, response)
function LineIterator(data)
Definition: head_utils.js:115
function handleTooLate(request, response)
function next()
function checkTooLate(data)