test_default_index_handler.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 // checks for correct output with the default index handler, mostly to do
40 // escaping checks -- highly dependent on the default index handler output
41 // format
42 
44 
45 function run_test()
46 {
48 
49  srv = createServer();
50  srv.registerDirectory("/", dir);
51 
52  var nameDir = do_get_file("data/name-scheme/");
53  srv.registerDirectory("/bar/", nameDir);
54 
55  srv.start(4444);
56 
57  function done()
58  {
59  do_test_pending();
61  srv.stop(function() { do_test_finished(); });
62  }
63 
65 }
66 
68 {
69  dir = Cc["@mozilla.org/file/directory_service;1"]
70  .getService(Ci.nsIProperties)
71  .get("TmpD", Ci.nsIFile);
72  dir.append("index_handler_test_" + Math.random());
73  dir.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0744);
74 
75  // populate with test directories, files, etc.
76  // Files must be in expected order of display on the index page!
77 
78  var files = [];
79 
80  makeFile("aa_directory", true, dir, files);
81  makeFile("Ba_directory", true, dir, files);
82  makeFile("bb_directory", true, dir, files);
83  makeFile("foo", true, dir, files);
84  makeFile("a_file", false, dir, files);
85  makeFile("B_file", false, dir, files);
86  makeFile("za'z", false, dir, files);
87  makeFile("zb&z", false, dir, files);
88  makeFile("zc<q", false, dir, files);
89  makeFile('zd"q', false, dir, files);
90  makeFile("ze%g", false, dir, files);
91  makeFile("zf%200h", false, dir, files);
92  makeFile("zg>m", false, dir, files);
93 
94  dirEntries = [files];
95 
96  var subdir = dir.clone();
97  subdir.append("foo");
98 
99  files = [];
100 
101  makeFile("aa_dir", true, subdir, files);
102  makeFile("b_dir", true, subdir, files);
103  makeFile("AA_file.txt", false, subdir, files);
104  makeFile("test.txt", false, subdir, files);
105 
106  dirEntries.push(files);
107 }
108 
110 {
111  dir.remove(true);
112 }
113 
114 
115 /*************
116  * UTILITIES *
117  *************/
118 
120 function hiddenDataCheck(bytes, uri, path)
121 {
122  var data = String.fromCharCode.apply(null, bytes);
123 
124  var parser = Cc["@mozilla.org/xmlextras/domparser;1"]
125  .createInstance(Ci.nsIDOMParser);
126 
127  // Note: the index format isn't XML -- it's actually HTML -- but we require
128  // the index format also be valid XML, albeit XML without namespaces,
129  // XML declarations, etc. Doing this simplifies output checking.
130  try
131  {
132  var doc = parser.parseFromString(data, "application/xml");
133  }
134  catch (e)
135  {
136  do_throw("document failed to parse as XML");
137  }
138 
139  // See all the .QueryInterface()s and .item()s happening here? That's because
140  // xpcshell sucks and doesn't have classinfo, so no automatic interface
141  // flattening or array-style access to items in NodeLists. Suck.
142 
143  var body = doc.documentElement.getElementsByTagName("body");
144  do_check_eq(body.length, 1);
145  body = body.item(0);
146 
147  // header
148  var header = body.QueryInterface(Ci.nsIDOMElement)
149  .getElementsByTagName("h1");
150  do_check_eq(header.length, 1);
151 
152  do_check_eq(header.item(0).QueryInterface(Ci.nsIDOM3Node).textContent, path);
153 
154  // files
155  var lst = body.getElementsByTagName("ol");
156  do_check_eq(lst.length, 1);
157  var items = lst.item(0).QueryInterface(Ci.nsIDOMElement)
158  .getElementsByTagName("li");
159 
160  var ios = Cc["@mozilla.org/network/io-service;1"]
161  .getService(Ci.nsIIOService);
162 
163  var top = ios.newURI(uri, null, null);
164 
165  // N.B. No ERROR_IF_SEE_THIS.txt^ file!
166  var dirEntries = [{name: "file.txt", isDirectory: false},
167  {name: "SHOULD_SEE_THIS.txt^", isDirectory: false}];
168 
169  for (var i = 0; i < items.length; i++)
170  {
171  var link = items.item(i)
172  .childNodes
173  .item(0)
174  .QueryInterface(Ci.nsIDOM3Node)
175  .QueryInterface(Ci.nsIDOMElement);
176  var f = dirEntries[i];
177 
178  var sep = f.isDirectory ? "/" : "";
179 
180  do_check_eq(link.textContent, f.name + sep);
181 
182  uri = ios.newURI(link.getAttribute("href"), null, top);
183  do_check_eq(decodeURIComponent(uri.path), path + f.name + sep);
184  }
185 }
186 
204 function dataCheck(bytes, uri, path, dirEntries)
205 {
206  var data = String.fromCharCode.apply(null, bytes);
207 
208  var parser = Cc["@mozilla.org/xmlextras/domparser;1"]
209  .createInstance(Ci.nsIDOMParser);
210 
211  // Note: the index format isn't XML -- it's actually HTML -- but we require
212  // the index format also be valid XML, albeit XML without namespaces,
213  // XML declarations, etc. Doing this simplifies output checking.
214  try
215  {
216  var doc = parser.parseFromString(data, "application/xml");
217  }
218  catch (e)
219  {
220  do_throw("document failed to parse as XML");
221  }
222 
223  // See all the .QueryInterface()s and .item()s happening here? That's because
224  // xpcshell sucks and doesn't have classinfo, so no automatic interface
225  // flattening or array-style access to items in NodeLists. Suck.
226 
227  var body = doc.documentElement.getElementsByTagName("body");
228  do_check_eq(body.length, 1);
229  body = body.item(0);
230 
231  // header
232  var header = body.QueryInterface(Ci.nsIDOMElement)
233  .getElementsByTagName("h1");
234  do_check_eq(header.length, 1);
235 
236  do_check_eq(header.item(0).QueryInterface(Ci.nsIDOM3Node).textContent, path);
237 
238  // files
239  var lst = body.getElementsByTagName("ol");
240  do_check_eq(lst.length, 1);
241  var items = lst.item(0).QueryInterface(Ci.nsIDOMElement)
242  .getElementsByTagName("li");
243 
244  var ios = Cc["@mozilla.org/network/io-service;1"]
245  .getService(Ci.nsIIOService);
246 
247  var dirURI = ios.newURI(uri, null, null);
248 
249  for (var i = 0; i < items.length; i++)
250  {
251  var link = items.item(i)
252  .childNodes
253  .item(0)
254  .QueryInterface(Ci.nsIDOM3Node)
255  .QueryInterface(Ci.nsIDOMElement);
256  var f = dirEntries[i];
257 
258  var sep = f.isDirectory ? "/" : "";
259 
260  do_check_eq(link.textContent, f.name + sep);
261 
262  uri = ios.newURI(link.getAttribute("href"), null, top);
263  do_check_eq(decodeURIComponent(uri.path), path + f.name + sep);
264  }
265 }
266 
272 function makeFile(name, isDirectory, parentDir, lst)
273 {
274  var type = Ci.nsIFile[isDirectory ? "DIRECTORY_TYPE" : "NORMAL_FILE_TYPE"];
275  var file = parentDir.clone();
276 
277  try
278  {
279  file.append(name);
280  file.create(type, 0755);
281  lst.push({name: name, isDirectory: isDirectory});
282  }
283  catch (e) { /* OS probably doesn't like file name, skip */ }
284 }
285 
286 /*********
287  * TESTS *
288  *********/
289 
290 var tests = [];
291 var test;
292 
293 // check top-level directory listing
294 test = new Test("http://localhost:4444/",
296 tests.push(test);
297 function start(ch)
298 {
299  do_check_eq(ch.getResponseHeader("Content-Type"), "text/html");
300 }
301 function stopRootDirectory(ch, cx, status, data)
302 {
303  dataCheck(data, "http://localhost:4444/", "/", dirEntries[0]);
304 }
305 
306 
307 // check non-top-level, too
308 test = new Test("http://localhost:4444/foo/",
310 tests.push(test);
311 function stopFooDirectory(ch, cx, status, data)
312 {
313  dataCheck(data, "http://localhost:4444/foo/", "/foo/", dirEntries[1]);
314 }
315 
316 
317 // trailing-caret leaf with hidden files
318 test = new Test("http://localhost:4444/bar/folder^/",
320 tests.push(test);
321 function stopTrailingCaretDirectory(ch, cx, status, data)
322 {
323  hiddenDataCheck(data, "http://localhost:4444/bar/folder^/", "/bar/folder^/");
324 }
function createTestDirectory()
function start(ch)
const Cc
do_check_eq(typeof PlacesUtils,"object")
function stopTrailingCaretDirectory(ch, cx, status, data)
function doc() browser.contentDocument
function runHttpTests(testArray, done)
Definition: head_utils.js:340
var header
Definition: FeedWriter.js:953
function createServer()
Definition: head_utils.js:53
function stopFooDirectory(ch, cx, status, data)
function destroyTestDirectory()
function makeFile(name, isDirectory, parentDir, lst)
unique done
function run_test()
return null
Definition: FeedWriter.js:1143
var uri
Definition: FeedWriter.js:1135
function Test(path, initChannel, onStartRequest, onStopRequest)
Definition: head_utils.js:322
function hiddenDataCheck(bytes, uri, path)
const Ci
var ios
Definition: head_feeds.js:5
function stopRootDirectory(ch, cx, status, data)
observe data
Definition: FeedWriter.js:1329
function dataCheck(bytes, uri, path, dirEntries)
_getSelectedPageStyle s i
var file