test_sjs_state.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) 2008
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  * Jeff Walden <jwalden+code@mit.edu>
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 // exercises the server's state-preservation API
41 
42 const PORT = 4444;
43 
44 var srv;
45 
46 function run_test()
47 {
48  srv = createServer();
49  var sjsDir = do_get_file("data/sjs/");
50  srv.registerDirectory("/", sjsDir);
51  srv.registerContentType("sjs", "sjs");
52  srv.registerPathHandler("/path-handler", pathHandler);
53  srv.start(PORT);
54 
55  function done()
56  {
57  do_check_eq(srv.getSharedState("shared-value"), "done!");
58  do_check_eq(srv.getState("/path-handler", "private-value"),
59  "pathHandlerPrivate2");
60  do_check_eq(srv.getState("/state1.sjs", "private-value"),
61  "");
62  do_check_eq(srv.getState("/state2.sjs", "private-value"),
63  "newPrivate5");
64  do_test_pending();
65  srv.stop(function() { do_test_finished(); });
66  }
67 
69 }
70 
71 
72 /************
73  * HANDLERS *
74  ************/
75 
76 var firstTime = true;
77 
78 function pathHandler(request, response)
79 {
80  response.setHeader("Cache-Control", "no-cache", false);
81 
82  response.setHeader("X-Old-Shared-Value", srv.getSharedState("shared-value"),
83  false);
84  response.setHeader("X-Old-Private-Value", srv.getState("/path-handler", "private-value"),
85  false);
86 
87  var privateValue, sharedValue;
88  if (firstTime)
89  {
90  firstTime = false;
91  privateValue = "pathHandlerPrivate";
92  sharedValue = "pathHandlerShared";
93  }
94  else
95  {
96  privateValue = "pathHandlerPrivate2";
97  sharedValue = "";
98  }
99 
100  srv.setState("/path-handler", "private-value", privateValue);
101  srv.setSharedState("shared-value", sharedValue);
102 
103  response.setHeader("X-New-Private-Value", privateValue, false);
104  response.setHeader("X-New-Shared-Value", sharedValue, false);
105 }
106 
107 
108 /***************
109  * BEGIN TESTS *
110  ***************/
111 
112 var test;
113 var tests = [];
114 
115 /* Hack around bug 474845 for now. */
116 function getHeaderFunction(ch)
117 {
118  function getHeader(name)
119  {
120  try
121  {
122  return ch.getResponseHeader(name);
123  }
124  catch (e)
125  {
126  if (e.result !== Cr.NS_ERROR_NOT_AVAILABLE)
127  throw e;
128  }
129  return "";
130  }
131  return getHeader;
132 }
133 
134 function expectValues(ch, oldShared, newShared, oldPrivate, newPrivate)
135 {
136  var getHeader = getHeaderFunction(ch);
137 
138  do_check_eq(ch.responseStatus, 200);
139  do_check_eq(getHeader("X-Old-Shared-Value"), oldShared);
140  do_check_eq(getHeader("X-New-Shared-Value"), newShared);
141  do_check_eq(getHeader("X-Old-Private-Value"), oldPrivate);
142  do_check_eq(getHeader("X-New-Private-Value"), newPrivate);
143 }
144 
145 
146 test = new Test("http://localhost:4444/state1.sjs?" +
147  "newShared=newShared&newPrivate=newPrivate",
149 tests.push(test);
150 
151 function start_initial(ch, cx)
152 {
153 dumpn("XXX start_initial");
154  expectValues(ch, "", "newShared", "", "newPrivate");
155 }
156 
157 
158 test = new Test("http://localhost:4444/state1.sjs?" +
159  "newShared=newShared2&newPrivate=newPrivate2",
161 tests.push(test);
162 
163 function start_overwrite(ch, cx)
164 {
165  expectValues(ch, "newShared", "newShared2", "newPrivate", "newPrivate2");
166 }
167 
168 
169 test = new Test("http://localhost:4444/state1.sjs?" +
170  "newShared=&newPrivate=newPrivate3",
172 tests.push(test);
173 
174 function start_remove(ch, cx)
175 {
176  expectValues(ch, "newShared2", "", "newPrivate2", "newPrivate3");
177 }
178 
179 
180 test = new Test("http://localhost:4444/path-handler",
182 tests.push(test);
183 
184 function start_handler(ch, cx)
185 {
186  expectValues(ch, "", "pathHandlerShared", "", "pathHandlerPrivate");
187 }
188 
189 
190 test = new Test("http://localhost:4444/path-handler",
192 tests.push(test);
193 
194 function start_handler_again(ch, cx)
195 {
196  expectValues(ch, "pathHandlerShared", "",
197  "pathHandlerPrivate", "pathHandlerPrivate2");
198 }
199 
200 
201 test = new Test("http://localhost:4444/state2.sjs?" +
202  "newShared=newShared4&newPrivate=newPrivate4",
204 tests.push(test);
205 
206 function start_other_initial(ch, cx)
207 {
208  expectValues(ch, "", "newShared4", "", "newPrivate4");
209 }
210 
211 
212 test = new Test("http://localhost:4444/state2.sjs?" +
213  "newShared=",
215 tests.push(test);
216 
218 {
219  expectValues(ch, "newShared4", "", "newPrivate4", "");
220 }
221 
222 
223 test = new Test("http://localhost:4444/state2.sjs?" +
224  "newShared=newShared5&newPrivate=newPrivate5",
226 tests.push(test);
227 
228 function start_other_set_new(ch, cx)
229 {
230  expectValues(ch, "", "newShared5", "newPrivate4", "newPrivate5");
231 }
232 
233 
234 test = new Test("http://localhost:4444/state1.sjs?" +
235  "newShared=done!&newPrivate=",
237 tests.push(test);
238 
240 {
241  expectValues(ch, "newShared5", "done!", "newPrivate3", "");
242 }
function run_test()
function start_set_remove_original(ch, cx)
do_check_eq(typeof PlacesUtils,"object")
function start_remove(ch, cx)
function runHttpTests(testArray, done)
Definition: head_utils.js:340
function start_overwrite(ch, cx)
function start_other_initial(ch, cx)
function start_handler_again(ch, cx)
function dumpn(str)
Definition: httpd.js:172
function getHeaderFunction(ch)
var firstTime
function start_initial(ch, cx)
function createServer()
Definition: head_utils.js:53
function expectValues(ch, oldShared, newShared, oldPrivate, newPrivate)
function start_other_set_new(ch, cx)
unique done
var test
const PORT
function start_handler(ch, cx)
return null
Definition: FeedWriter.js:1143
function start_other_remove_ignore(ch, cx)
function Test(path, initChannel, onStartRequest, onStopRequest)
Definition: head_utils.js:322
const Cr
var tests
function pathHandler(request, response)
var srv