|
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 /* This Source Code Form is subject to the terms of the Mozilla Public |
|
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
|
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
|
6 |
|
7 // tests the registerPrefixHandler API |
|
8 |
|
9 XPCOMUtils.defineLazyGetter(this, "BASE", function() { |
|
10 return "http://localhost:" + srv.identity.primaryPort; |
|
11 }); |
|
12 |
|
13 function nocache(ch) |
|
14 { |
|
15 ch.loadFlags |= Ci.nsIRequest.LOAD_BYPASS_CACHE; // important! |
|
16 } |
|
17 |
|
18 function notFound(ch) |
|
19 { |
|
20 do_check_eq(ch.responseStatus, 404); |
|
21 do_check_false(ch.requestSucceeded); |
|
22 } |
|
23 |
|
24 function makeCheckOverride(magic) |
|
25 { |
|
26 return (function checkOverride(ch) |
|
27 { |
|
28 do_check_eq(ch.responseStatus, 200); |
|
29 do_check_eq(ch.responseStatusText, "OK"); |
|
30 do_check_true(ch.requestSucceeded); |
|
31 do_check_eq(ch.getResponseHeader("Override-Succeeded"), magic); |
|
32 }); |
|
33 } |
|
34 |
|
35 XPCOMUtils.defineLazyGetter(this, "tests", function() { |
|
36 return [ |
|
37 new Test(BASE + "/prefix/dummy", prefixHandler, null, |
|
38 makeCheckOverride("prefix")), |
|
39 new Test(BASE + "/prefix/dummy", pathHandler, null, |
|
40 makeCheckOverride("path")), |
|
41 new Test(BASE + "/prefix/subpath/dummy", longerPrefixHandler, null, |
|
42 makeCheckOverride("subpath")), |
|
43 new Test(BASE + "/prefix/dummy", removeHandlers, null, notFound), |
|
44 new Test(BASE + "/prefix/subpath/dummy", newPrefixHandler, null, |
|
45 makeCheckOverride("subpath")) |
|
46 ]; |
|
47 }); |
|
48 |
|
49 /*************************** |
|
50 * registered prefix handler * |
|
51 ***************************/ |
|
52 |
|
53 function prefixHandler(channel) |
|
54 { |
|
55 nocache(channel); |
|
56 srv.registerPrefixHandler("/prefix/", makeOverride("prefix")); |
|
57 } |
|
58 |
|
59 /******************************** |
|
60 * registered path handler on top * |
|
61 ********************************/ |
|
62 |
|
63 function pathHandler(channel) |
|
64 { |
|
65 nocache(channel); |
|
66 srv.registerPathHandler("/prefix/dummy", makeOverride("path")); |
|
67 } |
|
68 |
|
69 /********************************** |
|
70 * registered longer prefix handler * |
|
71 **********************************/ |
|
72 |
|
73 function longerPrefixHandler(channel) |
|
74 { |
|
75 nocache(channel); |
|
76 srv.registerPrefixHandler("/prefix/subpath/", makeOverride("subpath")); |
|
77 } |
|
78 |
|
79 /************************ |
|
80 * removed prefix handler * |
|
81 ************************/ |
|
82 |
|
83 function removeHandlers(channel) |
|
84 { |
|
85 nocache(channel); |
|
86 srv.registerPrefixHandler("/prefix/", null); |
|
87 srv.registerPathHandler("/prefix/dummy", null); |
|
88 } |
|
89 |
|
90 /***************************** |
|
91 * re-register shorter handler * |
|
92 *****************************/ |
|
93 |
|
94 function newPrefixHandler(channel) |
|
95 { |
|
96 nocache(channel); |
|
97 srv.registerPrefixHandler("/prefix/", makeOverride("prefix")); |
|
98 } |
|
99 |
|
100 var srv; |
|
101 var serverBasePath; |
|
102 var testsDirectory; |
|
103 |
|
104 function run_test() |
|
105 { |
|
106 testsDirectory = do_get_profile(); |
|
107 |
|
108 srv = createServer(); |
|
109 srv.start(-1); |
|
110 |
|
111 runHttpTests(tests, testComplete(srv)); |
|
112 } |
|
113 |
|
114 // PATH HANDLERS |
|
115 |
|
116 // generate an override |
|
117 function makeOverride(magic) |
|
118 { |
|
119 return (function override(metadata, response) |
|
120 { |
|
121 response.setStatusLine("1.1", 200, "OK"); |
|
122 response.setHeader("Override-Succeeded", magic, false); |
|
123 |
|
124 var body = "success!"; |
|
125 response.bodyOutputStream.write(body, body.length); |
|
126 }); |
|
127 } |