test_sjs.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  * Jeff Walden <jwalden+code@mit.edu>.
20  * Portions created by the Initial Developer are Copyright (C) 2007
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either the GNU General Public License Version 2 or later (the "GPL"), or
27  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38 
39 // tests support for server JS-generated pages
40 
41 const BASE = "http://localhost:4444";
42 
43 var sjs = do_get_file("data/sjs/cgi.sjs");
44 var srv;
45 var test;
46 var tests = [];
47 
48 
49 /*********************
50  * UTILITY FUNCTIONS *
51  *********************/
52 
53 function bytesToString(bytes)
54 {
55  return bytes.map(function(v) { return String.fromCharCode(v); }).join("");
56 }
57 
58 function skipCache(ch)
59 {
60  ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE;
61 }
62 
63 
64 /********************
65  * DEFINE THE TESTS *
66  ********************/
67 
73 function setupTests(throwing)
74 {
75  const TEST_URL = BASE + "/cgi.sjs" + (throwing ? "?throw" : "");
76 
77  // registerFile with SJS => raw text
78 
79  function setupFile(ch)
80  {
81  srv.registerFile("/cgi.sjs", sjs);
82  skipCache(ch);
83  }
84 
85  function verifyRawText(channel, cx, status, bytes)
86  {
87  dumpn(channel.originalURI.spec);
89  }
90 
91  test = new Test(TEST_URL, setupFile, null, verifyRawText);
92  tests.push(test);
93 
94 
95  // add mapping, => interpreted
96 
97  function addTypeMapping(ch)
98  {
99  srv.registerContentType("sjs", "sjs");
100  skipCache(ch);
101  }
102 
103  function checkType(ch, cx)
104  {
105  if (throwing)
106  {
107  do_check_false(ch.requestSucceeded);
108  do_check_eq(ch.responseStatus, 500);
109  }
110  else
111  {
112  do_check_eq(ch.contentType, "text/plain");
113  }
114  }
115 
116  function checkContents(ch, cx, status, data)
117  {
118  if (!throwing)
119  do_check_eq("PASS", bytesToString(data));
120  }
121 
122  test = new Test(TEST_URL, addTypeMapping, checkType, checkContents);
123  tests.push(test);
124 
125 
126  // remove file/type mapping, map containing directory => raw text
127 
128  function setupDirectoryAndRemoveType(ch)
129  {
130  dumpn("removing type mapping");
131  srv.registerContentType("sjs", null);
132  srv.registerFile("/cgi.sjs", null);
133  srv.registerDirectory("/", sjs.parent);
134  skipCache(ch);
135  }
136 
137  test = new Test(TEST_URL, setupDirectoryAndRemoveType, null, verifyRawText);
138  tests.push(test);
139 
140 
141  // add mapping, => interpreted
142 
143  function contentAndCleanup(ch, cx, status, data)
144  {
145  checkContents(ch, cx, status, data);
146 
147  // clean up state we've set up
148  srv.registerDirectory("/", null);
149  srv.registerContentType("sjs", null);
150  }
151 
152  test = new Test(TEST_URL, addTypeMapping, checkType, contentAndCleanup);
153  tests.push(test);
154 
155  // NB: No remaining state in the server right now! If we have any here,
156  // either the second run of tests (without ?throw) or the tests added
157  // after the two sets will almost certainly fail.
158 }
159 
160 
161 /*****************
162  * ADD THE TESTS *
163  *****************/
164 
165 setupTests(true);
166 setupTests(false);
167 
168 // Test that when extension-mappings are used, the entire filename cannot be
169 // treated as an extension -- there must be at least one dot for a filename to
170 // match an extension.
171 
172 function init(ch)
173 {
174  // clean up state we've set up
175  srv.registerDirectory("/", sjs.parent);
176  srv.registerContentType("sjs", "sjs");
177  skipCache(ch);
178 }
179 
180 function checkNotSJS(ch, cx, status, data)
181 {
182  do_check_neq("FAIL", bytesToString(data));
183 }
184 
185 test = new Test(BASE + "/sjs", init, null, checkNotSJS);
186 tests.push(test);
187 
188 // Test that Range requests are passed through to the SJS file without
189 // bounds checking.
190 
191 function rangeInit(expectedRangeHeader)
192 {
193  return function setupRangeRequest(ch)
194  {
195  ch.setRequestHeader("Range", expectedRangeHeader, false);
196  };
197 }
198 
199 function checkRangeResult(ch, cx)
200 {
201  try
202  {
203  var val = ch.getResponseHeader("Content-Range");
204  }
205  catch (e) { /* IDL doesn't specify a particular exception to require */ }
206  if (val !== undefined)
207  {
208  do_throw("should not have gotten a Content-Range header, but got one " +
209  "with this value: " + val);
210  }
211  do_check_eq(200, ch.responseStatus);
212  do_check_eq("OK", ch.responseStatusText);
213 }
214 
215 test = new Test(BASE + "/range-checker.sjs",
216  rangeInit("not-a-bytes-equals-specifier"),
218 tests.push(test);
219 test = new Test(BASE + "/range-checker.sjs",
220  rangeInit("bytes=-"),
222 tests.push(test);
223 test = new Test(BASE + "/range-checker.sjs",
224  rangeInit("bytes=1000000-"),
226 tests.push(test);
227 test = new Test(BASE + "/range-checker.sjs",
228  rangeInit("bytes=1-4"),
230 tests.push(test);
231 test = new Test(BASE + "/range-checker.sjs",
232  rangeInit("bytes=-4"),
234 tests.push(test);
235 
236 // One last test: for file mappings, the content-type is determined by the
237 // extension of the file on the server, not by the extension of the requested
238 // path.
239 
240 function setupFileMapping(ch)
241 {
242  srv.registerFile("/script.html", sjs);
243 }
244 
245 function onStart(ch, cx)
246 {
247  do_check_eq(ch.contentType, "text/plain");
248 }
249 
250 function onStop(ch, cx, status, data)
251 {
252  do_check_eq("PASS", bytesToString(data));
253 }
254 
255 test = new Test(BASE + "/script.html", setupFileMapping, onStart, onStop);
256 tests.push(test);
257 
258 
259 /*****************
260  * RUN THE TESTS *
261  *****************/
262 
263 function run_test()
264 {
265  srv = createServer();
266 
267  // Test for a content-type which isn't a field-value
268  try
269  {
270  srv.registerContentType("foo", "bar\nbaz");
271  throw "this server throws on content-types which aren't field-values";
272  }
273  catch (e)
274  {
275  isException(e, Cr.NS_ERROR_INVALID_ARG);
276  }
277 
278 
279  // NB: The server has no state at this point -- all state is set up and torn
280  // down in the tests, because we run the same tests twice with only a
281  // different query string on the requests, followed by the oddball
282  // test that doesn't care about throwing or not.
283 
284  srv.start(4444);
286 }
var test
Definition: test_sjs.js:45
var tests
Definition: test_sjs.js:46
do_check_eq(typeof PlacesUtils,"object")
var srv
Definition: test_sjs.js:44
function runHttpTests(testArray, done)
Definition: head_utils.js:340
function setupTests(throwing)
Definition: test_sjs.js:73
function dumpn(str)
Definition: httpd.js:172
function init(ch)
Definition: test_sjs.js:172
function bytesToString(bytes)
Definition: test_sjs.js:53
function createServer()
Definition: head_utils.js:53
var sjs
Definition: test_sjs.js:43
function checkRangeResult(ch, cx)
Definition: test_sjs.js:199
function skipCache(ch)
Definition: test_sjs.js:58
this _dialogInput val(dateText)
function fileContents(file)
Definition: head_utils.js:94
function setupFileMapping(ch)
Definition: test_sjs.js:240
function run_test()
Definition: test_sjs.js:263
return null
Definition: FeedWriter.js:1143
function testComplete(srv)
Definition: head_utils.js:292
function Test(path, initChannel, onStartRequest, onStopRequest)
Definition: head_utils.js:322
function checkNotSJS(ch, cx, status, data)
Definition: test_sjs.js:180
const Cr
function rangeInit(expectedRangeHeader)
Definition: test_sjs.js:191
const Ci
function onStart(ch, cx)
Definition: test_sjs.js:245
observe data
Definition: FeedWriter.js:1329
function onStop(ch, cx, status, data)
Definition: test_sjs.js:250
const BASE
Definition: test_sjs.js:41
function isException(e, code)
Definition: head_utils.js:207