test_registerdirectory.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) 2006
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 the registerDirectory API
40 
41 const BASE = "http://localhost:4444";
42 
43 var tests = [];
44 var test;
45 
46 
47 function nocache(ch)
48 {
49  ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE; // important!
50 }
51 
52 function notFound(ch)
53 {
54  do_check_eq(ch.responseStatus, 404);
55  do_check_false(ch.requestSucceeded);
56 }
57 
58 function checkOverride(ch)
59 {
60  do_check_eq(ch.responseStatus, 200);
61  do_check_eq(ch.responseStatusText, "OK");
62  do_check_true(ch.requestSucceeded);
63  do_check_eq(ch.getResponseHeader("Override-Succeeded"), "yes");
64 }
65 
66 function check200(ch)
67 {
68  do_check_eq(ch.responseStatus, 200);
69  do_check_eq(ch.responseStatusText, "OK");
70 }
71 
72 function checkFile(ch, cx, status, data)
73 {
74  do_check_eq(ch.responseStatus, 200);
75  do_check_true(ch.requestSucceeded);
76 
77  var actualFile = serverBasePath.clone();
78  actualFile.append("test_registerdirectory.js");
79  do_check_eq(ch.getResponseHeader("Content-Length"),
80  actualFile.fileSize.toString());
81  do_check_eq(data.map(function(v) String.fromCharCode(v)).join(""),
82  fileContents(actualFile));
83 }
84 
85 
86 /***********************
87  * without a base path *
88  ***********************/
89 
90 test = new Test(BASE + "/test_registerdirectory.js",
92 tests.push(test);
93 
94 
95 /********************
96  * with a base path *
97  ********************/
98 
99 test = new Test(BASE + "/test_registerdirectory.js",
100  function(ch)
101  {
102  nocache(ch);
103  serverBasePath = testsDirectory.clone();
104  srv.registerDirectory("/", serverBasePath);
105  },
106  null,
107  checkFile);
108 tests.push(test);
109 
110 
111 /*****************************
112  * without a base path again *
113  *****************************/
114 
115 test = new Test(BASE + "/test_registerdirectory.js",
116  function(ch)
117  {
118  nocache(ch);
120  srv.registerDirectory("/", serverBasePath);
121  },
122  notFound,
123  null);
124 tests.push(test);
125 
126 
127 /***************************
128  * registered path handler *
129  ***************************/
130 
131 test = new Test(BASE + "/test_registerdirectory.js",
132  function(ch)
133  {
134  nocache(ch);
135  srv.registerPathHandler("/test_registerdirectory.js",
137  },
139  null);
140 tests.push(test);
141 
142 
143 /************************
144  * removed path handler *
145  ************************/
146 
147 test = new Test(BASE + "/test_registerdirectory.js",
148  function init_registerDirectory6(ch)
149  {
150  nocache(ch);
151  srv.registerPathHandler("/test_registerdirectory.js", null);
152  },
153  notFound,
154  null);
155 tests.push(test);
156 
157 
158 /********************
159  * with a base path *
160  ********************/
161 
162 test = new Test(BASE + "/test_registerdirectory.js",
163  function(ch)
164  {
165  nocache(ch);
166 
167  // set the base path again
168  serverBasePath = testsDirectory.clone();
169  srv.registerDirectory("/", serverBasePath);
170  },
171  null,
172  checkFile);
173 tests.push(test);
174 
175 
176 /*************************
177  * ...and a path handler *
178  *************************/
179 
180 test = new Test(BASE + "/test_registerdirectory.js",
181  function(ch)
182  {
183  nocache(ch);
184  srv.registerPathHandler("/test_registerdirectory.js",
186  },
188  null);
189 tests.push(test);
190 
191 
192 /************************
193  * removed base handler *
194  ************************/
195 
196 test = new Test(BASE + "/test_registerdirectory.js",
197  function(ch)
198  {
199  nocache(ch);
201  srv.registerDirectory("/", serverBasePath);
202  },
204  null);
205 tests.push(test);
206 
207 
208 /************************
209  * removed path handler *
210  ************************/
211 
212 test = new Test(BASE + "/test_registerdirectory.js",
213  function(ch)
214  {
215  nocache(ch);
216  srv.registerPathHandler("/test_registerdirectory.js", null);
217  },
218  notFound,
219  null);
220 tests.push(test);
221 
222 
223 /*************************
224  * mapping set up, works *
225  *************************/
226 
227 test = new Test(BASE + "/foo/test_registerdirectory.js",
228  function(ch)
229  {
230  nocache(ch);
231  serverBasePath = testsDirectory.clone();
232  srv.registerDirectory("/foo/", serverBasePath);
233  },
234  check200,
235  null);
236 tests.push(test);
237 
238 
239 /*********************
240  * no mapping, fails *
241  *********************/
242 
243 test = new Test(BASE + "/foo/test_registerdirectory.js/test_registerdirectory.js",
244  nocache,
245  notFound,
246  null);
247 tests.push(test);
248 
249 
250 /******************
251  * mapping, works *
252  ******************/
253 
254 test = new Test(BASE + "/foo/test_registerdirectory.js/test_registerdirectory.js",
255  function(ch)
256  {
257  nocache(ch);
258  srv.registerDirectory("/foo/test_registerdirectory.js/",
260  },
261  null,
262  checkFile);
263 tests.push(test);
264 
265 
266 /************************************
267  * two mappings set up, still works *
268  ************************************/
269 
270 test = new Test(BASE + "/foo/test_registerdirectory.js",
272 tests.push(test);
273 
274 
275 /**************************
276  * remove topmost mapping *
277  **************************/
278 
279 test = new Test(BASE + "/foo/test_registerdirectory.js",
280  function(ch)
281  {
282  nocache(ch);
283  srv.registerDirectory("/foo/", null);
284  },
285  notFound,
286  null);
287 tests.push(test);
288 
289 
290 /**************************************
291  * lower mapping still present, works *
292  **************************************/
293 
294 test = new Test(BASE + "/foo/test_registerdirectory.js/test_registerdirectory.js",
296 tests.push(test);
297 
298 
299 /*******************
300  * mapping removed *
301  *******************/
302 
303 test = new Test(BASE + "/foo/test_registerdirectory.js/test_registerdirectory.js",
304  function(ch)
305  {
306  nocache(ch);
307  srv.registerDirectory("/foo/test_registerdirectory.js/", null);
308  },
309  notFound,
310  null);
311 tests.push(test);
312 
313 
314 
315 var srv;
318 
319 function run_test()
320 {
321  testsDirectory = do_get_cwd();
322 
323  srv = createServer();
324  srv.start(4444);
325 
327 }
328 
329 
330 // PATH HANDLERS
331 
332 // override of /test_registerdirectory.js
333 function override_test_registerdirectory(metadata, response)
334 {
335  response.setStatusLine("1.1", 200, "OK");
336  response.setHeader("Override-Succeeded", "yes", false);
337 
338  var body = "success!";
339  response.bodyOutputStream.write(body, body.length);
340 }
do_check_eq(typeof PlacesUtils,"object")
function run_test()
function runHttpTests(testArray, done)
Definition: head_utils.js:340
function nocache(ch)
const BASE
function override_test_registerdirectory(metadata, response)
function checkOverride(ch)
function createServer()
Definition: head_utils.js:53
var testsDirectory
function check200(ch)
function fileContents(file)
Definition: head_utils.js:94
var serverBasePath
return null
Definition: FeedWriter.js:1143
function checkFile(ch, cx, status, data)
function testComplete(srv)
Definition: head_utils.js:292
function Test(path, initChannel, onStartRequest, onStopRequest)
Definition: head_utils.js:322
const Ci
observe data
Definition: FeedWriter.js:1329
function notFound(ch)